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

Commit bce7080

Browse files
atwalg2jasonLaster
authored andcommitted
[Preview] Evaluate Object properties with an assignment (#5693)
1 parent 4549bb8 commit bce7080

File tree

6 files changed

+402
-60
lines changed

6 files changed

+402
-60
lines changed

src/workers/parser/getSymbols.js

Lines changed: 21 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,19 @@
44

55
// @flow
66

7-
import flatten from "lodash/flatten";
87
import * as t from "@babel/types";
98

109
import createSimplePath from "./utils/simple-path";
1110
import { traverseAst } from "./utils/ast";
1211
import {
1312
isVariable,
1413
isFunction,
15-
getVariables,
16-
isComputedExpression
14+
isObjectShorthand,
15+
isComputedExpression,
16+
getObjectExpressionValue,
17+
getVariableNames,
18+
getComments,
19+
getSpecifiers
1720
} from "./utils/helpers";
1821

1922
import { inferClassName } from "./utils/inferClassName";
@@ -121,61 +124,6 @@ function getFunctionParameterNames(path: SimplePath): string[] {
121124
return [];
122125
}
123126

124-
function getVariableNames(path: SimplePath): SymbolDeclaration[] {
125-
if (t.isObjectProperty(path.node) && !isFunction(path.node.value)) {
126-
if (path.node.key.type === "StringLiteral") {
127-
return [
128-
{
129-
name: path.node.key.value,
130-
location: path.node.loc
131-
}
132-
];
133-
} else if (path.node.value.type === "Identifier") {
134-
return [{ name: path.node.value.name, location: path.node.loc }];
135-
} else if (path.node.value.type === "AssignmentPattern") {
136-
return [{ name: path.node.value.left.name, location: path.node.loc }];
137-
}
138-
139-
return [
140-
{
141-
name: path.node.key.name,
142-
location: path.node.loc
143-
}
144-
];
145-
}
146-
147-
if (!path.node.declarations) {
148-
return path.node.params.map(dec => ({
149-
name: dec.name,
150-
location: dec.loc
151-
}));
152-
}
153-
154-
const declarations = path.node.declarations
155-
.filter(dec => dec.id.type !== "ObjectPattern")
156-
.map(getVariables);
157-
158-
return flatten(declarations);
159-
}
160-
161-
function getComments(ast) {
162-
if (!ast || !ast.comments) {
163-
return [];
164-
}
165-
return ast.comments.map(comment => ({
166-
name: comment.location,
167-
location: comment.loc
168-
}));
169-
}
170-
171-
function getSpecifiers(specifiers) {
172-
if (!specifiers) {
173-
return [];
174-
}
175-
176-
return specifiers.map(specifier => specifier.local && specifier.local.name);
177-
}
178-
179127
/* eslint-disable complexity */
180128
function extractSymbol(path: SimplePath, symbols) {
181129
if (isVariable(path)) {
@@ -261,6 +209,15 @@ function extractSymbol(path: SimplePath, symbols) {
261209
}
262210
}
263211

212+
if (t.isStringLiteral(path) && t.isProperty(path.parentPath)) {
213+
let { start, end } = path.node.loc;
214+
return symbols.identifiers.push({
215+
name: path.node.value,
216+
expression: getObjectExpressionValue(path.parent),
217+
location: { start, end }
218+
});
219+
}
220+
264221
if (t.isIdentifier(path) && !t.isGenericTypeAnnotation(path.parent)) {
265222
let { start, end } = path.node.loc;
266223

@@ -269,8 +226,12 @@ function extractSymbol(path: SimplePath, symbols) {
269226
return;
270227
}
271228

272-
if (t.isProperty(path.parent)) {
273-
return;
229+
if (t.isProperty(path.parentPath) && !isObjectShorthand(path.parent)) {
230+
return symbols.identifiers.push({
231+
name: path.node.name,
232+
expression: getObjectExpressionValue(path.parent),
233+
location: { start, end }
234+
});
274235
}
275236

276237
if (path.node.typeAnnotation) {

0 commit comments

Comments
 (0)