Skip to content

Commit

Permalink
Ensure tsconfig lookups work as expected (#680)
Browse files Browse the repository at this point in the history
  • Loading branch information
spence-s authored Aug 25, 2022
1 parent a152e1b commit de5f878
Show file tree
Hide file tree
Showing 7 changed files with 83 additions and 5 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,7 @@ node_modules
yarn.lock
!test/fixtures/project/node_modules
test/fixtures/project/node_modules/.cache
!test/fixtures/typescript/extends-module/node_modules
test/fixtures/typescript/extends-module/node_modules/.cache
.nyc_output
coverage
10 changes: 5 additions & 5 deletions lib/options-manager.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ 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 Down Expand Up @@ -643,11 +644,10 @@ async function recursiveBuildTsConfig(tsConfig, tsConfigPath) {
}

// If any of the following are missing, then we need to look up the base config as it could apply
const basePath = path.isAbsolute(tsConfig.extends)
? tsConfig.extends
: path.resolve(path.dirname(tsConfigPath), tsConfig.extends);

const baseTsConfig = await readJson(basePath);
// Use node resolution
const require = createRequire(tsConfigPath);
const basePath = require.resolve(tsConfig.extends);
const baseTsConfig = JSON5.parse(await fs.readFile(basePath));

delete tsConfig.extends;

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.

3 changes: 3 additions & 0 deletions test/fixtures/typescript/extends-module/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"xo": {}
}
3 changes: 3 additions & 0 deletions test/fixtures/typescript/extends-module/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"extends": "@sindresorhus/tsconfig"
}
9 changes: 9 additions & 0 deletions test/options-manager.js
Original file line number Diff line number Diff line change
Expand Up @@ -671,6 +671,15 @@ test('mergeWithFileConfig: creates temp tsconfig if none present', async t => {
t.deepEqual(options.tsConfig, TSCONFIG_DEFAULTS);
});

test('mergeWithFileConfig: tsconfig can properly extend configs in node_modules', async t => {
const cwd = path.resolve('fixtures', 'typescript', 'extends-module');
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 de5f878

Please sign in to comment.