From c7090563019dde6c140961e89eee57c44ca91c9f Mon Sep 17 00:00:00 2001 From: Armano Date: Mon, 13 Jan 2020 04:53:45 +0100 Subject: [PATCH] fix(typescript-estree): resolve path relative to tsconfigRootDir (#1439) --- .../no-unnecessary-type-assertion.test.ts | 4 +-- packages/parser/tests/lib/parser.ts | 2 +- packages/parser/tests/lib/services.ts | 6 +++-- .../create-program/createProjectProgram.ts | 2 +- packages/typescript-estree/src/parser.ts | 2 +- .../tests/lib/__snapshots__/parse.ts.snap | 18 ++++++------- packages/typescript-estree/tests/lib/parse.ts | 2 +- .../tests/lib/persistentParse.ts | 26 ++++++++++++++++--- .../tests/lib/semanticInfo.ts | 12 +++++---- 9 files changed, 49 insertions(+), 25 deletions(-) diff --git a/packages/eslint-plugin/tests/rules/no-unnecessary-type-assertion.test.ts b/packages/eslint-plugin/tests/rules/no-unnecessary-type-assertion.test.ts index 595d1961932..3d5c958c3cc 100644 --- a/packages/eslint-plugin/tests/rules/no-unnecessary-type-assertion.test.ts +++ b/packages/eslint-plugin/tests/rules/no-unnecessary-type-assertion.test.ts @@ -128,7 +128,7 @@ function Test(props: { return
; } `, - filename: path.join(rootDir, 'react.tsx'), + filename: 'react.tsx', }, ], @@ -366,7 +366,7 @@ function Test(props: { line: 7, }, ], - filename: path.join(rootDir, 'react.tsx'), + filename: 'react.tsx', }, ], }); diff --git a/packages/parser/tests/lib/parser.ts b/packages/parser/tests/lib/parser.ts index 4f5136b036b..56ce08b6786 100644 --- a/packages/parser/tests/lib/parser.ts +++ b/packages/parser/tests/lib/parser.ts @@ -50,7 +50,7 @@ describe('parser', () => { jsx: false, }, // ts-estree specific - filePath: 'tests/fixtures/services/isolated-file.src.ts', + filePath: 'isolated-file.src.ts', project: 'tsconfig.json', useJSXTextNode: false, errorOnUnknownASTType: false, diff --git a/packages/parser/tests/lib/services.ts b/packages/parser/tests/lib/services.ts index 40dabd2e6df..37ddf0fadcf 100644 --- a/packages/parser/tests/lib/services.ts +++ b/packages/parser/tests/lib/services.ts @@ -12,7 +12,9 @@ import { //------------------------------------------------------------------------------ const FIXTURES_DIR = './tests/fixtures/services'; -const testFiles = glob.sync(`${FIXTURES_DIR}/**/*.src.ts`); +const testFiles = glob.sync(`**/*.src.ts`, { + cwd: FIXTURES_DIR, +}); function createConfig(filename: string): object { return { @@ -29,7 +31,7 @@ function createConfig(filename: string): object { describe('services', () => { testFiles.forEach(filename => { - const code = fs.readFileSync(filename, 'utf8'); + const code = fs.readFileSync(path.join(FIXTURES_DIR, filename), 'utf8'); const config = createConfig(filename); it( formatSnapshotName(filename, FIXTURES_DIR, '.ts'), diff --git a/packages/typescript-estree/src/create-program/createProjectProgram.ts b/packages/typescript-estree/src/create-program/createProjectProgram.ts index 32f81a40f66..b81409e647e 100644 --- a/packages/typescript-estree/src/create-program/createProjectProgram.ts +++ b/packages/typescript-estree/src/create-program/createProjectProgram.ts @@ -35,7 +35,7 @@ function createProjectProgram( const errorLines = [ '"parserOptions.project" has been set for @typescript-eslint/parser.', `The file does not match your project config: ${path.relative( - process.cwd(), + extra.tsconfigRootDir || process.cwd(), extra.filePath, )}.`, ]; diff --git a/packages/typescript-estree/src/parser.ts b/packages/typescript-estree/src/parser.ts index 6a9e0a1c6b5..0ecd1de3312 100644 --- a/packages/typescript-estree/src/parser.ts +++ b/packages/typescript-estree/src/parser.ts @@ -146,7 +146,6 @@ function applyParserOptionsToExtra(options: TSESTreeOptions): void { } else { extra.filePath = getFileName(extra); } - extra.filePath = ensureAbsolutePath(extra.filePath, extra); /** * The JSX AST changed the node type for string literals @@ -191,6 +190,7 @@ function applyParserOptionsToExtra(options: TSESTreeOptions): void { if (typeof options.tsconfigRootDir === 'string') { extra.tsconfigRootDir = options.tsconfigRootDir; } + extra.filePath = ensureAbsolutePath(extra.filePath, extra); // Transform glob patterns into paths if (extra.projects) { diff --git a/packages/typescript-estree/tests/lib/__snapshots__/parse.ts.snap b/packages/typescript-estree/tests/lib/__snapshots__/parse.ts.snap index 616c95d748e..4b6d7f2c783 100644 --- a/packages/typescript-estree/tests/lib/__snapshots__/parse.ts.snap +++ b/packages/typescript-estree/tests/lib/__snapshots__/parse.ts.snap @@ -313,57 +313,57 @@ Object { exports[`parse() invalid file error messages "parserOptions.extraFileExtensions" is empty the extension does not match 1`] = ` "\\"parserOptions.project\\" has been set for @typescript-eslint/parser. -The file does not match your project config: tests/fixtures/invalidFileErrors/other/unknownFileType.unknown. +The file does not match your project config: other/unknownFileType.unknown. The extension for the file (.unknown) is non-standard. You should add \\"parserOptions.extraFileExtensions\\" to your config." `; exports[`parse() invalid file error messages "parserOptions.extraFileExtensions" is non-empty invalid extension 1`] = ` "\\"parserOptions.project\\" has been set for @typescript-eslint/parser. -The file does not match your project config: tests/fixtures/invalidFileErrors/other/unknownFileType.unknown. +The file does not match your project config: other/unknownFileType.unknown. Found unexpected extension \\"unknown\\" specified with the \\"extraFileExtensions\\" option. Did you mean \\".unknown\\"? The extension for the file (.unknown) is non-standard. It should be added to your existing \\"parserOptions.extraFileExtensions\\"." `; exports[`parse() invalid file error messages "parserOptions.extraFileExtensions" is non-empty the extension does not match 1`] = ` "\\"parserOptions.project\\" has been set for @typescript-eslint/parser. -The file does not match your project config: tests/fixtures/invalidFileErrors/other/unknownFileType.unknown. +The file does not match your project config: other/unknownFileType.unknown. The extension for the file (.unknown) is non-standard. It should be added to your existing \\"parserOptions.extraFileExtensions\\"." `; exports[`parse() invalid file error messages "parserOptions.extraFileExtensions" is non-empty the extension matches duplicate extension 1`] = ` "\\"parserOptions.project\\" has been set for @typescript-eslint/parser. -The file does not match your project config: tests/fixtures/invalidFileErrors/ts/notIncluded.ts. +The file does not match your project config: ts/notIncluded.ts. You unnecessarily included the extension \\".ts\\" with the \\"extraFileExtensions\\" option. This extension is already handled by the parser by default. The file must be included in at least one of the projects provided." `; exports[`parse() invalid file error messages "parserOptions.extraFileExtensions" is non-empty the extension matches the file isn't included 1`] = ` "\\"parserOptions.project\\" has been set for @typescript-eslint/parser. -The file does not match your project config: tests/fixtures/invalidFileErrors/other/notIncluded.vue. +The file does not match your project config: other/notIncluded.vue. The file must be included in at least one of the projects provided." `; exports[`parse() invalid file error messages project includes errors for not included files 1`] = ` "\\"parserOptions.project\\" has been set for @typescript-eslint/parser. -The file does not match your project config: tests/fixtures/invalidFileErrors/ts/notIncluded.ts. +The file does not match your project config: ts/notIncluded.ts. The file must be included in at least one of the projects provided." `; exports[`parse() invalid file error messages project includes errors for not included files 2`] = ` "\\"parserOptions.project\\" has been set for @typescript-eslint/parser. -The file does not match your project config: tests/fixtures/invalidFileErrors/ts/notIncluded.tsx. +The file does not match your project config: ts/notIncluded.tsx. The file must be included in at least one of the projects provided." `; exports[`parse() invalid file error messages project includes errors for not included files 3`] = ` "\\"parserOptions.project\\" has been set for @typescript-eslint/parser. -The file does not match your project config: tests/fixtures/invalidFileErrors/js/notIncluded.js. +The file does not match your project config: js/notIncluded.js. The file must be included in at least one of the projects provided." `; exports[`parse() invalid file error messages project includes errors for not included files 4`] = ` "\\"parserOptions.project\\" has been set for @typescript-eslint/parser. -The file does not match your project config: tests/fixtures/invalidFileErrors/js/notIncluded.jsx. +The file does not match your project config: js/notIncluded.jsx. The file must be included in at least one of the projects provided." `; diff --git a/packages/typescript-estree/tests/lib/parse.ts b/packages/typescript-estree/tests/lib/parse.ts index 6cba9ba867b..986bf02573a 100644 --- a/packages/typescript-estree/tests/lib/parse.ts +++ b/packages/typescript-estree/tests/lib/parse.ts @@ -144,7 +144,7 @@ describe('parse()', () => { tokens: true, range: true, loc: true, - filePath: 'tests/fixtures/simpleProject/file.ts', + filePath: 'file.ts', }; const projectConfig: TSESTreeOptions = { ...baseConfig, diff --git a/packages/typescript-estree/tests/lib/persistentParse.ts b/packages/typescript-estree/tests/lib/persistentParse.ts index fd9cbdc93bb..98f5b1bcd0d 100644 --- a/packages/typescript-estree/tests/lib/persistentParse.ts +++ b/packages/typescript-estree/tests/lib/persistentParse.ts @@ -62,10 +62,11 @@ function parseFile( filename: keyof typeof CONTENTS, tmpDir: string, relative?: boolean, + ignoreTsconfigRootDir?: boolean, ): void { parseAndGenerateServices(CONTENTS[filename], { project: './tsconfig.json', - tsconfigRootDir: tmpDir, + tsconfigRootDir: ignoreTsconfigRootDir ? undefined : tmpDir, filePath: relative ? path.join('src', `${filename}.ts`) : path.join(tmpDir, 'src', `${filename}.ts`), @@ -178,7 +179,6 @@ function baseTests( it('should work with relative paths', () => { const PROJECT_DIR = setup(tsConfigIncludeAll, false); - process.chdir(PROJECT_DIR); // parse once to: assert the config as correct, and to make sure the program is setup expect(() => parseFile('foo', PROJECT_DIR, true)).not.toThrow(); @@ -189,13 +189,33 @@ function baseTests( writeFile(PROJECT_DIR, 'bar'); // make sure that file is correctly created - expect(existsSync('bar')).toEqual(true); expect(existsSync('bar', PROJECT_DIR)).toEqual(true); // both files should parse fine now expect(() => parseFile('foo', PROJECT_DIR, true)).not.toThrow(); expect(() => parseFile('bar', PROJECT_DIR, true)).not.toThrow(); }); + + it('should work with relative paths without tsconfig root', () => { + const PROJECT_DIR = setup(tsConfigIncludeAll, false); + process.chdir(PROJECT_DIR); + + // parse once to: assert the config as correct, and to make sure the program is setup + expect(() => parseFile('foo', PROJECT_DIR, true, true)).not.toThrow(); + // bar should throw because it doesn't exist yet + expect(() => parseFile('bar', PROJECT_DIR, true, true)).toThrow(); + + // write a new file and attempt to parse it + writeFile(PROJECT_DIR, 'bar'); + + // make sure that file is correctly created + expect(existsSync('bar')).toEqual(true); + expect(existsSync('bar', PROJECT_DIR)).toEqual(true); + + // both files should parse fine now + expect(() => parseFile('foo', PROJECT_DIR, true, true)).not.toThrow(); + expect(() => parseFile('bar', PROJECT_DIR, true, true)).not.toThrow(); + }); } describe('persistent parse', () => { diff --git a/packages/typescript-estree/tests/lib/semanticInfo.ts b/packages/typescript-estree/tests/lib/semanticInfo.ts index 3a99890c401..5f979fb94ad 100644 --- a/packages/typescript-estree/tests/lib/semanticInfo.ts +++ b/packages/typescript-estree/tests/lib/semanticInfo.ts @@ -16,7 +16,9 @@ import { import { TSESTree } from '../../src/ts-estree'; const FIXTURES_DIR = './tests/fixtures/semanticInfo'; -const testFiles = glob.sync(`${FIXTURES_DIR}/**/*.src.ts`); +const testFiles = glob.sync(`**/*.src.ts`, { + cwd: FIXTURES_DIR, +}); function createOptions(fileName: string): TSESTreeOptions & { cwd?: string } { return { @@ -40,7 +42,7 @@ beforeEach(() => clearCaches()); describe('semanticInfo', () => { // test all AST snapshots testFiles.forEach(filename => { - const code = readFileSync(filename, 'utf8'); + const code = readFileSync(join(FIXTURES_DIR, filename), 'utf8'); it( formatSnapshotName(filename, FIXTURES_DIR, extname(filename)), createSnapshotTestBlock( @@ -53,7 +55,7 @@ describe('semanticInfo', () => { it(`should cache the created ts.program`, () => { const filename = testFiles[0]; - const code = readFileSync(filename, 'utf8'); + const code = readFileSync(join(FIXTURES_DIR, filename), 'utf8'); const options = createOptions(filename); const optionsProjectString = { ...options, @@ -68,7 +70,7 @@ describe('semanticInfo', () => { it(`should handle "project": "./tsconfig.json" and "project": ["./tsconfig.json"] the same`, () => { const filename = testFiles[0]; - const code = readFileSync(filename, 'utf8'); + const code = readFileSync(join(FIXTURES_DIR, filename), 'utf8'); const options = createOptions(filename); const optionsProjectString = { ...options, @@ -85,7 +87,7 @@ describe('semanticInfo', () => { it(`should resolve absolute and relative tsconfig paths the same`, () => { const filename = testFiles[0]; - const code = readFileSync(filename, 'utf8'); + const code = readFileSync(join(FIXTURES_DIR, filename), 'utf8'); const options = createOptions(filename); const optionsAbsolutePath = { ...options,