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 <aweebit64@gmail.com>
1 parent 744ee3f commit 96c6c25

File tree

12 files changed

+86
-39
lines changed

12 files changed

+86
-39
lines changed

.eslintrc.js

Lines changed: 1 addition & 1 deletion
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

Lines changed: 2 additions & 6 deletions
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

Lines changed: 1 addition & 1 deletion
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

Lines changed: 0 additions & 2 deletions
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

Lines changed: 0 additions & 2 deletions
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

Lines changed: 0 additions & 2 deletions
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

Lines changed: 0 additions & 2 deletions
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

Lines changed: 0 additions & 2 deletions
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

Lines changed: 4 additions & 4 deletions
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

Lines changed: 13 additions & 0 deletions
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+
}

0 commit comments

Comments
 (0)