@@ -17,8 +17,12 @@ const SUPPORTED_TYPESCRIPT_VERSIONS = require("./package.json").devDependencies.
17
17
const ACTIVE_TYPESCRIPT_VERSION = ts . version ;
18
18
const isRunningSupportedTypeScriptVersion = semver . satisfies ( ACTIVE_TYPESCRIPT_VERSION , SUPPORTED_TYPESCRIPT_VERSIONS ) ;
19
19
20
+ const WARNING_BORDER = "=============" ;
21
+ const DEFAULT_FILEPATH = "<text>" ;
22
+
20
23
let extra ;
21
24
let warnedAboutTSVersion = false ;
25
+ let warnedAboutJSXOverride = false ;
22
26
23
27
/**
24
28
* Resets the extra config object
@@ -73,17 +77,31 @@ function generateAST(code, options, additionalParsingContext) {
73
77
if ( typeof options . tokens === "boolean" && options . tokens ) {
74
78
extra . tokens = [ ] ;
75
79
}
80
+
76
81
if ( typeof options . comment === "boolean" && options . comment ) {
77
82
extra . comment = true ;
78
83
extra . comments = [ ] ;
79
84
}
85
+
80
86
if ( typeof options . tolerant === "boolean" && options . tolerant ) {
81
87
extra . errors = [ ] ;
82
88
}
83
89
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 !== DEFAULT_FILEPATH ;
92
+ const hasTsxExtension = hasFilePath && / .t s x $ / . 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 ;
87
105
}
88
106
89
107
/**
@@ -114,18 +132,29 @@ function generateAST(code, options, additionalParsingContext) {
114
132
if ( additionalParsingContext . isParseForESLint ) {
115
133
extra . parseForESLint = true ;
116
134
}
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
+ }
117
147
}
118
148
119
149
if ( ! isRunningSupportedTypeScriptVersion && ! warnedAboutTSVersion ) {
120
- const border = "=============" ;
121
150
const versionWarning = [
122
- border ,
151
+ WARNING_BORDER ,
123
152
"WARNING: You are currently running a version of TypeScript which is not officially supported by typescript-eslint-parser." ,
124
153
"You may find that it works just fine, or you may not." ,
125
154
`SUPPORTED TYPESCRIPT VERSIONS: ${ SUPPORTED_TYPESCRIPT_VERSIONS } ` ,
126
155
`YOUR TYPESCRIPT VERSION: ${ ACTIVE_TYPESCRIPT_VERSION } ` ,
127
156
"Please only submit bug reports when using the officially supported version." ,
128
- border
157
+ WARNING_BORDER
129
158
] ;
130
159
extra . log ( versionWarning . join ( "\n\n" ) ) ;
131
160
warnedAboutTSVersion = true ;
0 commit comments