Skip to content

test(typescript-estree): add test cases for extraFileExtensions #1405

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

Merged
merged 3 commits into from
Jan 6, 2020
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
238 changes: 238 additions & 0 deletions packages/typescript-estree/tests/lib/__snapshots__/parse.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -311,12 +311,32 @@ 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 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.
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 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.
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.
Expand Down Expand Up @@ -1283,6 +1303,224 @@ Object {
}
`;

exports[`parse() isolated parsing should parse .json file - without JSX content - parserOptions.jsx = false 1`] = `
Object {
"ast": Object {
"body": Array [
Object {
"expression": Object {
"loc": Object {
"end": Object {
"column": 10,
"line": 1,
},
"start": Object {
"column": 0,
"line": 1,
},
},
"properties": Array [
Object {
"computed": false,
"key": Object {
"loc": Object {
"end": Object {
"column": 5,
"line": 1,
},
"start": Object {
"column": 2,
"line": 1,
},
},
"range": Array [
2,
5,
],
"raw": "\\"x\\"",
"type": "Literal",
"value": "x",
},
"kind": "init",
"loc": Object {
"end": Object {
"column": 8,
"line": 1,
},
"start": Object {
"column": 2,
"line": 1,
},
},
"method": false,
"range": Array [
2,
8,
],
"shorthand": false,
"type": "Property",
"value": Object {
"loc": Object {
"end": Object {
"column": 8,
"line": 1,
},
"start": Object {
"column": 7,
"line": 1,
},
},
"range": Array [
7,
8,
],
"raw": "1",
"type": "Literal",
"value": 1,
},
},
],
"range": Array [
0,
10,
],
"type": "ObjectExpression",
},
"loc": Object {
"end": Object {
"column": 10,
"line": 1,
},
"start": Object {
"column": 0,
"line": 1,
},
},
"range": Array [
0,
10,
],
"type": "ExpressionStatement",
},
],
"comments": Array [],
"loc": Object {
"end": Object {
"column": 10,
"line": 1,
},
"start": Object {
"column": 0,
"line": 1,
},
},
"range": Array [
0,
10,
],
"sourceType": "script",
"tokens": Array [
Object {
"loc": Object {
"end": Object {
"column": 1,
"line": 1,
},
"start": Object {
"column": 0,
"line": 1,
},
},
"range": Array [
0,
1,
],
"type": "Punctuator",
"value": "{",
},
Object {
"loc": Object {
"end": Object {
"column": 5,
"line": 1,
},
"start": Object {
"column": 2,
"line": 1,
},
},
"range": Array [
2,
5,
],
"type": "String",
"value": "\\"x\\"",
},
Object {
"loc": Object {
"end": Object {
"column": 6,
"line": 1,
},
"start": Object {
"column": 5,
"line": 1,
},
},
"range": Array [
5,
6,
],
"type": "Punctuator",
"value": ":",
},
Object {
"loc": Object {
"end": Object {
"column": 8,
"line": 1,
},
"start": Object {
"column": 7,
"line": 1,
},
},
"range": Array [
7,
8,
],
"type": "Numeric",
"value": "1",
},
Object {
"loc": Object {
"end": Object {
"column": 10,
"line": 1,
},
"start": Object {
"column": 9,
"line": 1,
},
},
"range": Array [
9,
10,
],
"type": "Punctuator",
"value": "}",
},
],
"type": "Program",
},
"services": Object {
"esTreeNodeToTSNodeMap": undefined,
"program": undefined,
"tsNodeToESTreeNodeMap": undefined,
},
}
`;

exports[`parse() isolated parsing should parse .jsx file - with JSX content - parserOptions.jsx = false 1`] = `
Object {
"ast": Object {
Expand Down
61 changes: 53 additions & 8 deletions packages/typescript-estree/tests/lib/parse.ts
Original file line number Diff line number Diff line change
Expand Up @@ -258,12 +258,17 @@ describe('parse()', () => {
jsxSetting,
shouldThrow = false,
}: {
ext: '.js' | '.jsx' | '.ts' | '.tsx' | '.vue';
ext: '.js' | '.jsx' | '.ts' | '.tsx' | '.vue' | '.json';
jsxContent: boolean;
jsxSetting: boolean;
shouldThrow?: boolean;
}): void => {
const code = jsxContent ? 'const x = <div />;' : 'const x = 1';
const code =
ext === '.json'
? '{ "x": 1 }'
: jsxContent
? 'const x = <div />;'
: 'const x = 1';
it(`should parse ${ext} file - ${
jsxContent ? 'with' : 'without'
} JSX content - parserOptions.jsx = ${jsxSetting}`, () => {
Expand Down Expand Up @@ -394,6 +399,11 @@ describe('parse()', () => {
jsxContent: true,
jsxSetting: true,
});
testParse({
ext: '.json',
jsxContent: false,
jsxSetting: false,
});
});

describe('invalid file error messages', () => {
Expand All @@ -406,13 +416,24 @@ describe('parse()', () => {
loc: true,
tsconfigRootDir: PROJECT_DIR,
project: './tsconfig.json',
extraFileExtensions: ['.vue'],
};
const testParse = (filePath: string) => (): void => {
parser.parseAndGenerateServices(code, {
...config,
filePath: join(PROJECT_DIR, filePath),
});
const testParse = (
filePath: string,
extraFileExtensions: string[] = ['.vue'],
) => (): void => {
try {
parser.parseAndGenerateServices(code, {
...config,
extraFileExtensions,
filePath: join(PROJECT_DIR, filePath),
});
} catch (error) {
/**
* Aligns paths between environments, node for windows uses `\`, for linux and mac uses `/`
*/
error.message = error.message.replace(/\\(?!["])/gm, '/');
throw error;
}
};

describe('project includes', () => {
Expand All @@ -431,6 +452,18 @@ describe('parse()', () => {
});
});

describe('"parserOptions.extraFileExtensions" is empty', () => {
it('should not error', () => {
expect(testParse('ts/included.ts', [])).not.toThrow();
});

it('the extension does not match', () => {
expect(
testParse('other/unknownFileType.unknown', []),
).toThrowErrorMatchingSnapshot();
});
});

describe('"parserOptions.extraFileExtensions" is non-empty', () => {
describe('the extension matches', () => {
it('the file is included', () => {
Expand All @@ -442,6 +475,18 @@ describe('parse()', () => {
testParse('other/notIncluded.vue'),
).toThrowErrorMatchingSnapshot();
});

it('duplicate extension', () => {
expect(
testParse('ts/notIncluded.ts', ['.ts']),
).toThrowErrorMatchingSnapshot();
});
});

it('invalid extension', () => {
expect(
testParse('other/unknownFileType.unknown', ['unknown']),
).toThrowErrorMatchingSnapshot();
});

it('the extension does not match', () => {
Expand Down