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

Commit b095ac5

Browse files
bomsyjasonLaster
authored andcommitted
added literals for computed member expressions (#5818)
1 parent 70f4b2e commit b095ac5

File tree

6 files changed

+85
-19
lines changed

6 files changed

+85
-19
lines changed

src/actions/preview.js

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -73,21 +73,19 @@ function isInvalidTarget(target: HTMLElement) {
7373
const cursorPos = target.getBoundingClientRect();
7474

7575
// exclude literal tokens where it does not make sense to show a preview
76-
const invaildType = ["cm-string", "cm-number", "cm-atom"].includes(
77-
target.className
78-
);
76+
const invalidType = ["cm-atom", ""].includes(target.className);
7977

8078
// exclude syntax where the expression would be a syntax error
8179
const invalidToken =
82-
tokenText === "" || tokenText.match(/[(){}\|&%,.;=<>\+-/\*\s]/);
80+
tokenText === "" || tokenText.match(/^[(){}\|&%,.;=<>\+-/\*\s](?=)/);
8381

8482
// exclude codemirror elements that are not tokens
8583
const invalidTarget =
8684
(target.parentElement &&
8785
!target.parentElement.closest(".CodeMirror-line")) ||
8886
cursorPos.top == 0;
8987

90-
return invalidTarget || invalidToken || invaildType;
88+
return invalidTarget || invalidToken || invalidType;
9189
}
9290

9391
export function getExtra(

src/actions/tests/__snapshots__/ast.spec.js.snap

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,7 @@ Object {
123123
},
124124
],
125125
"imports": Array [],
126+
"literals": Array [],
126127
"memberExpressions": Array [],
127128
"objectProperties": Array [],
128129
"variables": Array [

src/utils/ast.js

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -19,21 +19,23 @@ export function findBestMatchExpression(
1919
symbols: SymbolDeclarations,
2020
tokenPos: Position
2121
) {
22-
const { memberExpressions, identifiers } = symbols;
22+
const { memberExpressions, identifiers, literals } = symbols;
2323
const { line, column } = tokenPos;
24-
return identifiers.concat(memberExpressions).reduce((found, expression) => {
25-
const overlaps =
26-
expression.location.start.line == line &&
27-
expression.location.start.column <= column &&
28-
expression.location.end.column >= column &&
29-
!expression.computed;
24+
return identifiers
25+
.concat(memberExpressions, literals)
26+
.reduce((found, expression) => {
27+
const overlaps =
28+
expression.location.start.line == line &&
29+
expression.location.start.column <= column &&
30+
expression.location.end.column >= column &&
31+
!expression.computed;
3032

31-
if (overlaps) {
32-
return expression;
33-
}
33+
if (overlaps) {
34+
return expression;
35+
}
3436

35-
return found;
36-
}, null);
37+
return found;
38+
}, null);
3739
}
3840

3941
export function findEmptyLines(

src/workers/parser/getSymbols.js

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ function getSpecifiers(specifiers) {
129129

130130
return specifiers.map(specifier => specifier.local && specifier.local.name);
131131
}
132-
132+
/* eslint-disable complexity */
133133
function extractSymbol(path: SimplePath, symbols) {
134134
if (isVariable(path)) {
135135
symbols.variables.push(...getVariableNames(path));
@@ -189,6 +189,19 @@ function extractSymbol(path: SimplePath, symbols) {
189189
});
190190
}
191191

192+
if (
193+
(t.isStringLiteral(path) || t.isNumericLiteral(path)) &&
194+
t.isMemberExpression(path.parentPath)
195+
) {
196+
// We only need literals that are part of computed memeber expressions
197+
const { start, end } = path.node.loc;
198+
symbols.literals.push({
199+
name: path.node.value,
200+
location: { start, end },
201+
expression: getSnippet(path.parentPath)
202+
});
203+
}
204+
192205
if (t.isCallExpression(path)) {
193206
const callee = path.node.callee;
194207
const args = path.node.arguments;
@@ -249,6 +262,7 @@ function extractSymbol(path: SimplePath, symbols) {
249262
});
250263
}
251264
}
265+
/* eslint-enable complexity */
252266

253267
function extractSymbols(sourceId) {
254268
const symbols = {
@@ -261,6 +275,7 @@ function extractSymbols(sourceId) {
261275
identifiers: [],
262276
classes: [],
263277
imports: [],
278+
literals: [],
264279
hasJsx: false,
265280
hasTypes: false
266281
};

src/workers/parser/tests/__snapshots__/getSymbols.spec.js.snap

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,9 @@ classes:
8989
imports:
9090
9191
92+
literals:
93+
94+
9295
hasJsx: false
9396
9497
hasTypes: false"
@@ -131,6 +134,9 @@ classes:
131134
imports:
132135
133136
137+
literals:
138+
139+
134140
hasJsx: false
135141
136142
hasTypes: false"
@@ -181,6 +187,9 @@ classes:
181187
imports:
182188
183189
190+
literals:
191+
192+
184193
hasJsx: false
185194
186195
hasTypes: false"
@@ -333,6 +342,9 @@ classes:
333342
imports:
334343
335344
345+
literals:
346+
347+
336348
hasJsx: true
337349
338350
hasTypes: false"
@@ -421,6 +433,9 @@ classes:
421433
imports:
422434
423435
436+
literals:
437+
438+
424439
hasJsx: false
425440
426441
hasTypes: false"
@@ -455,6 +470,9 @@ classes:
455470
imports:
456471
457472
473+
literals:
474+
475+
458476
hasJsx: false
459477
460478
hasTypes: false"
@@ -634,6 +652,11 @@ classes:
634652
imports:
635653
636654
655+
literals:
656+
[(6, 37), (6, 38)] collection.books[1] 1
657+
[(7, 46), (7, 54)] collection.genres[\\"sci-fi\\"] sci-fi
658+
[(7, 63), (7, 64)] collection.genres[\\"sci-fi\\"].movies[0] 0
659+
637660
hasJsx: false
638661
639662
hasTypes: false"
@@ -713,6 +736,9 @@ classes:
713736
imports:
714737
715738
739+
literals:
740+
[(23, 15), (23, 16)] name[0] 0
741+
716742
hasJsx: false
717743
718744
hasTypes: false"
@@ -753,6 +779,9 @@ classes:
753779
imports:
754780
755781
782+
literals:
783+
784+
756785
hasJsx: false
757786
758787
hasTypes: true"
@@ -809,6 +838,9 @@ classes:
809838
imports:
810839
811840
841+
literals:
842+
843+
812844
hasJsx: false
813845
814846
hasTypes: false"
@@ -955,6 +987,9 @@ classes:
955987
imports:
956988
957989
990+
literals:
991+
992+
958993
hasJsx: false
959994
960995
hasTypes: false"
@@ -992,6 +1027,9 @@ classes:
9921027
imports:
9931028
9941029
1030+
literals:
1031+
1032+
9951033
hasJsx: true
9961034
9971035
hasTypes: false"
@@ -1050,6 +1088,9 @@ classes:
10501088
imports:
10511089
10521090
1091+
literals:
1092+
1093+
10531094
hasJsx: false
10541095
10551096
hasTypes: false"
@@ -1109,6 +1150,9 @@ classes:
11091150
imports:
11101151
11111152
1153+
literals:
1154+
1155+
11121156
hasJsx: false
11131157
11141158
hasTypes: false"
@@ -1146,6 +1190,9 @@ classes:
11461190
imports:
11471191
[(1, 0), (1, 41)] React, Component
11481192
1193+
literals:
1194+
1195+
11491196
hasJsx: false
11501197
11511198
hasTypes: false"
@@ -1192,6 +1239,9 @@ classes:
11921239
imports:
11931240
11941241
1242+
literals:
1243+
1244+
11951245
hasJsx: false
11961246
11971247
hasTypes: false"

src/workers/parser/utils/formatSymbols.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ function summarize(symbol) {
2929
: "";
3030
const expression = symbol.expression || "";
3131
const klass = symbol.klass || "";
32-
const name = symbol.name || "";
32+
const name = symbol.name == undefined ? "" : symbol.name;
3333
const names = symbol.specifiers ? symbol.specifiers.join(", ") : "";
3434
const values = symbol.values ? symbol.values.join(", ") : "";
3535

0 commit comments

Comments
 (0)