Skip to content

Commit c709056

Browse files
armano2bradzacher
authored andcommitted
fix(typescript-estree): resolve path relative to tsconfigRootDir (#1439)
1 parent 3c5659b commit c709056

9 files changed

Lines changed: 49 additions & 25 deletions

File tree

packages/eslint-plugin/tests/rules/no-unnecessary-type-assertion.test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ function Test(props: {
128128
return <div key={props.id!} />;
129129
}
130130
`,
131-
filename: path.join(rootDir, 'react.tsx'),
131+
filename: 'react.tsx',
132132
},
133133
],
134134

@@ -366,7 +366,7 @@ function Test(props: {
366366
line: 7,
367367
},
368368
],
369-
filename: path.join(rootDir, 'react.tsx'),
369+
filename: 'react.tsx',
370370
},
371371
],
372372
});

packages/parser/tests/lib/parser.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ describe('parser', () => {
5050
jsx: false,
5151
},
5252
// ts-estree specific
53-
filePath: 'tests/fixtures/services/isolated-file.src.ts',
53+
filePath: 'isolated-file.src.ts',
5454
project: 'tsconfig.json',
5555
useJSXTextNode: false,
5656
errorOnUnknownASTType: false,

packages/parser/tests/lib/services.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,9 @@ import {
1212
//------------------------------------------------------------------------------
1313

1414
const FIXTURES_DIR = './tests/fixtures/services';
15-
const testFiles = glob.sync(`${FIXTURES_DIR}/**/*.src.ts`);
15+
const testFiles = glob.sync(`**/*.src.ts`, {
16+
cwd: FIXTURES_DIR,
17+
});
1618

1719
function createConfig(filename: string): object {
1820
return {
@@ -29,7 +31,7 @@ function createConfig(filename: string): object {
2931

3032
describe('services', () => {
3133
testFiles.forEach(filename => {
32-
const code = fs.readFileSync(filename, 'utf8');
34+
const code = fs.readFileSync(path.join(FIXTURES_DIR, filename), 'utf8');
3335
const config = createConfig(filename);
3436
it(
3537
formatSnapshotName(filename, FIXTURES_DIR, '.ts'),

packages/typescript-estree/src/create-program/createProjectProgram.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ function createProjectProgram(
3535
const errorLines = [
3636
'"parserOptions.project" has been set for @typescript-eslint/parser.',
3737
`The file does not match your project config: ${path.relative(
38-
process.cwd(),
38+
extra.tsconfigRootDir || process.cwd(),
3939
extra.filePath,
4040
)}.`,
4141
];

packages/typescript-estree/src/parser.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,6 @@ function applyParserOptionsToExtra(options: TSESTreeOptions): void {
146146
} else {
147147
extra.filePath = getFileName(extra);
148148
}
149-
extra.filePath = ensureAbsolutePath(extra.filePath, extra);
150149

151150
/**
152151
* The JSX AST changed the node type for string literals
@@ -191,6 +190,7 @@ function applyParserOptionsToExtra(options: TSESTreeOptions): void {
191190
if (typeof options.tsconfigRootDir === 'string') {
192191
extra.tsconfigRootDir = options.tsconfigRootDir;
193192
}
193+
extra.filePath = ensureAbsolutePath(extra.filePath, extra);
194194

195195
// Transform glob patterns into paths
196196
if (extra.projects) {

packages/typescript-estree/tests/lib/__snapshots__/parse.ts.snap

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -313,57 +313,57 @@ Object {
313313

314314
exports[`parse() invalid file error messages "parserOptions.extraFileExtensions" is empty the extension does not match 1`] = `
315315
"\\"parserOptions.project\\" has been set for @typescript-eslint/parser.
316-
The file does not match your project config: tests/fixtures/invalidFileErrors/other/unknownFileType.unknown.
316+
The file does not match your project config: other/unknownFileType.unknown.
317317
The extension for the file (.unknown) is non-standard. You should add \\"parserOptions.extraFileExtensions\\" to your config."
318318
`;
319319

320320
exports[`parse() invalid file error messages "parserOptions.extraFileExtensions" is non-empty invalid extension 1`] = `
321321
"\\"parserOptions.project\\" has been set for @typescript-eslint/parser.
322-
The file does not match your project config: tests/fixtures/invalidFileErrors/other/unknownFileType.unknown.
322+
The file does not match your project config: other/unknownFileType.unknown.
323323
Found unexpected extension \\"unknown\\" specified with the \\"extraFileExtensions\\" option. Did you mean \\".unknown\\"?
324324
The extension for the file (.unknown) is non-standard. It should be added to your existing \\"parserOptions.extraFileExtensions\\"."
325325
`;
326326

327327
exports[`parse() invalid file error messages "parserOptions.extraFileExtensions" is non-empty the extension does not match 1`] = `
328328
"\\"parserOptions.project\\" has been set for @typescript-eslint/parser.
329-
The file does not match your project config: tests/fixtures/invalidFileErrors/other/unknownFileType.unknown.
329+
The file does not match your project config: other/unknownFileType.unknown.
330330
The extension for the file (.unknown) is non-standard. It should be added to your existing \\"parserOptions.extraFileExtensions\\"."
331331
`;
332332

333333
exports[`parse() invalid file error messages "parserOptions.extraFileExtensions" is non-empty the extension matches duplicate extension 1`] = `
334334
"\\"parserOptions.project\\" has been set for @typescript-eslint/parser.
335-
The file does not match your project config: tests/fixtures/invalidFileErrors/ts/notIncluded.ts.
335+
The file does not match your project config: ts/notIncluded.ts.
336336
You unnecessarily included the extension \\".ts\\" with the \\"extraFileExtensions\\" option. This extension is already handled by the parser by default.
337337
The file must be included in at least one of the projects provided."
338338
`;
339339

340340
exports[`parse() invalid file error messages "parserOptions.extraFileExtensions" is non-empty the extension matches the file isn't included 1`] = `
341341
"\\"parserOptions.project\\" has been set for @typescript-eslint/parser.
342-
The file does not match your project config: tests/fixtures/invalidFileErrors/other/notIncluded.vue.
342+
The file does not match your project config: other/notIncluded.vue.
343343
The file must be included in at least one of the projects provided."
344344
`;
345345

346346
exports[`parse() invalid file error messages project includes errors for not included files 1`] = `
347347
"\\"parserOptions.project\\" has been set for @typescript-eslint/parser.
348-
The file does not match your project config: tests/fixtures/invalidFileErrors/ts/notIncluded.ts.
348+
The file does not match your project config: ts/notIncluded.ts.
349349
The file must be included in at least one of the projects provided."
350350
`;
351351

352352
exports[`parse() invalid file error messages project includes errors for not included files 2`] = `
353353
"\\"parserOptions.project\\" has been set for @typescript-eslint/parser.
354-
The file does not match your project config: tests/fixtures/invalidFileErrors/ts/notIncluded.tsx.
354+
The file does not match your project config: ts/notIncluded.tsx.
355355
The file must be included in at least one of the projects provided."
356356
`;
357357

358358
exports[`parse() invalid file error messages project includes errors for not included files 3`] = `
359359
"\\"parserOptions.project\\" has been set for @typescript-eslint/parser.
360-
The file does not match your project config: tests/fixtures/invalidFileErrors/js/notIncluded.js.
360+
The file does not match your project config: js/notIncluded.js.
361361
The file must be included in at least one of the projects provided."
362362
`;
363363

364364
exports[`parse() invalid file error messages project includes errors for not included files 4`] = `
365365
"\\"parserOptions.project\\" has been set for @typescript-eslint/parser.
366-
The file does not match your project config: tests/fixtures/invalidFileErrors/js/notIncluded.jsx.
366+
The file does not match your project config: js/notIncluded.jsx.
367367
The file must be included in at least one of the projects provided."
368368
`;
369369

packages/typescript-estree/tests/lib/parse.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,7 @@ describe('parse()', () => {
144144
tokens: true,
145145
range: true,
146146
loc: true,
147-
filePath: 'tests/fixtures/simpleProject/file.ts',
147+
filePath: 'file.ts',
148148
};
149149
const projectConfig: TSESTreeOptions = {
150150
...baseConfig,

packages/typescript-estree/tests/lib/persistentParse.ts

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -62,10 +62,11 @@ function parseFile(
6262
filename: keyof typeof CONTENTS,
6363
tmpDir: string,
6464
relative?: boolean,
65+
ignoreTsconfigRootDir?: boolean,
6566
): void {
6667
parseAndGenerateServices(CONTENTS[filename], {
6768
project: './tsconfig.json',
68-
tsconfigRootDir: tmpDir,
69+
tsconfigRootDir: ignoreTsconfigRootDir ? undefined : tmpDir,
6970
filePath: relative
7071
? path.join('src', `${filename}.ts`)
7172
: path.join(tmpDir, 'src', `${filename}.ts`),
@@ -178,7 +179,6 @@ function baseTests(
178179

179180
it('should work with relative paths', () => {
180181
const PROJECT_DIR = setup(tsConfigIncludeAll, false);
181-
process.chdir(PROJECT_DIR);
182182

183183
// parse once to: assert the config as correct, and to make sure the program is setup
184184
expect(() => parseFile('foo', PROJECT_DIR, true)).not.toThrow();
@@ -189,13 +189,33 @@ function baseTests(
189189
writeFile(PROJECT_DIR, 'bar');
190190

191191
// make sure that file is correctly created
192-
expect(existsSync('bar')).toEqual(true);
193192
expect(existsSync('bar', PROJECT_DIR)).toEqual(true);
194193

195194
// both files should parse fine now
196195
expect(() => parseFile('foo', PROJECT_DIR, true)).not.toThrow();
197196
expect(() => parseFile('bar', PROJECT_DIR, true)).not.toThrow();
198197
});
198+
199+
it('should work with relative paths without tsconfig root', () => {
200+
const PROJECT_DIR = setup(tsConfigIncludeAll, false);
201+
process.chdir(PROJECT_DIR);
202+
203+
// parse once to: assert the config as correct, and to make sure the program is setup
204+
expect(() => parseFile('foo', PROJECT_DIR, true, true)).not.toThrow();
205+
// bar should throw because it doesn't exist yet
206+
expect(() => parseFile('bar', PROJECT_DIR, true, true)).toThrow();
207+
208+
// write a new file and attempt to parse it
209+
writeFile(PROJECT_DIR, 'bar');
210+
211+
// make sure that file is correctly created
212+
expect(existsSync('bar')).toEqual(true);
213+
expect(existsSync('bar', PROJECT_DIR)).toEqual(true);
214+
215+
// both files should parse fine now
216+
expect(() => parseFile('foo', PROJECT_DIR, true, true)).not.toThrow();
217+
expect(() => parseFile('bar', PROJECT_DIR, true, true)).not.toThrow();
218+
});
199219
}
200220

201221
describe('persistent parse', () => {

packages/typescript-estree/tests/lib/semanticInfo.ts

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,9 @@ import {
1616
import { TSESTree } from '../../src/ts-estree';
1717

1818
const FIXTURES_DIR = './tests/fixtures/semanticInfo';
19-
const testFiles = glob.sync(`${FIXTURES_DIR}/**/*.src.ts`);
19+
const testFiles = glob.sync(`**/*.src.ts`, {
20+
cwd: FIXTURES_DIR,
21+
});
2022

2123
function createOptions(fileName: string): TSESTreeOptions & { cwd?: string } {
2224
return {
@@ -40,7 +42,7 @@ beforeEach(() => clearCaches());
4042
describe('semanticInfo', () => {
4143
// test all AST snapshots
4244
testFiles.forEach(filename => {
43-
const code = readFileSync(filename, 'utf8');
45+
const code = readFileSync(join(FIXTURES_DIR, filename), 'utf8');
4446
it(
4547
formatSnapshotName(filename, FIXTURES_DIR, extname(filename)),
4648
createSnapshotTestBlock(
@@ -53,7 +55,7 @@ describe('semanticInfo', () => {
5355

5456
it(`should cache the created ts.program`, () => {
5557
const filename = testFiles[0];
56-
const code = readFileSync(filename, 'utf8');
58+
const code = readFileSync(join(FIXTURES_DIR, filename), 'utf8');
5759
const options = createOptions(filename);
5860
const optionsProjectString = {
5961
...options,
@@ -68,7 +70,7 @@ describe('semanticInfo', () => {
6870

6971
it(`should handle "project": "./tsconfig.json" and "project": ["./tsconfig.json"] the same`, () => {
7072
const filename = testFiles[0];
71-
const code = readFileSync(filename, 'utf8');
73+
const code = readFileSync(join(FIXTURES_DIR, filename), 'utf8');
7274
const options = createOptions(filename);
7375
const optionsProjectString = {
7476
...options,
@@ -85,7 +87,7 @@ describe('semanticInfo', () => {
8587

8688
it(`should resolve absolute and relative tsconfig paths the same`, () => {
8789
const filename = testFiles[0];
88-
const code = readFileSync(filename, 'utf8');
90+
const code = readFileSync(join(FIXTURES_DIR, filename), 'utf8');
8991
const options = createOptions(filename);
9092
const optionsAbsolutePath = {
9193
...options,

0 commit comments

Comments
 (0)