Skip to content

Commit

Permalink
Add basic tsconfig.json validation (#47595)
Browse files Browse the repository at this point in the history
* Add validate-tsconfig script

* Fix lint errors

* Add tsconfig linting to lint-staged
  • Loading branch information
noisysocks authored and ntsekouras committed Feb 9, 2023
1 parent f12ecd5 commit 46a938a
Show file tree
Hide file tree
Showing 12 changed files with 142 additions and 12 deletions.
63 changes: 63 additions & 0 deletions bin/validate-tsconfig.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
#!/usr/bin/env node

/**
* External dependencies
*/
// @ts-ignore
import glob from 'glob';
import { dirname, basename } from 'path';
import stripJsonComments from 'strip-json-comments';
import { readFileSync } from 'fs';

let hasErrors = false;

const rootTsconfigJson = JSON.parse( readFileSync( 'tsconfig.json', 'utf8' ) );

const packagesWithTypes = glob
.sync( 'packages/*/tsconfig.json' )
.map( ( tsconfigPath ) => basename( dirname( tsconfigPath ) ) );

for ( const packageName of packagesWithTypes ) {
if (
! rootTsconfigJson.references.some(
( reference ) => reference.path === `packages/${ packageName }`
)
) {
console.error(
`Missing reference to "packages/${ packageName }" in root tsconfig.json`
);
hasErrors = true;
}

const packageJson = JSON.parse(
readFileSync( `packages/${ packageName }/package.json`, 'utf8' )
);
const tsconfigJson = JSON.parse(
stripJsonComments(
readFileSync( `packages/${ packageName }/tsconfig.json`, 'utf8' )
)
);
if ( packageJson.dependencies ) {
for ( const dependency of Object.keys( packageJson.dependencies ) ) {
if ( dependency.startsWith( '@wordpress/' ) ) {
const dependencyPackageName = dependency.slice(
'@wordpress/'.length
);
if (
packagesWithTypes.includes( dependencyPackageName ) &&
! tsconfigJson.references?.some(
( reference ) =>
reference.path === `../${ dependencyPackageName }`
)
) {
console.error(
`Missing reference to "../${ dependencyPackageName }" in packages/${ packageName }/tsconfig.json`
);
hasErrors = true;
}
}
}
}
}

process.exit( hasErrors ? 1 : 0 );
14 changes: 11 additions & 3 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 6 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,7 @@
"source-map-loader": "3.0.0",
"sprintf-js": "1.1.1",
"storybook-source-link": "2.0.4",
"strip-json-comments": "5.0.0",
"style-loader": "3.2.1",
"terser-webpack-plugin": "5.1.4",
"typescript": "4.4.2",
Expand Down Expand Up @@ -266,12 +267,13 @@
"fixtures:regenerate": "npm-run-all fixtures:clean fixtures:generate",
"format": "wp-scripts format",
"format:php": "wp-env run composer run-script format",
"lint": "concurrently \"npm run lint:lockfile\" \"npm run lint:js\" \"npm run lint:pkg-json\" \"npm run lint:css\"",
"lint": "concurrently \"npm run lint:lockfile\" \"npm run lint:tsconfig\" \"npm run lint:js\" \"npm run lint:pkg-json\" \"npm run lint:css\"",
"lint:css": "wp-scripts lint-style \"**/*.scss\"",
"lint:css:fix": "npm run lint:css -- --fix",
"lint:js": "wp-scripts lint-js",
"lint:js:fix": "npm run lint:js -- --fix",
"lint:lockfile": "node ./bin/validate-package-lock.js",
"lint:tsconfig": "node ./bin/validate-tsconfig.mjs",
"lint:md:docs": "wp-scripts lint-md-docs",
"prelint:php": "wp-env run composer \"update --no-interaction\"",
"lint:php": "wp-env run composer run-script lint",
Expand Down Expand Up @@ -342,6 +344,9 @@
"npm run docs:theme-ref",
"node ./bin/api-docs/are-api-docs-unstaged.js",
"node ./bin/packages/lint-staged-typecheck.js"
],
"**/tsconfig.json": [
"npm run lint:tsconfig"
]
},
"wp-env": {
Expand Down
20 changes: 19 additions & 1 deletion packages/block-editor/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,28 @@
"declarationDir": "build-types"
},
"references": [
{ "path": "../a11y" },
{ "path": "../api-fetch" },
{ "path": "../blob" },
{ "path": "../components" },
{ "path": "../compose" },
{ "path": "../data" },
{ "path": "../date" },
{ "path": "../deprecated" },
{ "path": "../dom" },
{ "path": "../element" },
{ "path": "../escape-html" },
{ "path": "../hooks" },
{ "path": "../data" }
{ "path": "../html-entities" },
{ "path": "../i18n" },
{ "path": "../icons" },
{ "path": "../is-shallow-equal" },
{ "path": "../keycodes" },
{ "path": "../style-engine" },
{ "path": "../token-list" },
{ "path": "../url" },
{ "path": "../warning" },
{ "path": "../wordcount" }
],
// NOTE: This package is being progressively typed. You are encouraged to
// expand this array with files which can be type-checked. At some point in
Expand Down
24 changes: 23 additions & 1 deletion packages/block-library/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,28 @@
"types": [ "gutenberg-env" ],
"strictNullChecks": true
},
"references": [ { "path": "../element" } ],
"references": [
{ "path": "../a11y" },
{ "path": "../api-fetch" },
{ "path": "../autop" },
{ "path": "../blob" },
{ "path": "../block-editor" },
{ "path": "../components" },
{ "path": "../compose" },
{ "path": "../core-data" },
{ "path": "../data" },
{ "path": "../date" },
{ "path": "../deprecated" },
{ "path": "../dom" },
{ "path": "../element" },
{ "path": "../escape-html" },
{ "path": "../hooks" },
{ "path": "../html-entities" },
{ "path": "../i18n" },
{ "path": "../icons" },
{ "path": "../keycodes" },
{ "path": "../primitives" },
{ "path": "../url" }
],
"include": [ "src/**/*.ts", "src/**/*.tsx" ]
}
5 changes: 4 additions & 1 deletion packages/components/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,13 @@
{ "path": "../deprecated" },
{ "path": "../dom" },
{ "path": "../element" },
{ "path": "../html-entities" },
{ "path": "../escape-html" },
{ "path": "../hooks" },
{ "path": "../html-entities" },
{ "path": "../i18n" },
{ "path": "../icons" },
{ "path": "../is-shallow-equal" },
{ "path": "../keycodes" },
{ "path": "../primitives" },
{ "path": "../react-i18n" },
{ "path": "../warning" }
Expand Down
7 changes: 4 additions & 3 deletions packages/compose/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,12 @@
"types": [ "gutenberg-env" ]
},
"references": [
{ "path": "../element" },
{ "path": "../priority-queue" },
{ "path": "../deprecated" },
{ "path": "../dom" },
{ "path": "../element" },
{ "path": "../is-shallow-equal" },
{ "path": "../keycodes" }
{ "path": "../keycodes" },
{ "path": "../priority-queue" }
],
"include": [ "src/**/*" ]
}
1 change: 1 addition & 0 deletions packages/core-data/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
},
"references": [
{ "path": "../api-fetch" },
{ "path": "../compose" },
{ "path": "../data" },
{ "path": "../deprecated" },
{ "path": "../element" },
Expand Down
1 change: 1 addition & 0 deletions packages/date/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,6 @@
"declarationDir": "build-types",
"noUnusedParameters": false
},
"references": [ { "path": "../deprecated" } ],
"include": [ "src/**/*" ]
}
5 changes: 5 additions & 0 deletions packages/e2e-test-utils-playwright/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,10 @@
"allowJs": true,
"checkJs": false
},
"references": [
{ "path": "../api-fetch" },
{ "path": "../keycodes" },
{ "path": "../url" }
],
"include": [ "src/**/*" ]
}
1 change: 1 addition & 0 deletions packages/eslint-plugin/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
"rootDir": "rules",
"declarationDir": "build-types"
},
"references": [ { "path": "../prettier-config" } ],
// NOTE: This package is being progressively typed. You are encouraged to
// expand this array with files which can be type-checked. At some point in
// the future, this can be simplified to an `includes` of `src/**/*`.
Expand Down
6 changes: 4 additions & 2 deletions tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
{ "path": "packages/autop" },
{ "path": "packages/blob" },
{ "path": "packages/block-editor" },
{ "path": "packages/block-library" },
{ "path": "packages/block-serialization-default-parser" },
{ "path": "packages/components" },
{ "path": "packages/compose" },
Expand All @@ -15,13 +16,13 @@
{ "path": "packages/deprecated" },
{ "path": "packages/docgen" },
{ "path": "packages/dom" },
{ "path": "packages/element" },
{ "path": "packages/dom-ready" },
{ "path": "packages/e2e-test-utils-playwright" },
{ "path": "packages/element" },
{ "path": "packages/escape-html" },
{ "path": "packages/eslint-plugin" },
{ "path": "packages/html-entities" },
{ "path": "packages/hooks" },
{ "path": "packages/html-entities" },
{ "path": "packages/i18n" },
{ "path": "packages/icons" },
{ "path": "packages/is-shallow-equal" },
Expand All @@ -32,6 +33,7 @@
{ "path": "packages/priority-queue" },
{ "path": "packages/project-management-automation" },
{ "path": "packages/react-i18n" },
{ "path": "packages/redux-routine" },
{ "path": "packages/report-flaky-tests" },
{ "path": "packages/style-engine" },
{ "path": "packages/token-list" },
Expand Down

0 comments on commit 46a938a

Please sign in to comment.