feat(typescript): add fallback for JSX detection (#1551)

master
Lucas Azzola 2017-05-08 22:57:19 +10:00 committed by Christopher Chedeau
parent eabff629dd
commit 44934da349
6 changed files with 82 additions and 19 deletions

View File

@ -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 "</"
"(^[^\"'`]*</)", // Contains "</" when probably not in a string
"|",
"(^[^/]{2}.*\/>)" // Contains "/>" on line not starting with "//"
].join(""), "m").test(text);

View File

@ -0,0 +1,40 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP
exports[`not-react.ts 1`] = `
/// <reference types="node" />
type ul = any;
const somethingElse = 1;
const thing = <ul>somethingElse;
const div = "<div></div>";
const h1 = \`<h1>Hi</h1>\`;
const footer = '<footer></footer>';
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/// <reference types="node" />
type ul = any;
const somethingElse = 1;
const thing = <ul>somethingElse;
const div = "<div></div>";
const h1 = \`<h1>Hi</h1>\`;
const footer = "<footer></footer>";
`;
exports[`react.tsx 1`] = `
const MyCoolList = ({ things }) =>
<ul>
{things.map(MyCoolThing)}
</ul>;
const MyCoolThing = ({ thingo }) => <li>{thingo}</li>;
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
const MyCoolList = ({ things }) => (
<ul>
{things.map(MyCoolThing)}
</ul>
);
const MyCoolThing = ({ thingo }) => <li>{thingo}</li>;
`;

View File

@ -0,0 +1 @@
run_spec(__dirname, {parser: "typescript"});

View File

@ -0,0 +1,8 @@
/// <reference types="node" />
type ul = any;
const somethingElse = 1;
const thing = <ul>somethingElse;
const div = "<div></div>";
const h1 = `<h1>Hi</h1>`;
const footer = '<footer></footer>';

View File

@ -0,0 +1,6 @@
const MyCoolList = ({ things }) =>
<ul>
{things.map(MyCoolThing)}
</ul>;
const MyCoolThing = ({ thingo }) => <li>{thingo}</li>;

View File

@ -1,6 +1,7 @@
"use strict";
const fs = require("fs");
const extname = require("path").extname;
const prettier = require("../");
const types = require("../src/ast-types");
const parser = require("../src/parser");
@ -25,10 +26,8 @@ function removeEmptyStatements(ast) {
function run_spec(dirname, options, additionalParsers) {
fs.readdirSync(dirname).forEach(filename => {
if (
(filename.endsWith(".js") || filename.endsWith(".ts")) &&
filename !== "jsfmt.spec.js"
) {
const extension = extname(filename);
if (/^\.[jt]sx?$/.test(extension) && filename !== "jsfmt.spec.js") {
const path = dirname + "/" + filename;
if (!RUN_AST_TESTS) {