Skip to content

chore(typescript-estree): clarify default file extensions #963

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 5 commits into from
Jan 5, 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
3 changes: 2 additions & 1 deletion packages/parser/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,8 @@ This option allows you to provide the root directory for relative tsconfig paths

Default `undefined`.

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
This option allows you to provide one or more additional file extensions which should be considered in the TypeScript Program compilation.
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"]`.

### `warnOnUnsupportedTypeScriptVersion`

Expand Down
2 changes: 1 addition & 1 deletion packages/parser/tests/lib/parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ describe('parser', () => {
errorOnUnknownASTType: false,
errorOnTypeScriptSyntacticAndSemanticIssues: false,
tsconfigRootDir: 'tests/fixtures/services',
extraFileExtensions: ['foo'],
extraFileExtensions: ['.foo'],
};
parseForESLint(code, config);
expect(spy).toHaveBeenCalledWith(code, {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,12 @@ import { ASTAndProgram } from './shared';

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

const DEFAULT_EXTRA_FILE_EXTENSIONS = ['.ts', '.tsx', '.js', '.jsx'];

/**
* @param code The code of the file being linted
* @param options The config object
* @param createDefaultProgram True if the default program should be created
* @param extra The config object
* @returns If found, returns the source file corresponding to the code and the containing program
*/
function createProjectProgram(
Expand Down Expand Up @@ -38,11 +41,26 @@ function createProjectProgram(
];
let hasMatchedAnError = false;

const extraFileExtensions = extra.extraFileExtensions || [];

extraFileExtensions.forEach(extraExtension => {
if (!extraExtension.startsWith('.')) {
errorLines.push(
`Found unexpected extension "${extraExtension}" specified with the "extraFileExtensions" option. Did you mean ".${extraExtension}"?`,
);
}
if (DEFAULT_EXTRA_FILE_EXTENSIONS.includes(extraExtension)) {
errorLines.push(
`You unnecessarily included the extension "${extraExtension}" with the "extraFileExtensions" option. This extension is already handled by the parser by default.`,
);
}
});

const fileExtension = path.extname(extra.filePath);
if (!['.ts', '.tsx', '.js', '.jsx'].includes(fileExtension)) {
if (!DEFAULT_EXTRA_FILE_EXTENSIONS.includes(fileExtension)) {
const nonStandardExt = `The extension for the file (${fileExtension}) is non-standard`;
if (extra.extraFileExtensions && extra.extraFileExtensions.length > 0) {
if (!extra.extraFileExtensions.includes(fileExtension)) {
if (extraFileExtensions.length > 0) {
if (!extraFileExtensions.includes(fileExtension)) {
errorLines.push(
`${nonStandardExt}. It should be added to your existing "parserOptions.extraFileExtensions".`,
);
Expand Down