From 9e3ad6ecd09917d4f4f206028fbe59647835bb2a Mon Sep 17 00:00:00 2001 From: Davide De Cenzo Date: Fri, 15 Dec 2017 23:43:54 +0100 Subject: [PATCH] [WIP] [Parser] detect if files use JSX (#4911) --- .../tests/__snapshots__/ast.spec.js.snap | 1 + src/workers/parser/getSymbols.js | 9 +- .../__snapshots__/getSymbols.spec.js.snap | 84 ++++++++++++++++--- src/workers/parser/tests/fixtures/jsx.js | 1 + src/workers/parser/tests/getSymbols.spec.js | 3 +- src/workers/parser/utils/formatSymbols.js | 65 ++++++++------ 6 files changed, 122 insertions(+), 41 deletions(-) create mode 100644 src/workers/parser/tests/fixtures/jsx.js diff --git a/src/actions/tests/__snapshots__/ast.spec.js.snap b/src/actions/tests/__snapshots__/ast.spec.js.snap index 1858a5a0ac..0811bd93e1 100644 --- a/src/actions/tests/__snapshots__/ast.spec.js.snap +++ b/src/actions/tests/__snapshots__/ast.spec.js.snap @@ -188,6 +188,7 @@ Object { ], }, ], + "hasJsx": false, "identifiers": Array [ Object { "expression": "base", diff --git a/src/workers/parser/getSymbols.js b/src/workers/parser/getSymbols.js index 89ac650577..77b5717cf8 100644 --- a/src/workers/parser/getSymbols.js +++ b/src/workers/parser/getSymbols.js @@ -12,6 +12,7 @@ import getFunctionName from "./utils/getFunctionName"; import type { Source } from "debugger-html"; import type { NodePath, Node, Location as BabelLocation } from "babel-traverse"; + let symbolDeclarations = new Map(); export type SymbolDeclaration = {| @@ -104,6 +105,7 @@ function extractSymbols(source: Source) { const identifiers = []; const classes = []; const imports = []; + let hasJsx = false; const ast = traverseAst(source, { enter(path: NodePath) { @@ -121,6 +123,10 @@ function extractSymbols(source: Source) { }); } + if (t.isJSXElement(path)) { + hasJsx = true; + } + if (t.isClassDeclaration(path)) { classes.push({ name: path.node.id.name, @@ -218,7 +224,8 @@ function extractSymbols(source: Source) { comments, identifiers, classes, - imports + imports, + hasJsx }; } diff --git a/src/workers/parser/tests/__snapshots__/getSymbols.spec.js.snap b/src/workers/parser/tests/__snapshots__/getSymbols.spec.js.snap index cc05021b60..744daec238 100644 --- a/src/workers/parser/tests/__snapshots__/getSymbols.spec.js.snap +++ b/src/workers/parser/tests/__snapshots__/getSymbols.spec.js.snap @@ -77,7 +77,9 @@ classes: [(23, 0), (31, 1)] Ultra imports: -" + + +hasJsx: false" `; exports[`Parser.getSymbols call sites 1`] = ` @@ -115,7 +117,9 @@ classes: imports: -" + + +hasJsx: false" `; exports[`Parser.getSymbols class 1`] = ` @@ -164,7 +168,9 @@ classes: [(15, 0), (15, 14)] Test2 imports: -" + + +hasJsx: false" `; exports[`Parser.getSymbols component 1`] = ` @@ -325,7 +331,9 @@ classes: [(5, 0), (20, 1)] Punny imports: -" + + +hasJsx: true" `; exports[`Parser.getSymbols es6 1`] = ` @@ -357,7 +365,9 @@ classes: imports: -" + + +hasJsx: false" `; exports[`Parser.getSymbols expression 1`] = ` @@ -563,7 +573,9 @@ classes: imports: -" + + +hasJsx: false" `; exports[`Parser.getSymbols finds symbols in an html file 1`] = ` @@ -640,7 +652,9 @@ classes: imports: -" + + +hasJsx: false" `; exports[`Parser.getSymbols flow 1`] = ` @@ -673,7 +687,9 @@ classes: [(1, 0), (5, 1)] App imports: -" + + +hasJsx: false" `; exports[`Parser.getSymbols func 1`] = ` @@ -722,7 +738,41 @@ classes: imports: -" + + +hasJsx: false" +`; + +exports[`Parser.getSymbols jsx 1`] = ` +"functions: + + +variables: +[(1, 6), (1, 45)] jsxElement + +callExpressions: + + +memberExpressions: + + +objectProperties: + + +comments: + + +identifiers: +[(1, 6), (1, 45)] jsxElement jsxElement +[(1, 6), (1, 16)] jsxElement jsxElement + +classes: + + +imports: + + +hasJsx: true" `; exports[`Parser.getSymbols math 1`] = ` @@ -776,7 +826,9 @@ classes: imports: -" + + +hasJsx: false" `; exports[`Parser.getSymbols proto 1`] = ` @@ -834,7 +886,9 @@ classes: imports: -" + + +hasJsx: false" `; exports[`Parser.getSymbols react component 1`] = ` @@ -867,7 +921,9 @@ classes: [(3, 0), (3, 39)] PrimaryPanes imports: -[(1, 0), (1, 41)] React, Component" +[(1, 0), (1, 41)] React, Component + +hasJsx: false" `; exports[`Parser.getSymbols var 1`] = ` @@ -909,5 +965,7 @@ classes: imports: -" + + +hasJsx: false" `; diff --git a/src/workers/parser/tests/fixtures/jsx.js b/src/workers/parser/tests/fixtures/jsx.js new file mode 100644 index 0000000000..f67ed71707 --- /dev/null +++ b/src/workers/parser/tests/fixtures/jsx.js @@ -0,0 +1 @@ +const jsxElement =

Hi ! I'm here !

; diff --git a/src/workers/parser/tests/getSymbols.spec.js b/src/workers/parser/tests/getSymbols.spec.js index 58850c64b9..8981422c7e 100644 --- a/src/workers/parser/tests/getSymbols.spec.js +++ b/src/workers/parser/tests/getSymbols.spec.js @@ -25,6 +25,7 @@ cases( }, { name: "component", file: "component" }, { name: "react component", file: "frameworks/component" }, - { name: "flow", file: "flow" } + { name: "flow", file: "flow" }, + { name: "jsx", file: "jsx" } ] ); diff --git a/src/workers/parser/utils/formatSymbols.js b/src/workers/parser/utils/formatSymbols.js index a0cec694a1..9403013feb 100644 --- a/src/workers/parser/utils/formatSymbols.js +++ b/src/workers/parser/utils/formatSymbols.js @@ -4,38 +4,51 @@ import getSymbols from "../getSymbols"; -export function formatSymbols(source: Source) { - const symbols = getSymbols(source); +function formatLocation(loc) { + if (!loc) { + return ""; + } - function formatLocation(loc) { - if (!loc) { - return ""; - } - const { start, end } = loc; + const { start, end } = loc; + const startLoc = `(${start.line}, ${start.column})`; + const endLoc = `(${end.line}, ${end.column})`; - const startLoc = `(${start.line}, ${start.column})`; - const endLoc = `(${end.line}, ${end.column})`; - return `[${startLoc}, ${endLoc}]`; + return `[${startLoc}, ${endLoc}]`; +} + +function summarize(symbol) { + if (typeof symbol == "boolean") { + return symbol ? "true" : "false"; } - function summarize(symbol) { - const loc = formatLocation(symbol.location); - const exprLoc = formatLocation(symbol.expressionLocation); - const params = symbol.parameterNames - ? `(${symbol.parameterNames.join(", ")})` - : ""; - const expression = symbol.expression || ""; - const klass = symbol.klass || ""; - - const name = symbol.name || ""; - const names = symbol.specifiers ? symbol.specifiers.join(", ") : ""; - - return `${loc} ${exprLoc} ${expression} ${name}${params} ${klass} ${ - names - }`.trim(); // eslint-disable-line max-len + const loc = formatLocation(symbol.location); + const exprLoc = formatLocation(symbol.expressionLocation); + const params = symbol.parameterNames + ? `(${symbol.parameterNames.join(", ")})` + : ""; + const expression = symbol.expression || ""; + const klass = symbol.klass || ""; + const name = symbol.name || ""; + const names = symbol.specifiers ? symbol.specifiers.join(", ") : ""; + const values = symbol.values ? symbol.values.join(", ") : ""; + + return `${loc} ${exprLoc} ${expression} ${name}${params} ${klass} ${names} ${ + values + }`.trim(); // eslint-disable-line max-len +} + +function formatKey(name, symbols) { + if (name == "hasJsx") { + return `hasJsx: ${symbols.hasJsx ? "true" : "false"}`; } + return `${name}:\n${symbols[name].map(summarize).join("\n")}`; +} + +export function formatSymbols(source: Source) { + const symbols = getSymbols(source); return Object.keys(symbols) - .map(name => `${name}:\n${symbols[name].map(summarize).join("\n")}`) + + .map(name => formatKey(name, symbols)) .join("\n\n"); }