Skip to content

Commit

Permalink
Support TypeScript 5 (#712)
Browse files Browse the repository at this point in the history
  • Loading branch information
spence-s committed Apr 5, 2023
1 parent 58225b7 commit a815db3
Show file tree
Hide file tree
Showing 10 changed files with 111 additions and 60 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Expand Up @@ -6,5 +6,7 @@ test/fixtures/project/node_modules/.cache
test/fixtures/typescript/extends-module/node_modules/.cache
!test/fixtures/typescript/extends-tsconfig-bases/node_modules
test/fixtures/typescript/extends-tsconfig-bases/node_modules/.cache
!test/fixtures/typescript/extends-array/node_modules
test/fixtures/typescript/extends-array/node_modules/.cache
.nyc_output
coverage
69 changes: 11 additions & 58 deletions lib/options-manager.js
Expand Up @@ -2,7 +2,6 @@ import {existsSync, promises as fs} from 'node:fs';
import process from 'node:process';
import os from 'node:os';
import path from 'node:path';
import {createRequire} from 'node:module';
import arrify from 'arrify';
import {mergeWith, flow, pick} from 'lodash-es';
import {findUpSync} from 'find-up';
Expand All @@ -11,12 +10,12 @@ import prettier from 'prettier';
import semver from 'semver';
import {cosmiconfig, defaultLoaders} from 'cosmiconfig';
import micromatch from 'micromatch';
import JSON5 from 'json5';
import stringify from 'json-stable-stringify-without-jsonify';
import {Legacy} from '@eslint/eslintrc';
import createEsmUtils from 'esm-utils';
import MurmurHash3 from 'imurmurhash';
import slash from 'slash';
import {getTsconfig} from 'get-tsconfig';
import {
DEFAULT_IGNORES,
DEFAULT_EXTENSION,
Expand Down Expand Up @@ -159,26 +158,20 @@ const handleTSConfig = async options => {
options.tsConfig = {};
options.tsConfigPath = '';

const {project: tsConfigProjectPath, tsconfigRootDir} = options.parserOptions || {};
const {project: tsConfigProjectPath} = options.parserOptions || {};

if (tsConfigProjectPath) {
options.tsConfigPath = path.resolve(options.cwd, tsConfigProjectPath);
options.tsConfig = JSON5.parse(await fs.readFile(options.tsConfigPath));
options.tsConfig = tsConfigResolvePaths(getTsconfig(options.tsConfigPath).config, options.tsConfigPath);
} else {
const tsConfigExplorer = cosmiconfig([], {
searchPlaces: ['tsconfig.json'],
loaders: {'.json': (_, content) => JSON5.parse(content)},
stopDir: tsconfigRootDir,
});
const searchResults = (await tsConfigExplorer.search(options.filePath)) || {};
options.tsConfigPath = searchResults.filepath;
options.tsConfig = searchResults.config;
}

if (options.tsConfig) {
// If the tsconfig extends from another file, we need to ensure that the file is covered by the tsconfig
// or not. The basefile could have includes/excludes/files properties that should be applied to the final tsconfig representation.
options.tsConfig = await recursiveBuildTsConfig(options.tsConfig, options.tsConfigPath);
const {config: tsConfig, path: filepath} = getTsconfig(options.filePath) || {};
options.tsConfigPath = filepath;
options.tsConfig = tsConfig;
if (options.tsConfigPath) {
options.tsConfig = tsConfigResolvePaths(tsConfig, options.tsConfigPath);
} else {
delete options.tsConfig;
}
}

let hasMatch;
Expand Down Expand Up @@ -637,46 +630,6 @@ const getOptionGroups = async (files, options) => {
return optionGroups;
};

async function recursiveBuildTsConfig(tsConfig, tsConfigPath) {
tsConfig = tsConfigResolvePaths(tsConfig, tsConfigPath);

if (!tsConfig.extends || (typeof tsConfig.extends === 'string' && tsConfig.extends.includes('node_modules'))) {
return tsConfig;
}

// If any of the following are missing, then we need to look up the base config as it could apply
const require = createRequire(tsConfigPath);

let basePath;
try {
basePath = require.resolve(tsConfig.extends);
} catch (error) {
// Tsconfig resolution is odd, It allows behavior that is not exactly like node resolution
// therefore we attempt to smooth this out here with this hack
try {
basePath = require.resolve(path.join(tsConfig.extends, 'tsconfig.json'));
} catch {
// Throw the orginal resolution error to let the user know their extends block is invalid
throw error;
}
}

const baseTsConfig = JSON5.parse(await fs.readFile(basePath));

delete tsConfig.extends;

tsConfig = {
compilerOptions: {
...baseTsConfig.compilerOptions,
...tsConfig.compilerOptions,
},
...baseTsConfig,
...tsConfig,
};

return recursiveBuildTsConfig(tsConfig, basePath);
}

// Convert all include, files, and exclude to absolute paths
// and or globs. This works because ts only allows simple glob subset
const tsConfigResolvePaths = (tsConfig, tsConfigPath) => {
Expand Down
4 changes: 2 additions & 2 deletions package.json
Expand Up @@ -81,10 +81,10 @@
"find-cache-dir": "^4.0.0",
"find-up": "^6.3.0",
"get-stdin": "^9.0.0",
"get-tsconfig": "^4.5.0",
"globby": "^13.1.2",
"imurmurhash": "^0.1.4",
"json-stable-stringify-without-jsonify": "^1.0.1",
"json5": "^2.2.1",
"lodash-es": "^4.17.21",
"meow": "^11.0.0",
"micromatch": "^4.0.5",
Expand All @@ -93,7 +93,7 @@
"semver": "^7.3.8",
"slash": "^5.0.0",
"to-absolute-glob": "^2.0.2",
"typescript": "^4.9.3"
"typescript": "^5.0.3"
},
"devDependencies": {
"ava": "^5.1.0",
Expand Down

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

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

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

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

3 changes: 3 additions & 0 deletions test/fixtures/typescript/extends-array/package.json
@@ -0,0 +1,3 @@
{
"xo": {}
}
6 changes: 6 additions & 0 deletions test/fixtures/typescript/extends-array/tsconfig.json
@@ -0,0 +1,6 @@
{
"extends": [
"@sindresorhus/tsconfig",
"@tsconfig/node16"
],
}
9 changes: 9 additions & 0 deletions test/options-manager.js
Expand Up @@ -691,6 +691,15 @@ test('mergeWithFileConfig: tsconfig can properly extend tsconfig base node_modul
t.is(options.tsConfigPath, expectedConfigPath);
});

test('mergeWithFileConfig: tsconfig can properly resolve extends arrays introduced in ts 5', async t => {
const cwd = path.resolve('fixtures', 'typescript', 'extends-array');
const expectedConfigPath = path.join(cwd, 'tsconfig.json');
const filePath = path.resolve(cwd, 'does-not-matter.ts');
await t.notThrowsAsync(manager.mergeWithFileConfig({cwd, filePath}));
const {options} = await manager.mergeWithFileConfig({cwd, filePath});
t.is(options.tsConfigPath, expectedConfigPath);
});

test('applyOverrides', t => {
t.deepEqual(
manager.applyOverrides(
Expand Down

0 comments on commit a815db3

Please sign in to comment.