Skip to content

Commit

Permalink
test(typescript-estree): add test cases for extraFileExtensions (#1405)
Browse files Browse the repository at this point in the history
  • Loading branch information
armano2 authored and bradzacher committed Jan 6, 2020
1 parent 01caad2 commit a2c53ae
Show file tree
Hide file tree
Showing 2 changed files with 291 additions and 8 deletions.
238 changes: 238 additions & 0 deletions packages/typescript-estree/tests/lib/__snapshots__/parse.ts.snap
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
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

0 comments on commit a2c53ae

Please sign in to comment.