Skip to content

Commit 95c13fe

Browse files
authoredSep 30, 2019
fix(typescript-estree): handle optional computed prop w/o type (#1026)
1 parent 8ce3a81 commit 95c13fe

File tree

4 files changed

+1793
-256
lines changed

4 files changed

+1793
-256
lines changed
 

‎packages/parser/tests/lib/__snapshots__/typescript.ts.snap

+239-30
Original file line numberDiff line numberDiff line change
@@ -7660,68 +7660,114 @@ Object {
76607660

76617661
exports[`typescript fixtures/basics/class-with-optional-properties.src 1`] = `
76627662
Object {
7663-
"$id": 4,
7663+
"$id": 10,
76647664
"block": Object {
76657665
"range": Array [
76667666
0,
7667-
64,
7667+
219,
76687668
],
76697669
"type": "Program",
76707670
},
76717671
"childScopes": Array [
76727672
Object {
7673-
"$id": 3,
7673+
"$id": 9,
76747674
"block": Object {
76757675
"range": Array [
76767676
0,
7677-
64,
7677+
219,
76787678
],
76797679
"type": "Program",
76807680
},
76817681
"childScopes": Array [
76827682
Object {
7683-
"$id": 2,
7683+
"$id": 8,
76847684
"block": Object {
76857685
"range": Array [
7686-
0,
7687-
63,
7686+
51,
7687+
218,
76887688
],
76897689
"type": "ClassDeclaration",
76907690
},
76917691
"childScopes": Array [],
76927692
"functionExpressionScope": false,
76937693
"isStrict": true,
7694-
"references": Array [],
7695-
"throughReferences": Array [],
7694+
"references": Array [
7695+
Object {
7696+
"$id": 6,
7697+
"from": Object {
7698+
"$ref": 8,
7699+
},
7700+
"identifier": Object {
7701+
"name": "computed",
7702+
"range": Array [
7703+
116,
7704+
124,
7705+
],
7706+
"type": "Identifier",
7707+
},
7708+
"kind": "r",
7709+
"resolved": Object {
7710+
"$ref": 0,
7711+
},
7712+
"writeExpr": undefined,
7713+
},
7714+
Object {
7715+
"$id": 7,
7716+
"from": Object {
7717+
"$ref": 8,
7718+
},
7719+
"identifier": Object {
7720+
"name": "computed2",
7721+
"range": Array [
7722+
155,
7723+
164,
7724+
],
7725+
"type": "Identifier",
7726+
},
7727+
"kind": "r",
7728+
"resolved": Object {
7729+
"$ref": 1,
7730+
},
7731+
"writeExpr": undefined,
7732+
},
7733+
],
7734+
"throughReferences": Array [
7735+
Object {
7736+
"$ref": 6,
7737+
},
7738+
Object {
7739+
"$ref": 7,
7740+
},
7741+
],
76967742
"type": "class",
76977743
"upperScope": Object {
7698-
"$ref": 3,
7744+
"$ref": 9,
76997745
},
77007746
"variableMap": Object {
77017747
"Foo": Object {
7702-
"$ref": 1,
7748+
"$ref": 5,
77037749
},
77047750
},
77057751
"variableScope": Object {
7706-
"$ref": 3,
7752+
"$ref": 9,
77077753
},
77087754
"variables": Array [
77097755
Object {
7710-
"$id": 1,
7756+
"$id": 5,
77117757
"defs": Array [
77127758
Object {
77137759
"name": Object {
77147760
"name": "Foo",
77157761
"range": Array [
7716-
6,
7717-
9,
7762+
57,
7763+
60,
77187764
],
77197765
"type": "Identifier",
77207766
},
77217767
"node": Object {
77227768
"range": Array [
7723-
0,
7724-
63,
7769+
51,
7770+
218,
77257771
],
77267772
"type": "ClassDeclaration",
77277773
},
@@ -7734,54 +7780,217 @@ Object {
77347780
Object {
77357781
"name": "Foo",
77367782
"range": Array [
7737-
6,
7738-
9,
7783+
57,
7784+
60,
77397785
],
77407786
"type": "Identifier",
77417787
},
77427788
],
77437789
"name": "Foo",
77447790
"references": Array [],
77457791
"scope": Object {
7746-
"$ref": 2,
7792+
"$ref": 8,
77477793
},
77487794
},
77497795
],
77507796
},
77517797
],
77527798
"functionExpressionScope": false,
77537799
"isStrict": true,
7754-
"references": Array [],
7800+
"references": Array [
7801+
Object {
7802+
"$id": 3,
7803+
"from": Object {
7804+
"$ref": 9,
7805+
},
7806+
"identifier": Object {
7807+
"name": "computed",
7808+
"range": Array [
7809+
6,
7810+
14,
7811+
],
7812+
"type": "Identifier",
7813+
},
7814+
"kind": "w",
7815+
"resolved": Object {
7816+
"$ref": 0,
7817+
},
7818+
"writeExpr": Object {
7819+
"range": Array [
7820+
17,
7821+
23,
7822+
],
7823+
"type": "Literal",
7824+
},
7825+
},
7826+
Object {
7827+
"$id": 4,
7828+
"from": Object {
7829+
"$ref": 9,
7830+
},
7831+
"identifier": Object {
7832+
"name": "computed2",
7833+
"range": Array [
7834+
31,
7835+
40,
7836+
],
7837+
"type": "Identifier",
7838+
},
7839+
"kind": "w",
7840+
"resolved": Object {
7841+
"$ref": 1,
7842+
},
7843+
"writeExpr": Object {
7844+
"range": Array [
7845+
43,
7846+
49,
7847+
],
7848+
"type": "Literal",
7849+
},
7850+
},
7851+
],
77557852
"throughReferences": Array [],
77567853
"type": "module",
77577854
"upperScope": Object {
7758-
"$ref": 4,
7855+
"$ref": 10,
77597856
},
77607857
"variableMap": Object {
77617858
"Foo": Object {
7859+
"$ref": 2,
7860+
},
7861+
"computed": Object {
77627862
"$ref": 0,
77637863
},
7864+
"computed2": Object {
7865+
"$ref": 1,
7866+
},
77647867
},
77657868
"variableScope": Object {
7766-
"$ref": 3,
7869+
"$ref": 9,
77677870
},
77687871
"variables": Array [
77697872
Object {
77707873
"$id": 0,
77717874
"defs": Array [
77727875
Object {
77737876
"name": Object {
7774-
"name": "Foo",
7877+
"name": "computed",
77757878
"range": Array [
77767879
6,
7777-
9,
7880+
14,
77787881
],
77797882
"type": "Identifier",
77807883
},
77817884
"node": Object {
7885+
"range": Array [
7886+
6,
7887+
23,
7888+
],
7889+
"type": "VariableDeclarator",
7890+
},
7891+
"parent": Object {
77827892
"range": Array [
77837893
0,
7784-
63,
7894+
24,
7895+
],
7896+
"type": "VariableDeclaration",
7897+
},
7898+
"type": "Variable",
7899+
},
7900+
],
7901+
"eslintUsed": undefined,
7902+
"identifiers": Array [
7903+
Object {
7904+
"name": "computed",
7905+
"range": Array [
7906+
6,
7907+
14,
7908+
],
7909+
"type": "Identifier",
7910+
},
7911+
],
7912+
"name": "computed",
7913+
"references": Array [
7914+
Object {
7915+
"$ref": 3,
7916+
},
7917+
Object {
7918+
"$ref": 6,
7919+
},
7920+
],
7921+
"scope": Object {
7922+
"$ref": 9,
7923+
},
7924+
},
7925+
Object {
7926+
"$id": 1,
7927+
"defs": Array [
7928+
Object {
7929+
"name": Object {
7930+
"name": "computed2",
7931+
"range": Array [
7932+
31,
7933+
40,
7934+
],
7935+
"type": "Identifier",
7936+
},
7937+
"node": Object {
7938+
"range": Array [
7939+
31,
7940+
49,
7941+
],
7942+
"type": "VariableDeclarator",
7943+
},
7944+
"parent": Object {
7945+
"range": Array [
7946+
25,
7947+
50,
7948+
],
7949+
"type": "VariableDeclaration",
7950+
},
7951+
"type": "Variable",
7952+
},
7953+
],
7954+
"eslintUsed": undefined,
7955+
"identifiers": Array [
7956+
Object {
7957+
"name": "computed2",
7958+
"range": Array [
7959+
31,
7960+
40,
7961+
],
7962+
"type": "Identifier",
7963+
},
7964+
],
7965+
"name": "computed2",
7966+
"references": Array [
7967+
Object {
7968+
"$ref": 4,
7969+
},
7970+
Object {
7971+
"$ref": 7,
7972+
},
7973+
],
7974+
"scope": Object {
7975+
"$ref": 9,
7976+
},
7977+
},
7978+
Object {
7979+
"$id": 2,
7980+
"defs": Array [
7981+
Object {
7982+
"name": Object {
7983+
"name": "Foo",
7984+
"range": Array [
7985+
57,
7986+
60,
7987+
],
7988+
"type": "Identifier",
7989+
},
7990+
"node": Object {
7991+
"range": Array [
7992+
51,
7993+
218,
77857994
],
77867995
"type": "ClassDeclaration",
77877996
},
@@ -7794,16 +8003,16 @@ Object {
77948003
Object {
77958004
"name": "Foo",
77968005
"range": Array [
7797-
6,
7798-
9,
8006+
57,
8007+
60,
77998008
],
78008009
"type": "Identifier",
78018010
},
78028011
],
78038012
"name": "Foo",
78048013
"references": Array [],
78058014
"scope": Object {
7806-
"$ref": 3,
8015+
"$ref": 9,
78078016
},
78088017
},
78098018
],
@@ -7817,7 +8026,7 @@ Object {
78178026
"upperScope": null,
78188027
"variableMap": Object {},
78198028
"variableScope": Object {
7820-
"$ref": 4,
8029+
"$ref": 10,
78218030
},
78228031
"variables": Array [],
78238032
}
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,13 @@
1+
const computed = 'buzz';
2+
const computed2 = 'bazz';
13
class Foo {
24
foo?;
35
bar? : string;
46
private baz? : string;
7+
[computed]?;
8+
['literal']?;
9+
[1]?;
10+
[computed2]?: string;
11+
['literal2']?: string;
12+
[2]?: string;
513
}

‎packages/typescript-estree/src/convert.ts

+5-1
Original file line numberDiff line numberDiff line change
@@ -941,7 +941,11 @@ export class Converter {
941941
result.accessibility = accessibility;
942942
}
943943

944-
if (node.name.kind === SyntaxKind.Identifier && node.questionToken) {
944+
if (
945+
(node.name.kind === SyntaxKind.Identifier ||
946+
node.name.kind === SyntaxKind.ComputedPropertyName) &&
947+
node.questionToken
948+
) {
945949
result.optional = true;
946950
}
947951

‎packages/typescript-estree/tests/lib/__snapshots__/typescript.ts.snap

+1,541-225
Large diffs are not rendered by default.

0 commit comments

Comments
 (0)
Please sign in to comment.