Skip to content
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

fix(typescript-estree): fix filename handling for vue JSX + markdown #1127

Merged
merged 12 commits into from Nov 3, 2019
@@ -1,7 +1,11 @@
import debug from 'debug';
import ts from 'typescript';
import { Extra } from '../parser-options';
import { ASTAndProgram, DEFAULT_COMPILER_OPTIONS } from './shared';
import {
ASTAndProgram,
DEFAULT_COMPILER_OPTIONS,
getScriptKind,
} from './shared';

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

Expand Down Expand Up @@ -43,8 +47,7 @@ function createIsolatedProgram(code: string, extra: Extra): ASTAndProgram {
code,
ts.ScriptTarget.Latest,
true,
// force typescript to ignore the file extension
extra.jsx ? ts.ScriptKind.TSX : ts.ScriptKind.TS,
getScriptKind(extra, filename),
);
},
readFile() {
Expand Down
@@ -1,6 +1,7 @@
import debug from 'debug';
import ts from 'typescript';
import { Extra } from '../parser-options';
import { getScriptKind } from './shared';

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

Expand All @@ -16,8 +17,7 @@ function createSourceFile(code: string, extra: Extra): ts.SourceFile {
code,
ts.ScriptTarget.Latest,
/* setParentNodes */ true,
// force typescript to ignore the file extension
extra.jsx ? ts.ScriptKind.TSX : ts.ScriptKind.TS,
getScriptKind(extra),
);
}

Expand Down
30 changes: 30 additions & 0 deletions packages/typescript-estree/src/create-program/shared.ts
Expand Up @@ -38,11 +38,41 @@ function canonicalDirname(p: CanonicalPath): CanonicalPath {
return path.dirname(p) as CanonicalPath;
}

function getScriptKind(
extra: Extra,
filePath: string = extra.filePath,
): ts.ScriptKind {
const extension = path.extname(filePath);
// note - we respect the user's extension when it is known we could override it and force it to match their
// jsx setting, but that could create weird situations where we throw parse errors when TSC doesn't
switch (extension.toLowerCase()) {
case 'ts':
return ts.ScriptKind.TS;

case 'tsx':
return ts.ScriptKind.TSX;

case 'js':
return ts.ScriptKind.JS;
bradzacher marked this conversation as resolved.
Show resolved Hide resolved

case 'jsx':
return ts.ScriptKind.JSX;

case 'json':
return ts.ScriptKind.JSON;

default:
// unknown extension, force typescript to ignore the file extension, and respect the user's setting
return extra.jsx ? ts.ScriptKind.TSX : ts.ScriptKind.TS;
}
}

export {
ASTAndProgram,
canonicalDirname,
CanonicalPath,
DEFAULT_COMPILER_OPTIONS,
getCanonicalFileName,
getScriptKind,
getTsconfigPath,
};