Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
fix(typescript-estree): resolve path relative to tsconfigRootDir (#1439)
  • Loading branch information
armano2 authored and bradzacher committed Jan 13, 2020
1 parent 3c5659b commit c709056
Show file tree
Hide file tree
Showing 9 changed files with 49 additions and 25 deletions.
Expand Up @@ -128,7 +128,7 @@ function Test(props: {
return <div key={props.id!} />;
}
`,
filename: path.join(rootDir, 'react.tsx'),
filename: 'react.tsx',
},
],

Expand Down Expand Up @@ -366,7 +366,7 @@ function Test(props: {
line: 7,
},
],
filename: path.join(rootDir, 'react.tsx'),
filename: 'react.tsx',
},
],
});
2 changes: 1 addition & 1 deletion packages/parser/tests/lib/parser.ts
Expand Up @@ -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,
Expand Down
6 changes: 4 additions & 2 deletions packages/parser/tests/lib/services.ts
Expand Up @@ -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 {
Expand All @@ -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'),
Expand Down
Expand Up @@ -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,
)}.`,
];
Expand Down
2 changes: 1 addition & 1 deletion packages/typescript-estree/src/parser.ts
Expand Up @@ -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
Expand Down Expand Up @@ -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) {
Expand Down
18 changes: 9 additions & 9 deletions packages/typescript-estree/tests/lib/__snapshots__/parse.ts.snap
Expand Up @@ -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."
`;

Expand Down
2 changes: 1 addition & 1 deletion packages/typescript-estree/tests/lib/parse.ts
Expand Up @@ -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,
Expand Down
26 changes: 23 additions & 3 deletions packages/typescript-estree/tests/lib/persistentParse.ts
Expand Up @@ -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`),
Expand Down Expand Up @@ -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();
Expand All @@ -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', () => {
Expand Down
12 changes: 7 additions & 5 deletions packages/typescript-estree/tests/lib/semanticInfo.ts
Expand Up @@ -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 {
Expand All @@ -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(
Expand All @@ -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,
Expand All @@ -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,
Expand All @@ -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,
Expand Down

0 comments on commit c709056

Please sign in to comment.