Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: ensure tsconfig lookups and parsing work as expected #680

Merged
merged 1 commit into from
Aug 25, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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