Skip to content

Commit 0db8056

Browse files
author
Chris Joel
committed
Switching script parser from Acorn to Babel
1 parent 2ea2136 commit 0db8056

9 files changed

+1184
-216
lines changed

package-lock.json

+752-129
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+19-8
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@
44
"description": "",
55
"main": "lib/index.js",
66
"typings": "lib/index.d.ts",
7-
"files": ["lib"],
7+
"files": [
8+
"lib"
9+
],
810
"scripts": {
911
"build": "tsc",
1012
"watch": "tsc -w",
@@ -13,7 +15,23 @@
1315
"author": "The Polymer Authors",
1416
"license": "BSD-3-Clause",
1517
"dependencies": {
18+
"@babel/core": "^7.0.0-beta.46",
19+
"@babel/plugin-syntax-async-generators": "^7.0.0-beta.46",
20+
"@babel/plugin-syntax-dynamic-import": "^7.0.0-beta.46",
21+
"@babel/plugin-syntax-import-meta": "^7.0.0-beta.46",
22+
"@babel/plugin-syntax-object-rest-spread": "^7.0.0-beta.46",
23+
"@babel/template": "^7.0.0-beta.46",
24+
"@babel/traverse": "^7.0.0-beta.46",
25+
"@types/acorn": "^4.0.3",
26+
"@types/babel-types": "^6.25.1",
27+
"@types/babylon": "^6.16.2",
28+
"@types/escodegen": "0.0.6",
29+
"@types/express": "^4.11.1",
30+
"@types/koa": "^2.0.44",
1631
"@types/node": "^9.4.7",
32+
"@types/through2": "^2.0.33",
33+
"@types/vinyl": "^2.0.2",
34+
"@types/vinyl-fs": "^2.4.8",
1735
"acorn": "^5.5.3",
1836
"acorn-import-meta": "^0.2.1",
1937
"dom5": "^3.0.0",
@@ -26,13 +44,6 @@
2644
"vinyl-source-stream": "^2.0.0"
2745
},
2846
"devDependencies": {
29-
"@types/acorn": "^4.0.3",
30-
"@types/escodegen": "0.0.6",
31-
"@types/express": "^4.11.1",
32-
"@types/koa": "^2.0.44",
33-
"@types/through2": "^2.0.33",
34-
"@types/vinyl": "^2.0.2",
35-
"@types/vinyl-fs": "^2.4.8",
3647
"koa-static": "^4.0.2",
3748
"typescript": "^2.7.2"
3849
}

src/custom-types/babel-core.d.ts

+287
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,287 @@
1+
// Babel 7 doesn't have typings yet. These are minimal and temporary
2+
// (and borrowed from
3+
// https://github.com/Polymer/tools/blob/master/packages/build/custom_typings/babel-7.d.ts)
4+
5+
declare module '@babel/core';
6+
7+
declare module '@babel/plugin-syntax-import-meta';
8+
9+
declare module '@babel/plugin-syntax-dynamic-import';
10+
11+
declare module '@babel/helper-plugin-utils';
12+
13+
declare module '@babel/template';
14+
15+
declare module '@babel/traverse' {
16+
import * as t from 'babel-types';
17+
export type Node = t.Node;
18+
19+
export class Scope {
20+
constructor(path: NodePath, parentScope?: Scope);
21+
path: NodePath;
22+
block: Node;
23+
parentBlock: Node;
24+
parent: Scope;
25+
// hub: Hub;
26+
bindings: {[name: string]: Binding;};
27+
28+
/** Generate a unique identifier and add it to the current scope. */
29+
generateDeclaredUidIdentifier(name?: string): t.Identifier;
30+
31+
/** Generate a unique identifier. */
32+
generateUidIdentifier(name?: string): t.Identifier;
33+
34+
/** Generate a unique `_id1` binding. */
35+
generateUid(name?: string): string;
36+
37+
/** Walks the scope tree and gathers **all** bindings. */
38+
getAllBindings(...kinds: string[]): object;
39+
}
40+
41+
export class Binding {
42+
constructor(opts: {
43+
existing: Binding; identifier: t.Identifier; scope: Scope; path: NodePath;
44+
kind: 'var' | 'let' | 'const';
45+
});
46+
identifier: t.Identifier;
47+
scope: Scope;
48+
path: NodePath;
49+
kind: 'var'|'let'|'const'|'module';
50+
referenced: boolean;
51+
references: number;
52+
referencePaths: NodePath[];
53+
constant: boolean;
54+
constantViolations: NodePath[];
55+
}
56+
57+
export class NodePath<T = Node> {
58+
node: T;
59+
scope: Scope;
60+
61+
traverse(visitor: Visitor, state?: any): void;
62+
63+
// ------------------------- replacement -------------------------
64+
/**
65+
* Replace a node with an array of multiple. This method performs the
66+
* following steps:
67+
*
68+
* - Inherit the comments of first provided node with that of the current
69+
* node.
70+
* - Insert the provided nodes after the current node.
71+
* - Remove the current node.
72+
*/
73+
replaceWithMultiple(nodes: Node[]): void;
74+
75+
/**
76+
* Parse a string as an expression and replace the current node with the
77+
* result.
78+
*
79+
* NOTE: This is typically not a good idea to use. Building source strings
80+
* when transforming ASTs is an antipattern and SHOULD NOT be encouraged.
81+
* Even if it's easier to use, your transforms will be extremely brittle.
82+
*/
83+
replaceWithSourceString(replacement: any): void;
84+
85+
/** Replace the current node with another. */
86+
replaceWith(replacement: Node|NodePath): void;
87+
88+
/**
89+
* This method takes an array of statements nodes and then explodes it
90+
* into expressions. This method retains completion records which is
91+
* extremely important to retain original semantics.
92+
*/
93+
replaceExpressionWithStatements(nodes: Node[]): Node;
94+
95+
replaceInline(nodes: Node|Node[]): void;
96+
}
97+
98+
99+
// The Visitor has to be generic because babel binds `this` for each property.
100+
// `this` is usually used in babel plugins to pass plugin state from
101+
// `pre` -> `visitor` -> `post`. An example of this can be seen in the
102+
// official babel handbook:
103+
// https://github.com/thejameskyle/babel-handbook/blob/master/translations/en/plugin-handbook.md#-pre-and-post-in-plugins
104+
export interface Visitor<S = Node> extends VisitNodeObject<Node> {
105+
ArrayExpression?: VisitNode<S, t.ArrayExpression>;
106+
AssignmentExpression?: VisitNode<S, t.AssignmentExpression>;
107+
LVal?: VisitNode<S, t.LVal>;
108+
Expression?: VisitNode<S, t.Expression>;
109+
BinaryExpression?: VisitNode<S, t.BinaryExpression>;
110+
Directive?: VisitNode<S, t.Directive>;
111+
DirectiveLiteral?: VisitNode<S, t.DirectiveLiteral>;
112+
BlockStatement?: VisitNode<S, t.BlockStatement>;
113+
BreakStatement?: VisitNode<S, t.BreakStatement>;
114+
Identifier?: VisitNode<S, t.Identifier>;
115+
CallExpression?: VisitNode<S, t.CallExpression>;
116+
CatchClause?: VisitNode<S, t.CatchClause>;
117+
ConditionalExpression?: VisitNode<S, t.ConditionalExpression>;
118+
ContinueStatement?: VisitNode<S, t.ContinueStatement>;
119+
DebuggerStatement?: VisitNode<S, t.DebuggerStatement>;
120+
DoWhileStatement?: VisitNode<S, t.DoWhileStatement>;
121+
Statement?: VisitNode<S, t.Statement>;
122+
EmptyStatement?: VisitNode<S, t.EmptyStatement>;
123+
ExpressionStatement?: VisitNode<S, t.ExpressionStatement>;
124+
File?: VisitNode<S, t.File>;
125+
Program?: VisitNode<S, t.Program>;
126+
ForInStatement?: VisitNode<S, t.ForInStatement>;
127+
VariableDeclaration?: VisitNode<S, t.VariableDeclaration>;
128+
ForStatement?: VisitNode<S, t.ForStatement>;
129+
FunctionDeclaration?: VisitNode<S, t.FunctionDeclaration>;
130+
FunctionExpression?: VisitNode<S, t.FunctionExpression>;
131+
IfStatement?: VisitNode<S, t.IfStatement>;
132+
LabeledStatement?: VisitNode<S, t.LabeledStatement>;
133+
StringLiteral?: VisitNode<S, t.StringLiteral>;
134+
NumericLiteral?: VisitNode<S, t.NumericLiteral>;
135+
NullLiteral?: VisitNode<S, t.NullLiteral>;
136+
BooleanLiteral?: VisitNode<S, t.BooleanLiteral>;
137+
RegExpLiteral?: VisitNode<S, t.RegExpLiteral>;
138+
LogicalExpression?: VisitNode<S, t.LogicalExpression>;
139+
MemberExpression?: VisitNode<S, t.MemberExpression>;
140+
NewExpression?: VisitNode<S, t.NewExpression>;
141+
ObjectExpression?: VisitNode<S, t.ObjectExpression>;
142+
ObjectMethod?: VisitNode<S, t.ObjectMethod>;
143+
ObjectProperty?: VisitNode<S, t.ObjectProperty>;
144+
RestElement?: VisitNode<S, t.RestElement>;
145+
ReturnStatement?: VisitNode<S, t.ReturnStatement>;
146+
SequenceExpression?: VisitNode<S, t.SequenceExpression>;
147+
SwitchCase?: VisitNode<S, t.SwitchCase>;
148+
SwitchStatement?: VisitNode<S, t.SwitchStatement>;
149+
ThisExpression?: VisitNode<S, t.ThisExpression>;
150+
ThrowStatement?: VisitNode<S, t.ThrowStatement>;
151+
TryStatement?: VisitNode<S, t.TryStatement>;
152+
UnaryExpression?: VisitNode<S, t.UnaryExpression>;
153+
UpdateExpression?: VisitNode<S, t.UpdateExpression>;
154+
VariableDeclarator?: VisitNode<S, t.VariableDeclarator>;
155+
WhileStatement?: VisitNode<S, t.WhileStatement>;
156+
WithStatement?: VisitNode<S, t.WithStatement>;
157+
AssignmentPattern?: VisitNode<S, t.AssignmentPattern>;
158+
ArrayPattern?: VisitNode<S, t.ArrayPattern>;
159+
ArrowFunctionExpression?: VisitNode<S, t.ArrowFunctionExpression>;
160+
ClassBody?: VisitNode<S, t.ClassBody>;
161+
ClassDeclaration?: VisitNode<S, t.ClassDeclaration>;
162+
ClassExpression?: VisitNode<S, t.ClassExpression>;
163+
ExportAllDeclaration?: VisitNode<S, t.ExportAllDeclaration>;
164+
ExportDefaultDeclaration?: VisitNode<S, t.ExportDefaultDeclaration>;
165+
ExportNamedDeclaration?: VisitNode<S, t.ExportNamedDeclaration>;
166+
Declaration?: VisitNode<S, t.Declaration>;
167+
ExportSpecifier?: VisitNode<S, t.ExportSpecifier>;
168+
ForOfStatement?: VisitNode<S, t.ForOfStatement>;
169+
ImportDeclaration?: VisitNode<S, t.ImportDeclaration>;
170+
ImportDefaultSpecifier?: VisitNode<S, t.ImportDefaultSpecifier>;
171+
ImportNamespaceSpecifier?: VisitNode<S, t.ImportNamespaceSpecifier>;
172+
ImportSpecifier?: VisitNode<S, t.ImportSpecifier>;
173+
MetaProperty?: VisitNode<S, t.MetaProperty>;
174+
ClassMethod?: VisitNode<S, t.ClassMethod>;
175+
ObjectPattern?: VisitNode<S, t.ObjectPattern>;
176+
SpreadElement?: VisitNode<S, t.SpreadElement>;
177+
Super?: VisitNode<S, t.Super>;
178+
TaggedTemplateExpression?: VisitNode<S, t.TaggedTemplateExpression>;
179+
TemplateLiteral?: VisitNode<S, t.TemplateLiteral>;
180+
TemplateElement?: VisitNode<S, t.TemplateElement>;
181+
YieldExpression?: VisitNode<S, t.YieldExpression>;
182+
AnyTypeAnnotation?: VisitNode<S, t.AnyTypeAnnotation>;
183+
ArrayTypeAnnotation?: VisitNode<S, t.ArrayTypeAnnotation>;
184+
BooleanTypeAnnotation?: VisitNode<S, t.BooleanTypeAnnotation>;
185+
BooleanLiteralTypeAnnotation?: VisitNode<S, t.BooleanLiteralTypeAnnotation>;
186+
NullLiteralTypeAnnotation?: VisitNode<S, t.NullLiteralTypeAnnotation>;
187+
ClassImplements?: VisitNode<S, t.ClassImplements>;
188+
ClassProperty?: VisitNode<S, t.ClassProperty>;
189+
DeclareClass?: VisitNode<S, t.DeclareClass>;
190+
DeclareFunction?: VisitNode<S, t.DeclareFunction>;
191+
DeclareInterface?: VisitNode<S, t.DeclareInterface>;
192+
DeclareModule?: VisitNode<S, t.DeclareModule>;
193+
DeclareTypeAlias?: VisitNode<S, t.DeclareTypeAlias>;
194+
DeclareVariable?: VisitNode<S, t.DeclareVariable>;
195+
ExistentialTypeParam?: VisitNode<S, t.ExistentialTypeParam>;
196+
FunctionTypeAnnotation?: VisitNode<S, t.FunctionTypeAnnotation>;
197+
FunctionTypeParam?: VisitNode<S, t.FunctionTypeParam>;
198+
GenericTypeAnnotation?: VisitNode<S, t.GenericTypeAnnotation>;
199+
InterfaceExtends?: VisitNode<S, t.InterfaceExtends>;
200+
InterfaceDeclaration?: VisitNode<S, t.InterfaceDeclaration>;
201+
IntersectionTypeAnnotation?: VisitNode<S, t.IntersectionTypeAnnotation>;
202+
MixedTypeAnnotation?: VisitNode<S, t.MixedTypeAnnotation>;
203+
NullableTypeAnnotation?: VisitNode<S, t.NullableTypeAnnotation>;
204+
NumericLiteralTypeAnnotation?: VisitNode<S, t.NumericLiteralTypeAnnotation>;
205+
NumberTypeAnnotation?: VisitNode<S, t.NumberTypeAnnotation>;
206+
StringLiteralTypeAnnotation?: VisitNode<S, t.StringLiteralTypeAnnotation>;
207+
StringTypeAnnotation?: VisitNode<S, t.StringTypeAnnotation>;
208+
ThisTypeAnnotation?: VisitNode<S, t.ThisTypeAnnotation>;
209+
TupleTypeAnnotation?: VisitNode<S, t.TupleTypeAnnotation>;
210+
TypeofTypeAnnotation?: VisitNode<S, t.TypeofTypeAnnotation>;
211+
TypeAlias?: VisitNode<S, t.TypeAlias>;
212+
TypeAnnotation?: VisitNode<S, t.TypeAnnotation>;
213+
TypeCastExpression?: VisitNode<S, t.TypeCastExpression>;
214+
TypeParameterDeclaration?: VisitNode<S, t.TypeParameterDeclaration>;
215+
TypeParameterInstantiation?: VisitNode<S, t.TypeParameterInstantiation>;
216+
ObjectTypeAnnotation?: VisitNode<S, t.ObjectTypeAnnotation>;
217+
ObjectTypeCallProperty?: VisitNode<S, t.ObjectTypeCallProperty>;
218+
ObjectTypeIndexer?: VisitNode<S, t.ObjectTypeIndexer>;
219+
ObjectTypeProperty?: VisitNode<S, t.ObjectTypeProperty>;
220+
QualifiedTypeIdentifier?: VisitNode<S, t.QualifiedTypeIdentifier>;
221+
UnionTypeAnnotation?: VisitNode<S, t.UnionTypeAnnotation>;
222+
VoidTypeAnnotation?: VisitNode<S, t.VoidTypeAnnotation>;
223+
JSXAttribute?: VisitNode<S, t.JSXAttribute>;
224+
JSXIdentifier?: VisitNode<S, t.JSXIdentifier>;
225+
JSXNamespacedName?: VisitNode<S, t.JSXNamespacedName>;
226+
JSXElement?: VisitNode<S, t.JSXElement>;
227+
JSXExpressionContainer?: VisitNode<S, t.JSXExpressionContainer>;
228+
JSXClosingElement?: VisitNode<S, t.JSXClosingElement>;
229+
JSXMemberExpression?: VisitNode<S, t.JSXMemberExpression>;
230+
JSXOpeningElement?: VisitNode<S, t.JSXOpeningElement>;
231+
JSXEmptyExpression?: VisitNode<S, t.JSXEmptyExpression>;
232+
JSXSpreadAttribute?: VisitNode<S, t.JSXSpreadAttribute>;
233+
JSXText?: VisitNode<S, t.JSXText>;
234+
Noop?: VisitNode<S, t.Noop>;
235+
ParenthesizedExpression?: VisitNode<S, t.ParenthesizedExpression>;
236+
AwaitExpression?: VisitNode<S, t.AwaitExpression>;
237+
BindExpression?: VisitNode<S, t.BindExpression>;
238+
Decorator?: VisitNode<S, t.Decorator>;
239+
DoExpression?: VisitNode<S, t.DoExpression>;
240+
ExportDefaultSpecifier?: VisitNode<S, t.ExportDefaultSpecifier>;
241+
ExportNamespaceSpecifier?: VisitNode<S, t.ExportNamespaceSpecifier>;
242+
RestProperty?: VisitNode<S, t.RestProperty>;
243+
SpreadProperty?: VisitNode<S, t.SpreadProperty>;
244+
Binary?: VisitNode<S, t.Binary>;
245+
Scopable?: VisitNode<S, t.Scopable>;
246+
BlockParent?: VisitNode<S, t.BlockParent>;
247+
Block?: VisitNode<S, t.Block>;
248+
Terminatorless?: VisitNode<S, t.Terminatorless>;
249+
CompletionStatement?: VisitNode<S, t.CompletionStatement>;
250+
Conditional?: VisitNode<S, t.Conditional>;
251+
Loop?: VisitNode<S, t.Loop>;
252+
While?: VisitNode<S, t.While>;
253+
ExpressionWrapper?: VisitNode<S, t.ExpressionWrapper>;
254+
For?: VisitNode<S, t.For>;
255+
ForXStatement?: VisitNode<S, t.ForXStatement>;
256+
Function?: VisitNode<S, t.Function>;
257+
FunctionParent?: VisitNode<S, t.FunctionParent>;
258+
Pureish?: VisitNode<S, t.Pureish>;
259+
Literal?: VisitNode<S, t.Literal>;
260+
Immutable?: VisitNode<S, t.Immutable>;
261+
UserWhitespacable?: VisitNode<S, t.UserWhitespacable>;
262+
Method?: VisitNode<S, t.Method>;
263+
ObjectMember?: VisitNode<S, t.ObjectMember>;
264+
Property?: VisitNode<S, t.Property>;
265+
UnaryLike?: VisitNode<S, t.UnaryLike>;
266+
Pattern?: VisitNode<S, t.Pattern>;
267+
Class?: VisitNode<S, t.Class>;
268+
ModuleDeclaration?: VisitNode<S, t.ModuleDeclaration>;
269+
ExportDeclaration?: VisitNode<S, t.ExportDeclaration>;
270+
ModuleSpecifier?: VisitNode<S, t.ModuleSpecifier>;
271+
Flow?: VisitNode<S, t.Flow>;
272+
FlowBaseAnnotation?: VisitNode<S, t.FlowBaseAnnotation>;
273+
FlowDeclaration?: VisitNode<S, t.FlowDeclaration>;
274+
JSX?: VisitNode<S, t.JSX>;
275+
Scope?: VisitNode<S, t.Scopable>;
276+
}
277+
278+
export type VisitNode<T, P> = VisitNodeFunction<T, P>|VisitNodeObject<T>;
279+
280+
export type VisitNodeFunction<T, P> =
281+
(this: T, path: NodePath<P>, state: any) => void;
282+
283+
export interface VisitNodeObject<T> {
284+
enter?(path: NodePath<T>, state: any): void;
285+
exit?(path: NodePath<T>, state: any): void;
286+
}
287+
}

src/html-module-specifier-transform.ts

+7-6
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ import {getFileContents} from './file.js';
2121
import {ScriptView} from './script-view.js';
2222
import {transformStream} from './stream.js';
2323

24-
2524
export const htmlModuleSpecifierTransform = (): Transform =>
2625
transformStream<File, File>(async(file: File): Promise<File> => {
2726
if (/.html$/.test(file.path)) {
@@ -78,13 +77,15 @@ export const transformSpecifiersInHtmlString = (htmlString: string): string => {
7877

7978
export const transformSpecifiersInJsString = (jsString: string): string => {
8079
const scriptView = ScriptView.fromSourceString(jsString);
81-
const {importDeclarations} = scriptView;
80+
const {specifierNodes} = scriptView;
81+
82+
for (const specifier of specifierNodes) {
83+
const {node} = specifier;
84+
const {source} = node as any;
85+
const {value} = source;
8286

83-
for (const declaration of importDeclarations) {
84-
const {source} = declaration;
85-
if (/.html$/.test(source.value)) {
87+
if (/.html$/.test(value)) {
8688
source.value = source.value.replace(/\.html$/, '.html.js');
87-
source.raw = source.raw.replace(/\.html$/, '.html.js');
8889
}
8990
}
9091

0 commit comments

Comments
 (0)