Skip to content
This repository has been archived by the owner on Jan 11, 2023. It is now read-only.

Commit

Permalink
[WIP] [Parser] detect if files use JSX (#4911)
Browse files Browse the repository at this point in the history
  • Loading branch information
davide-dc-dev authored and jasonLaster committed Dec 18, 2017
1 parent dbf9c74 commit 9e3ad6e
Show file tree
Hide file tree
Showing 6 changed files with 122 additions and 41 deletions.
1 change: 1 addition & 0 deletions src/actions/tests/__snapshots__/ast.spec.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,7 @@ Object {
],
},
],
"hasJsx": false,
"identifiers": Array [
Object {
"expression": "base",
Expand Down
9 changes: 8 additions & 1 deletion src/workers/parser/getSymbols.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {|
Expand Down Expand Up @@ -104,6 +105,7 @@ function extractSymbols(source: Source) {
const identifiers = [];
const classes = [];
const imports = [];
let hasJsx = false;

const ast = traverseAst(source, {
enter(path: NodePath) {
Expand All @@ -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,
Expand Down Expand Up @@ -218,7 +224,8 @@ function extractSymbols(source: Source) {
comments,
identifiers,
classes,
imports
imports,
hasJsx
};
}

Expand Down
84 changes: 71 additions & 13 deletions src/workers/parser/tests/__snapshots__/getSymbols.spec.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,9 @@ classes:
[(23, 0), (31, 1)] Ultra
imports:
"
hasJsx: false"
`;

exports[`Parser.getSymbols call sites 1`] = `
Expand Down Expand Up @@ -115,7 +117,9 @@ classes:
imports:
"
hasJsx: false"
`;

exports[`Parser.getSymbols class 1`] = `
Expand Down Expand Up @@ -164,7 +168,9 @@ classes:
[(15, 0), (15, 14)] Test2
imports:
"
hasJsx: false"
`;

exports[`Parser.getSymbols component 1`] = `
Expand Down Expand Up @@ -325,7 +331,9 @@ classes:
[(5, 0), (20, 1)] Punny
imports:
"
hasJsx: true"
`;

exports[`Parser.getSymbols es6 1`] = `
Expand Down Expand Up @@ -357,7 +365,9 @@ classes:
imports:
"
hasJsx: false"
`;

exports[`Parser.getSymbols expression 1`] = `
Expand Down Expand Up @@ -563,7 +573,9 @@ classes:
imports:
"
hasJsx: false"
`;

exports[`Parser.getSymbols finds symbols in an html file 1`] = `
Expand Down Expand Up @@ -640,7 +652,9 @@ classes:
imports:
"
hasJsx: false"
`;

exports[`Parser.getSymbols flow 1`] = `
Expand Down Expand Up @@ -673,7 +687,9 @@ classes:
[(1, 0), (5, 1)] App
imports:
"
hasJsx: false"
`;

exports[`Parser.getSymbols func 1`] = `
Expand Down Expand Up @@ -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`] = `
Expand Down Expand Up @@ -776,7 +826,9 @@ classes:
imports:
"
hasJsx: false"
`;

exports[`Parser.getSymbols proto 1`] = `
Expand Down Expand Up @@ -834,7 +886,9 @@ classes:
imports:
"
hasJsx: false"
`;

exports[`Parser.getSymbols react component 1`] = `
Expand Down Expand Up @@ -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`] = `
Expand Down Expand Up @@ -909,5 +965,7 @@ classes:
imports:
"
hasJsx: false"
`;
1 change: 1 addition & 0 deletions src/workers/parser/tests/fixtures/jsx.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
const jsxElement = <h1> Hi ! I'm here ! </h1>;
3 changes: 2 additions & 1 deletion src/workers/parser/tests/getSymbols.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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" }
]
);
65 changes: 39 additions & 26 deletions src/workers/parser/utils/formatSymbols.js
Original file line number Diff line number Diff line change
Expand Up @@ -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");
}

0 comments on commit 9e3ad6e

Please sign in to comment.