Skip to content
This repository was archived by the owner on Jan 19, 2019. It is now read-only.

Commit bac14e1

Browse files
author
Kai Cataldo
committed
Breaking: Infer parsing as JSX from file extension (fixes #399)
1 parent 6611535 commit bac14e1

File tree

1 file changed

+35
-6
lines changed

1 file changed

+35
-6
lines changed

parser.js

+35-6
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,12 @@ const SUPPORTED_TYPESCRIPT_VERSIONS = require("./package.json").devDependencies.
1717
const ACTIVE_TYPESCRIPT_VERSION = ts.version;
1818
const isRunningSupportedTypeScriptVersion = semver.satisfies(ACTIVE_TYPESCRIPT_VERSION, SUPPORTED_TYPESCRIPT_VERSIONS);
1919

20+
const WARNING_BORDER = "=============";
21+
const DEFAULT_FILEPATH = "<text>";
22+
2023
let extra;
2124
let warnedAboutTSVersion = false;
25+
let warnedAboutJSXOverride = false;
2226

2327
/**
2428
* Resets the extra config object
@@ -73,17 +77,31 @@ function generateAST(code, options, additionalParsingContext) {
7377
if (typeof options.tokens === "boolean" && options.tokens) {
7478
extra.tokens = [];
7579
}
80+
7681
if (typeof options.comment === "boolean" && options.comment) {
7782
extra.comment = true;
7883
extra.comments = [];
7984
}
85+
8086
if (typeof options.tolerant === "boolean" && options.tolerant) {
8187
extra.errors = [];
8288
}
8389

84-
if (options.ecmaFeatures && typeof options.ecmaFeatures === "object") {
85-
// pass through jsx option
86-
extra.ecmaFeatures.jsx = options.ecmaFeatures.jsx;
90+
const hasEcmaFeatures = options.ecmaFeatures && typeof options.ecmaFeatures === "object";
91+
const hasFilePath = options.filePath && options.filePath !== DEFAULT_FILEPATH;
92+
const hasTsxExtension = hasFilePath && /.tsx$/.test(options.filePath);
93+
94+
// Allows user to parse a string of text passed on the command line in JSX mode.
95+
if (hasEcmaFeatures) {
96+
if (typeof options.ecmaFeatures.jsx !== "undefined") {
97+
extra.ecmaFeatures.jsx = options.ecmaFeatures.jsx;
98+
}
99+
}
100+
101+
// Infer whether or not the parser should parse in "JSX mode" or not.
102+
// This will override the parserOptions.ecmaFeatures.jsx config option if a filePath is provided.
103+
if (hasFilePath) {
104+
extra.ecmaFeatures.jsx = hasTsxExtension;
87105
}
88106

89107
/**
@@ -114,18 +132,29 @@ function generateAST(code, options, additionalParsingContext) {
114132
if (additionalParsingContext.isParseForESLint) {
115133
extra.parseForESLint = true;
116134
}
135+
136+
if (!warnedAboutJSXOverride && hasFilePath && hasEcmaFeatures && typeof options.ecmaFeatures.jsx !== "undefined") {
137+
const warning = [
138+
WARNING_BORDER,
139+
"typescript-eslint-parser will automatically detect whether it should be parsing in JSX mode or not based on file extension.",
140+
"Consider removing parserOptions.ecmaFeatures.jsx from your configuration, as it will be overridden by the extension of the file being parsed.",
141+
WARNING_BORDER
142+
];
143+
144+
extra.log(warning.join("\n\n"));
145+
warnedAboutJSXOverride = true;
146+
}
117147
}
118148

119149
if (!isRunningSupportedTypeScriptVersion && !warnedAboutTSVersion) {
120-
const border = "=============";
121150
const versionWarning = [
122-
border,
151+
WARNING_BORDER,
123152
"WARNING: You are currently running a version of TypeScript which is not officially supported by typescript-eslint-parser.",
124153
"You may find that it works just fine, or you may not.",
125154
`SUPPORTED TYPESCRIPT VERSIONS: ${SUPPORTED_TYPESCRIPT_VERSIONS}`,
126155
`YOUR TYPESCRIPT VERSION: ${ACTIVE_TYPESCRIPT_VERSION}`,
127156
"Please only submit bug reports when using the officially supported version.",
128-
border
157+
WARNING_BORDER
129158
];
130159
extra.log(versionWarning.join("\n\n"));
131160
warnedAboutTSVersion = true;

0 commit comments

Comments
 (0)