diff --git a/src/parser.js b/src/parser.js index bfa7b1009..426fcbc40 100644 --- a/src/parser.js +++ b/src/parser.js @@ -53,20 +53,15 @@ function parseWithBabylon(text) { } function parseWithTypeScript(text) { - // While we are working on typescript, we are putting it in devDependencies - // so it shouldn't be picked up by static analysis - const r = require; - const parser = r("typescript-eslint-parser"); + const jsx = isProbablyJsx(text); try { - return parser.parse(text, { - loc: true, - range: true, - tokens: true, - attachComment: true, - ecmaFeatures: { - jsx: isJsx(text) - } - }); + try { + // Try passing with our best guess first. + return tryParseTypeScript(text, jsx); + } catch (e) { + // But if we get it wrong, try the opposite. + return tryParseTypeScript(text, !jsx); + } } catch(e) { throw createError( e.message, @@ -76,13 +71,27 @@ function parseWithTypeScript(text) { } } +function tryParseTypeScript(text, jsx) { + // While we are working on typescript, we are putting it in devDependencies + // so it shouldn't be picked up by static analysis + const r = require; + const parser = r("typescript-eslint-parser"); + return parser.parse(text, { + loc: true, + range: true, + tokens: true, + attachComment: true, + ecmaFeatures: { jsx } + }); +} + /** * Use a naive regular expression until we address * https://github.com/prettier/prettier/issues/1538 */ -function isJsx(text) { +function isProbablyJsx(text) { return new RegExp([ - "()" // Contains "/>" on line not starting with "//" ].join(""), "m").test(text); diff --git a/tests/typescript_tsx/__snapshots__/jsfmt.spec.js.snap b/tests/typescript_tsx/__snapshots__/jsfmt.spec.js.snap new file mode 100644 index 000000000..8168e9ea9 --- /dev/null +++ b/tests/typescript_tsx/__snapshots__/jsfmt.spec.js.snap @@ -0,0 +1,40 @@ +// Jest Snapshot v1, https://goo.gl/fbAQLP + +exports[`not-react.ts 1`] = ` +/// + +type ul = any; +const somethingElse = 1; +const thing =