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

Commit e450058

Browse files
loganfsmythjasonLaster
authored andcommitted
Enable original-file Typescript parsing via Babylon. (#5778)
1 parent 0a56dea commit e450058

File tree

6 files changed

+395
-4
lines changed

6 files changed

+395
-4
lines changed

src/workers/parser/getScopes/visitor.js

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -570,7 +570,28 @@ const scopeCollectionVisitor = {
570570
};
571571
}
572572
});
573-
} else if (t.isIdentifier(node) && t.isReferenced(node, parentNode)) {
573+
} else if (t.isTSEnumDeclaration(node)) {
574+
state.scope.bindings[node.id.name] = {
575+
type: "const",
576+
refs: [
577+
{
578+
type: "decl",
579+
start: fromBabelLocation(node.id.loc.start, state.sourceId),
580+
end: fromBabelLocation(node.id.loc.end, state.sourceId),
581+
declaration: {
582+
start: fromBabelLocation(node.loc.start, state.sourceId),
583+
end: fromBabelLocation(node.loc.end, state.sourceId)
584+
}
585+
}
586+
]
587+
};
588+
} else if (
589+
t.isIdentifier(node) &&
590+
t.isReferenced(node, parentNode) &&
591+
// Babel doesn't cover this in 'isReferenced' yet, but it should
592+
// eventually.
593+
!t.isTSEnumMember(parentNode, { id: node })
594+
) {
574595
let freeVariables = state.freeVariables.get(node.name);
575596
if (!freeVariables) {
576597
freeVariables = [];

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

Lines changed: 338 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12068,6 +12068,344 @@ Array [
1206812068
]
1206912069
`;
1207012070

12071+
exports[`getScopes finds scope bindings in a typescript file at line 3 column 0 1`] = `
12072+
Array [
12073+
Object {
12074+
"bindings": Object {
12075+
"Color": Object {
12076+
"refs": Array [
12077+
Object {
12078+
"declaration": Object {
12079+
"end": Object {
12080+
"column": 29,
12081+
"line": 2,
12082+
"sourceId": "scopes/ts-sample/originalSource-1",
12083+
},
12084+
"start": Object {
12085+
"column": 0,
12086+
"line": 2,
12087+
"sourceId": "scopes/ts-sample/originalSource-1",
12088+
},
12089+
},
12090+
"end": Object {
12091+
"column": 10,
12092+
"line": 2,
12093+
"sourceId": "scopes/ts-sample/originalSource-1",
12094+
},
12095+
"start": Object {
12096+
"column": 5,
12097+
"line": 2,
12098+
"sourceId": "scopes/ts-sample/originalSource-1",
12099+
},
12100+
"type": "decl",
12101+
},
12102+
Object {
12103+
"end": Object {
12104+
"column": 10,
12105+
"line": 2,
12106+
"sourceId": "scopes/ts-sample/originalSource-1",
12107+
},
12108+
"meta": null,
12109+
"start": Object {
12110+
"column": 5,
12111+
"line": 2,
12112+
"sourceId": "scopes/ts-sample/originalSource-1",
12113+
},
12114+
"type": "ref",
12115+
},
12116+
],
12117+
"type": "const",
12118+
},
12119+
"Example": Object {
12120+
"refs": Array [
12121+
Object {
12122+
"declaration": Object {
12123+
"end": Object {
12124+
"column": 1,
12125+
"line": 8,
12126+
"sourceId": "scopes/ts-sample/originalSource-1",
12127+
},
12128+
"start": Object {
12129+
"column": 0,
12130+
"line": 4,
12131+
"sourceId": "scopes/ts-sample/originalSource-1",
12132+
},
12133+
},
12134+
"end": Object {
12135+
"column": 13,
12136+
"line": 4,
12137+
"sourceId": "scopes/ts-sample/originalSource-1",
12138+
},
12139+
"start": Object {
12140+
"column": 6,
12141+
"line": 4,
12142+
"sourceId": "scopes/ts-sample/originalSource-1",
12143+
},
12144+
"type": "decl",
12145+
},
12146+
],
12147+
"type": "let",
12148+
},
12149+
},
12150+
"displayName": "Lexical Global",
12151+
"end": Object {
12152+
"column": 0,
12153+
"line": 9,
12154+
"sourceId": "scopes/ts-sample/originalSource-1",
12155+
},
12156+
"start": Object {
12157+
"column": 0,
12158+
"line": 1,
12159+
"sourceId": "scopes/ts-sample/originalSource-1",
12160+
},
12161+
"type": "block",
12162+
},
12163+
Object {
12164+
"bindings": Object {
12165+
"Error": Object {
12166+
"refs": Array [
12167+
Object {
12168+
"end": Object {
12169+
"column": 19,
12170+
"line": 6,
12171+
"sourceId": "scopes/ts-sample/originalSource-1",
12172+
},
12173+
"meta": null,
12174+
"start": Object {
12175+
"column": 14,
12176+
"line": 6,
12177+
"sourceId": "scopes/ts-sample/originalSource-1",
12178+
},
12179+
"type": "ref",
12180+
},
12181+
],
12182+
"type": "global",
12183+
},
12184+
"this": Object {
12185+
"refs": Array [],
12186+
"type": "implicit",
12187+
},
12188+
},
12189+
"displayName": "Global",
12190+
"end": Object {
12191+
"column": 0,
12192+
"line": 9,
12193+
"sourceId": "scopes/ts-sample/originalSource-1",
12194+
},
12195+
"start": Object {
12196+
"column": 0,
12197+
"line": 1,
12198+
"sourceId": "scopes/ts-sample/originalSource-1",
12199+
},
12200+
"type": "object",
12201+
},
12202+
]
12203+
`;
12204+
12205+
exports[`getScopes finds scope bindings in a typescript file at line 6 column 4 1`] = `
12206+
Array [
12207+
Object {
12208+
"bindings": Object {
12209+
"arguments": Object {
12210+
"refs": Array [],
12211+
"type": "implicit",
12212+
},
12213+
"this": Object {
12214+
"refs": Array [],
12215+
"type": "implicit",
12216+
},
12217+
},
12218+
"displayName": "method",
12219+
"end": Object {
12220+
"column": 3,
12221+
"line": 7,
12222+
"sourceId": "scopes/ts-sample/originalSource-1",
12223+
},
12224+
"start": Object {
12225+
"column": 2,
12226+
"line": 5,
12227+
"sourceId": "scopes/ts-sample/originalSource-1",
12228+
},
12229+
"type": "function",
12230+
},
12231+
Object {
12232+
"bindings": Object {
12233+
"Example": Object {
12234+
"refs": Array [
12235+
Object {
12236+
"declaration": Object {
12237+
"end": Object {
12238+
"column": 1,
12239+
"line": 8,
12240+
"sourceId": "scopes/ts-sample/originalSource-1",
12241+
},
12242+
"start": Object {
12243+
"column": 0,
12244+
"line": 4,
12245+
"sourceId": "scopes/ts-sample/originalSource-1",
12246+
},
12247+
},
12248+
"end": Object {
12249+
"column": 13,
12250+
"line": 4,
12251+
"sourceId": "scopes/ts-sample/originalSource-1",
12252+
},
12253+
"start": Object {
12254+
"column": 6,
12255+
"line": 4,
12256+
"sourceId": "scopes/ts-sample/originalSource-1",
12257+
},
12258+
"type": "decl",
12259+
},
12260+
],
12261+
"type": "const",
12262+
},
12263+
},
12264+
"displayName": "Class",
12265+
"end": Object {
12266+
"column": 1,
12267+
"line": 8,
12268+
"sourceId": "scopes/ts-sample/originalSource-1",
12269+
},
12270+
"start": Object {
12271+
"column": 0,
12272+
"line": 4,
12273+
"sourceId": "scopes/ts-sample/originalSource-1",
12274+
},
12275+
"type": "block",
12276+
},
12277+
Object {
12278+
"bindings": Object {
12279+
"Color": Object {
12280+
"refs": Array [
12281+
Object {
12282+
"declaration": Object {
12283+
"end": Object {
12284+
"column": 29,
12285+
"line": 2,
12286+
"sourceId": "scopes/ts-sample/originalSource-1",
12287+
},
12288+
"start": Object {
12289+
"column": 0,
12290+
"line": 2,
12291+
"sourceId": "scopes/ts-sample/originalSource-1",
12292+
},
12293+
},
12294+
"end": Object {
12295+
"column": 10,
12296+
"line": 2,
12297+
"sourceId": "scopes/ts-sample/originalSource-1",
12298+
},
12299+
"start": Object {
12300+
"column": 5,
12301+
"line": 2,
12302+
"sourceId": "scopes/ts-sample/originalSource-1",
12303+
},
12304+
"type": "decl",
12305+
},
12306+
Object {
12307+
"end": Object {
12308+
"column": 10,
12309+
"line": 2,
12310+
"sourceId": "scopes/ts-sample/originalSource-1",
12311+
},
12312+
"meta": null,
12313+
"start": Object {
12314+
"column": 5,
12315+
"line": 2,
12316+
"sourceId": "scopes/ts-sample/originalSource-1",
12317+
},
12318+
"type": "ref",
12319+
},
12320+
],
12321+
"type": "const",
12322+
},
12323+
"Example": Object {
12324+
"refs": Array [
12325+
Object {
12326+
"declaration": Object {
12327+
"end": Object {
12328+
"column": 1,
12329+
"line": 8,
12330+
"sourceId": "scopes/ts-sample/originalSource-1",
12331+
},
12332+
"start": Object {
12333+
"column": 0,
12334+
"line": 4,
12335+
"sourceId": "scopes/ts-sample/originalSource-1",
12336+
},
12337+
},
12338+
"end": Object {
12339+
"column": 13,
12340+
"line": 4,
12341+
"sourceId": "scopes/ts-sample/originalSource-1",
12342+
},
12343+
"start": Object {
12344+
"column": 6,
12345+
"line": 4,
12346+
"sourceId": "scopes/ts-sample/originalSource-1",
12347+
},
12348+
"type": "decl",
12349+
},
12350+
],
12351+
"type": "let",
12352+
},
12353+
},
12354+
"displayName": "Lexical Global",
12355+
"end": Object {
12356+
"column": 0,
12357+
"line": 9,
12358+
"sourceId": "scopes/ts-sample/originalSource-1",
12359+
},
12360+
"start": Object {
12361+
"column": 0,
12362+
"line": 1,
12363+
"sourceId": "scopes/ts-sample/originalSource-1",
12364+
},
12365+
"type": "block",
12366+
},
12367+
Object {
12368+
"bindings": Object {
12369+
"Error": Object {
12370+
"refs": Array [
12371+
Object {
12372+
"end": Object {
12373+
"column": 19,
12374+
"line": 6,
12375+
"sourceId": "scopes/ts-sample/originalSource-1",
12376+
},
12377+
"meta": null,
12378+
"start": Object {
12379+
"column": 14,
12380+
"line": 6,
12381+
"sourceId": "scopes/ts-sample/originalSource-1",
12382+
},
12383+
"type": "ref",
12384+
},
12385+
],
12386+
"type": "global",
12387+
},
12388+
"this": Object {
12389+
"refs": Array [],
12390+
"type": "implicit",
12391+
},
12392+
},
12393+
"displayName": "Global",
12394+
"end": Object {
12395+
"column": 0,
12396+
"line": 9,
12397+
"sourceId": "scopes/ts-sample/originalSource-1",
12398+
},
12399+
"start": Object {
12400+
"column": 0,
12401+
"line": 1,
12402+
"sourceId": "scopes/ts-sample/originalSource-1",
12403+
},
12404+
"type": "object",
12405+
},
12406+
]
12407+
`;
12408+
1207112409
exports[`getScopes finds scope bindings with expression metadata at line 2 column 0 1`] = `
1207212410
Array [
1207312411
Object {
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
2+
enum Color {Red, Green, Blue}
3+
4+
class Example<T> {
5+
method(): never {
6+
throw new Error();
7+
}
8+
}

0 commit comments

Comments
 (0)