Skip to content

Commit 96c6c25

Browse files
shadowspawnaweebit
andauthored
Refactor type-checking setup (#1969)
* Refactor type-checking setup * Refactor tsconfig particularly to enable loose check in VSCode, strict checks run separately for type definitions * Simplify includes for tsconfig * Explicitly separate the tsconfig for use with npm run-scripts * Improve comment * Resolved couple of work-in-progress comments * Update tsconfig to recommended node16 lib/module/target * Make checks strict by default and opt-out * Restore broken code to merge later changes * Updates after merge --------- Co-authored-by: Wee Bit <[email protected]>
1 parent 744ee3f commit 96c6c25

12 files changed

+86
-39
lines changed

.eslintrc.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ const javascriptSettings = {
1616
const typescriptSettings = {
1717
files: ['*.ts', '*.mts'],
1818
parserOptions: {
19-
project: './tsconfig.json'
19+
project: './tsconfig.ts.json'
2020
},
2121
plugins: [
2222
'@typescript-eslint'

index.js

+2-6
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,12 @@ const { CommanderError, InvalidArgumentError } = require('./lib/error.js');
44
const { Help } = require('./lib/help.js');
55
const { Option } = require('./lib/option.js');
66

7-
// @ts-check
8-
97
/**
108
* Expose the root command.
119
*/
1210

13-
const program = new Command();
14-
exports = module.exports = program; // default export (deprecated)
15-
exports.program = program; // more explicit access to global command
16-
11+
exports = module.exports = new Command();
12+
exports.program = exports; // More explicit access to global command.
1713
// createArgument, createCommand, and createOption are implicitly available as they are methods on program.
1814

1915
/**

jest.config.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ const config = {
22
testEnvironment: 'node',
33
collectCoverage: true,
44
transform: {
5-
'^.+\\.tsx?$': 'ts-jest'
5+
'^.+\\.tsx?$': ['ts-jest', { tsconfig: 'tsconfig.ts.json' }]
66
},
77
testPathIgnorePatterns: [
88
'/node_modules/'

lib/argument.js

-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
const { InvalidArgumentError } = require('./error.js');
22

3-
// @ts-check
4-
53
class Argument {
64
/**
75
* Initialize a new command argument with the given name and description.

lib/command.js

-2
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,6 @@ const { Help } = require('./help.js');
1010
const { Option, splitOptionFlags, DualOptions } = require('./option.js');
1111
const { suggestSimilar } = require('./suggestSimilar');
1212

13-
// @ts-check
14-
1513
class Command extends EventEmitter {
1614
/**
1715
* Initialize a new `Command`.

lib/error.js

-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
// @ts-check
2-
31
/**
42
* CommanderError class
53
* @class

lib/help.js

-2
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,6 @@ const { humanReadableArgName } = require('./argument.js');
88
* @typedef { import("./option.js").Option } Option
99
*/
1010

11-
// @ts-check
12-
1311
// Although this is a class, methods are static in style to allow override using subclass or just functions.
1412
class Help {
1513
constructor() {

lib/option.js

-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
const { InvalidArgumentError } = require('./error.js');
22

3-
// @ts-check
4-
53
class Option {
64
/**
75
* Initialize a new `Option` with the given `flags` and `description`.

package.json

+4-4
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,11 @@
2222
"lint": "npm run lint:javascript && npm run lint:typescript",
2323
"lint:javascript": "eslint index.js esm.mjs \"lib/*.js\" \"tests/**/*.js\"",
2424
"lint:typescript": "eslint typings/*.ts tests/*.ts",
25-
"test": "jest && npm run test-typings",
25+
"test": "jest && npm run typecheck-ts",
2626
"test-esm": "node ./tests/esm-imports-test.mjs",
27-
"test-typings": "tsd",
28-
"typescript-checkJS": "tsc --allowJS --checkJS index.js lib/*.js --noEmit",
29-
"test-all": "npm run test && npm run lint && npm run typescript-checkJS && npm run test-esm"
27+
"typecheck-ts": "tsd && tsc -p tsconfig.ts.json",
28+
"typecheck-js": "tsc -p tsconfig.js.json",
29+
"test-all": "npm run test && npm run lint && npm run typecheck-js && npm run test-esm"
3030
},
3131
"files": [
3232
"index.js",

tsconfig.js.json

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
{ /*
2+
Simple override including just JavaScript files.
3+
Used by npm run-script typecheck-js
4+
*/
5+
/* Visit https://aka.ms/tsconfig to read more about tsconfig configuration. */
6+
"extends": "./tsconfig.json",
7+
"include": [
8+
/* All JavaScript targets from tsconfig.json include. */
9+
"*.js",
10+
"*.mjs",
11+
"lib/**/*.js"
12+
],
13+
}

tsconfig.json

+45-17
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,47 @@
11
{
2-
"compilerOptions": {
3-
"module": "commonjs",
4-
"lib": [
5-
"es6"
6-
],
7-
"noImplicitAny": true,
8-
"noImplicitThis": true,
9-
"strictNullChecks": true,
10-
"types": [
11-
"node",
12-
"jest"
13-
],
14-
"esModuleInterop": true, // Mainly so can test an import problem which only occurs with this option on!
15-
"noEmit": true,
16-
"forceConsistentCasingInFileNames": true
17-
},
18-
"include": ["**/*.ts"],
2+
/*
3+
TypeScript is being used to do type checking across both JavaScript and TypeScript files.
4+
In particular, this picks up some problems in the JSDoc in the JavaScript files, and validates the code
5+
is consistent with the JSDoc.
6+
7+
The settings here are used by VSCode.
8+
9+
See also tsconfig.js.json and tsconfig.ts.json.
10+
*/
11+
/* Visit https://aka.ms/tsconfig to read more about tsconfig configuration. */
12+
"compilerOptions": {
13+
"lib": ["es2021"],
14+
"module": "node16",
15+
"target": "es2021",
16+
17+
"allowJs": true,
18+
"checkJs": true,
19+
20+
/* Strict by default, but dial it down to reduce churn in our JavaScript code. */
21+
"strict": true,
22+
"noImplicitAny": false,
23+
"strictNullChecks": false,
24+
"useUnknownInCatchVariables": false,
25+
26+
"types": [
27+
"node",
28+
"jest"
29+
],
30+
"noEmit": true, /* just type checking and not emitting transpiled files */
31+
"skipLibCheck": false, /* we want to check our hand crafted definitions */
32+
"forceConsistentCasingInFileNames": true,
33+
"esModuleInterop": true /* common TypeScript config */
34+
},
35+
"include": [
36+
/* JavaScript. Should match includes in tsconfig.js.json. */
37+
"*.js",
38+
"*.mjs",
39+
"lib/**/*.js",
40+
/* TypeScript. Should match includes in tsconfig.ts.json. */
41+
"**/*.ts",
42+
"**/*.mts"
43+
],
44+
"exclude": [
45+
"node_modules"
46+
]
1947
}

tsconfig.ts.json

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
{ /*
2+
Override to include just TypeScript files and use stricter settings than we do with JavaScript.
3+
Used by:
4+
- npm run-script typecheck-ts
5+
- eslint
6+
*/
7+
/* Visit https://aka.ms/tsconfig to read more about tsconfig configuration. */
8+
"extends": "./tsconfig.json",
9+
"compilerOptions": {
10+
/* Full strict is fine for the TypeScript files, so turn back on the checks we turned off for mixed-use. */
11+
"noImplicitAny": true,
12+
"strictNullChecks": true,
13+
"useUnknownInCatchVariables": true,
14+
},
15+
"include": [
16+
/* All TypeScript targets from tsconfig.json include. */
17+
"**/*.ts",
18+
"**/*.mts"
19+
],
20+
}

0 commit comments

Comments
 (0)