Refactor type-checking setup (#1969)
Before adoption: this commit came in with the history the project adopted, so no landing records it.
* 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 ---------
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Trailers
- Co-authored-by: Wee Bit <aweebit64@gmail.com>
Diff stats
12 files+86 added−39 removed
- other:+86 added−39 removed
Provenance
- Commit
96c6c25- Tree
96b2de7- Author
John Gee <john@ruru.gen.nz>- Committer
GitHub <noreply@github.com>- Parent
744ee3f
12 files changed+86 added−39 removed
modified:lib/argument.js-2 removed
12343456139 unmodified linesExpand allconst { InvalidArgumentError } = require('./error.js');
// @ts-check
class Argument { /** * Initialize a new command argument with the given name and description. * The default is that the argument is required, and you can explicitly139 unmodified linesExpand allmodified:lib/command.js-2 removed
8 unmodified lines9 const { Help } = require('./help.js');10 const { Option, splitOptionFlags, DualOptions } = require('./option.js');11 const { suggestSimilar } = require('./suggestSimilar');12 13-// @ts-check14-13 class Command extends EventEmitter {14 /**15 * Initialize a new `Command`.16 *2163 unmodified linesmodified:lib/error.js-2 removed
12123439 unmodified lines// @ts-check
/** * CommanderError class * @class */39 unmodified linesmodified:lib/help.js-2 removed
6 unmodified lines7 * @typedef { import("./command.js").Command } Command8 * @typedef { import("./option.js").Option } Option9 */10 11-// @ts-check12-11 // Although this is a class, methods are static in style to allow override using subclass or just functions.12 class Help {13 constructor() {14 this.helpWidth = undefined;448 unmodified linesmodified:lib/option.js-2 removed
1 const { InvalidArgumentError } = require('./error.js');2 3-// @ts-check4-3 class Option {4 /**5 * Initialize a new `Option` with the given `flags` and `description`.6 *323 unmodified linesmodified:.eslintrc.js+1 added-1 removed
14 unmodified lines15 16 const typescriptSettings = {17 files: ['*.ts', '*.mts'],18 parserOptions: {19- project: './tsconfig.json'19+ project: './tsconfig.ts.json'20 },21 plugins: [22 '@typescript-eslint'23 ],44 unmodified linesmodified:index.js+2 added-6 removed
2 unmodified lines3 const { CommanderError, InvalidArgumentError } = require('./lib/error.js');4 const { Help } = require('./lib/help.js');5 const { Option } = require('./lib/option.js');6 7-// @ts-check8-7 /**8 * Expose the root command.9 */10 13-const program = new Command();14-exports = module.exports = program; // default export (deprecated)15-exports.program = program; // more explicit access to global command16-11+exports = module.exports = new Command();12+exports.program = exports; // More explicit access to global command.13 // createArgument, createCommand, and createOption are implicitly available as they are methods on program.14 15 /**16 * Expose classes10 unmodified linesmodified:jest.config.js+1 added-1 removed
1 const config = {2 testEnvironment: 'node',3 collectCoverage: true,4 transform: {5- '^.+\\.tsx?$': 'ts-jest'5+ '^.+\\.tsx?$': ['ts-jest', { tsconfig: 'tsconfig.ts.json' }]6 },7 testPathIgnorePatterns: [8 '/node_modules/'9 ]3 unmodified linesmodified:package.json+4 added-4 removed
20 unmodified lines21 "scripts": {22 "lint": "npm run lint:javascript && npm run lint:typescript",23 "lint:javascript": "eslint index.js esm.mjs \"lib/*.js\" \"tests/**/*.js\"",24 "lint:typescript": "eslint typings/*.ts tests/*.ts",25- "test": "jest && npm run test-typings",25+ "test": "jest && npm run typecheck-ts",26 "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"30 },31 "files": [32 "index.js",33 "lib/*.js",47 unmodified linesadded:tsconfig.js.json+13 added
1+{ /* 2+ Simple override including just JavaScript files.3+ Used by npm run-script typecheck-js4+ */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+}modified:tsconfig.json+45 added-17 removed
1 {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": true17- },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 code5+ 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+ ]47 }added:tsconfig.ts.json+20 added
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-ts5+ - eslint6+ */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+}