8000 chore(typescript-estree): clarify default file extensions (#963) · lionelB/typescript-eslint@3f2f3e8 · GitHub
[go: up one dir, main page]

Skip to content

Commit 3f2f3e8

Browse files
dimitropoulosarmano2
authored andcommitted
chore(typescript-estree): clarify default file extensions (typescript-eslint#963)
Co-authored-by: Armano <armano2@users.noreply.github.com>
1 parent 01c939f commit 3f2f3e8

File tree

3 files changed

+25
-6
lines changed

3 files changed

+25
-6
lines changed

packages/parser/README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,8 @@ This option allows you to provide the root directory for relative tsconfig paths
128128

129129
Default `undefined`.
130130

131-
This option allows you to provide one or more additional file extensions which should be considered in the TypeScript Program compilation. E.g. a `.vue` file
131+
This option allows you to provide one or more additional file extensions which should be considered in the TypeScript Program compilation.
132+
The default extensions are `.ts`, `.tsx`, `.js`, and `.jsx`. Add extensions starting with `.`, followed by the file extension. E.g. for a `.vue` file use `"extraFileExtensions: [".vue"]`.
132133

133134
### `warnOnUnsupportedTypeScriptVersion`
134135

packages/parser/tests/lib/parser.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ describe('parser', () => {
5656
errorOnUnknownASTType: false,
5757
errorOnTypeScriptSyntacticAndSemanticIssues: false,
5858
tsconfigRootDir: 'tests/fixtures/services',
59-
extraFileExtensions: ['foo'],
59+
extraFileExtensions: ['.foo'],
6060
};
6161
parseForESLint(code, config);
6262
expect(spy).toHaveBeenCalledWith(code, {

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

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,12 @@ import { ASTAndProgram } from './shared';
77

88
const log = debug('typescript-eslint:typescript-estree:createProjectProgram');
99

10+
const DEFAULT_EXTRA_FILE_EXTENSIONS = ['.ts', '.tsx', '.js', '.jsx'];
11+
1012
/**
1113
* @param code The code of the file being linted
12-
* @param options The config object
14+
* @param createDefaultProgram True if the default program should be created
15+
* @param extra The config object
1316
* @returns If found, returns the source file corresponding to the code and the containing program
1417
*/
1518
function createProjectProgram(
@@ -38,11 +41,26 @@ function createProjectProgram(
3841
];
3942
let hasMatchedAnError = false;
4043

44+
const extraFileExtensions = extra.extraFileExtensions || [];
45+
46+
extraFileExtensions.forEach(extraExtension => {
47+
if (!extraExtension.startsWith('.')) {
48+
errorLines.push(
49+
`Found unexpected extension "${extraExtension}" specified with the "extraFileExtensions" option. Did you mean ".${extraExtension}"?`,
50+
);
51+
}
52+
if (DEFAULT_EXTRA_FILE_EXTENSIONS.includes(extraExtension)) {
53+
errorLines.push(
54+
`You unnecessarily included the extension "${extraExtension}" with the "extraFileExtensions" option. This extension is already handled by the parser by default.`,
55+
);
56+
}
57+
});
58+
4159
const fileExtension = path.extname(extra.filePath);
42-
if (!['.ts', '.tsx', '.js', '.jsx'].includes(fileExtension)) {
60+
if (!DEFAULT_EXTRA_FILE_EXTENSIONS.includes(fileExtension)) {
4361
const nonStandardExt = `The extension for the file (${fileExtension}) is non-standard`;
44-
if (extra.extraFileExtensions && extra.extraFileExtensions.length > 0) {
45-
if (!extra.extraFileExtensions.includes(fileExtension)) {
62+
if (extraFileExtensions.length > 0) {
63+
if (!extraFileExtensions.includes(fileExtension)) {
4664
errorLines.push(
4765
`${nonStandardExt}. It should be added to your existing "parserOptions.extraFileExtensions".`,
4866
);

0 commit comments

Comments
 (0)
0