Skip to content

Commit 73bdf95

Browse files
committed
comments, round 3
1 parent 93fc9df commit 73bdf95

File tree

3 files changed

+90
-102
lines changed

3 files changed

+90
-102
lines changed

packages/webpack/src/loader.ts

Lines changed: 39 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,6 @@ import * as ts from 'typescript';
33
import {AotPlugin} from './plugin';
44
import {TypeScriptFileRefactor} from './refactor';
55

6-
// TODO: move all this to ast-tools.
7-
function _findNodes(sourceFile: ts.SourceFile, node: ts.Node, kind: ts.SyntaxKind,
8-
keepGoing = false): ts.Node[] {
9-
if (node.kind == kind && !keepGoing) {
10-
return [node];
11-
}
12-
13-
return node.getChildren(sourceFile).reduce((result, n) => {
14-
return result.concat(_findNodes(sourceFile, n, kind, keepGoing));
15-
}, node.kind == kind ? [node] : []);
16-
}
17-
186
function _getContentOfKeyLiteral(source: ts.SourceFile, node: ts.Node): string {
197
if (node.kind == ts.SyntaxKind.Identifier) {
208
return (node as ts.Identifier).text;
@@ -27,7 +15,7 @@ function _getContentOfKeyLiteral(source: ts.SourceFile, node: ts.Node): string {
2715

2816
function _removeDecorators(refactor: TypeScriptFileRefactor) {
2917
// Find all decorators.
30-
_findNodes(refactor.sourceFile, refactor.sourceFile, ts.SyntaxKind.Decorator, false)
18+
refactor.findAstNodes(refactor.sourceFile, ts.SyntaxKind.Decorator)
3119
.forEach(d => refactor.removeNode(d));
3220
}
3321

@@ -49,7 +37,7 @@ function _replaceBootstrap(plugin: AotPlugin, refactor: TypeScriptFileRefactor)
4937
const relativeNgFactoryPath = path.relative(dirName, fullEntryModulePath);
5038
const ngFactoryPath = './' + relativeNgFactoryPath.replace(/\\/g, '/');
5139

52-
const allCalls = _findNodes(refactor.sourceFile, refactor.sourceFile,
40+
const allCalls = refactor.findAstNodes(refactor.sourceFile,
5341
ts.SyntaxKind.CallExpression, true) as ts.CallExpression[];
5442

5543
const bootstraps = allCalls
@@ -62,12 +50,13 @@ function _replaceBootstrap(plugin: AotPlugin, refactor: TypeScriptFileRefactor)
6250

6351
const calls: ts.CallExpression[] = bootstraps
6452
.reduce((previous, access) => {
65-
return previous.concat(
66-
_findNodes(refactor.sourceFile, access, ts.SyntaxKind.CallExpression, true));
53+
const expressions
54+
= refactor.findAstNodes(access, ts.SyntaxKind.CallExpression, true) as ts.CallExpression[];
55+
return previous.concat(expressions);
6756
}, [])
68-
.filter(call => {
57+
.filter((call: ts.CallExpression) => {
6958
return call.expression.kind == ts.SyntaxKind.Identifier
70-
&& call.expression.text == 'platformBrowserDynamic';
59+
&& (call.expression as ts.Identifier).text == 'platformBrowserDynamic';
7160
});
7261

7362
if (calls.length == 0) {
@@ -98,40 +87,40 @@ function _replaceResources(refactor: TypeScriptFileRefactor) {
9887
const sourceFile = refactor.sourceFile;
9988

10089
// Find all object literals.
101-
_findNodes(sourceFile, sourceFile, ts.SyntaxKind.ObjectLiteralExpression, true)
102-
// Get all their property assignments.
103-
.map(node => _findNodes(sourceFile, node, ts.SyntaxKind.PropertyAssignment))
104-
// Flatten into a single array (from an array of array<property assignments>).
105-
.reduce((prev, curr) => curr ? prev.concat(curr) : prev, [])
106-
// Remove every property assignment that aren't 'loadChildren'.
107-
.filter((node: ts.PropertyAssignment) => {
108-
const key = _getContentOfKeyLiteral(sourceFile, node.name);
109-
if (!key) {
110-
// key is an expression, can't do anything.
111-
return false;
112-
}
113-
return key == 'templateUrl' || key == 'styleUrls';
114-
})
115-
// Get the full text of the initializer.
116-
.forEach((node: ts.PropertyAssignment) => {
117-
const key = _getContentOfKeyLiteral(sourceFile, node.name);
118-
119-
if (key == 'templateUrl') {
120-
refactor.replaceNode(node, `template: require(${node.initializer.getFullText(sourceFile)})`);
121-
} else if (key == 'styleUrls') {
122-
const arr: ts.ArrayLiteralExpression[] =
123-
<ts.ArrayLiteralExpression[]>_findNodes(sourceFile, node,
124-
ts.SyntaxKind.ArrayLiteralExpression, false);
125-
if (!arr || arr.length == 0 || arr[0].elements.length == 0) {
126-
return;
90+
refactor.findAstNodes(sourceFile, ts.SyntaxKind.ObjectLiteralExpression, true)
91+
// Get all their property assignments.
92+
.map(node => refactor.findAstNodes(node, ts.SyntaxKind.PropertyAssignment))
93+
// Flatten into a single array (from an array of array<property assignments>).
94+
.reduce((prev, curr) => curr ? prev.concat(curr) : prev, [])
95+
// Remove every property assignment that aren't 'loadChildren'.
96+
.filter((node: ts.PropertyAssignment) => {
97+
const key = _getContentOfKeyLiteral(sourceFile, node.name);
98+
if (!key) {
99+
// key is an expression, can't do anything.
100+
return false;
127101
}
102+
return key == 'templateUrl' || key == 'styleUrls';
103+
})
104+
// Get the full text of the initializer.
105+
.forEach((node: ts.PropertyAssignment) => {
106+
const key = _getContentOfKeyLiteral(sourceFile, node.name);
107+
108+
if (key == 'templateUrl') {
109+
refactor.replaceNode(node,
110+
`template: require(${node.initializer.getFullText(sourceFile)})`);
111+
} else if (key == 'styleUrls') {
112+
const arr = <ts.ArrayLiteralExpression[]>(
113+
refactor.findAstNodes(node, ts.SyntaxKind.ArrayLiteralExpression, false));
114+
if (!arr || arr.length == 0 || arr[0].elements.length == 0) {
115+
return;
116+
}
128117

129-
const initializer = arr[0].elements.map((element: ts.Expression) => {
130-
return element.getFullText(sourceFile);
131-
});
132-
refactor.replaceNode(node, `styles: [require(${initializer.join('), require(')})]`);
133-
}
134-
});
118+
const initializer = arr[0].elements.map((element: ts.Expression) => {
119+
return element.getFullText(sourceFile);
120+
});
121+
refactor.replaceNode(node, `styles: [require(${initializer.join('), require(')})]`);
122+
}
123+
});
135124
}
136125

137126

packages/webpack/src/paths-plugin.ts

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ export class PathsPlugin implements Tapable {
103103
});
104104
}
105105

106-
apply(resolver: ResolverPlugin) {
106+
apply(resolver: ResolverPlugin): void {
107107
let { baseUrl } = this._compilerOptions;
108108

109109
if (baseUrl) {
@@ -115,7 +115,7 @@ export class PathsPlugin implements Tapable {
115115
});
116116
}
117117

118-
resolve(resolver: ResolverPlugin, mapping: any, request: any, callback: Callback<any>) {
118+
resolve(resolver: ResolverPlugin, mapping: any, request: any, callback: Callback<any>): any {
119119
let innerRequest = getInnerRequest(resolver, request);
120120
if (!innerRequest) {
121121
return callback();
@@ -125,7 +125,6 @@ export class PathsPlugin implements Tapable {
125125
if (!match) {
126126
return callback();
127127
}
128-
console.log(3, innerRequest);
129128

130129
let newRequestStr = mapping.target;
131130
if (!mapping.onlyModule) {
@@ -157,7 +156,7 @@ export class PathsPlugin implements Tapable {
157156
);
158157
}
159158

160-
createPlugin(resolver: ResolverPlugin, mapping: any) {
159+
createPlugin(resolver: ResolverPlugin, mapping: any): any {
161160
return (request: any, callback: Callback<any>) => {
162161
try {
163162
this.resolve(resolver, mapping, request, callback);

packages/webpack/src/refactor.ts

Lines changed: 48 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -5,51 +5,6 @@ import {SourceMapConsumer, SourceMapGenerator} from 'source-map';
55
const MagicString = require('magic-string');
66

77

8-
9-
/**
10-
* Find all nodes from the AST in the subtree of node of SyntaxKind kind.
11-
* @param node
12-
* @param kind
13-
* @param recursive Whether to go in matched nodes to keep matching.
14-
* @param max The maximum number of items to return.
15-
* @return all nodes of kind, or [] if none is found
16-
*/
17-
function _findNodes(node: ts.Node, kind: ts.SyntaxKind,
18-
recursive = false,
19-
max: number = Infinity): ts.Node[] {
20-
if (!node || max == 0) {
21-
return [];
22-
}
23-
24-
let arr: ts.Node[] = [];
25-
if (node.kind === kind) {
26-
// If we're not recursively looking for children, stop here.
27-
if (!recursive) {
28-
return [node];
29-
}
30-
31-
arr.push(node);
32-
max--;
33-
}
34-
if (max > 0) {
35-
for (const child of node.getChildren()) {
36-
_findNodes(child, kind, recursive, max).forEach((node: ts.Node) => {
37-
if (max > 0) {
38-
arr.push(node);
39-
}
40-
max--;
41-
});
42-
43-
if (max <= 0) {
44-
break;
45-
}
46-
}
47-
}
48-
return arr;
49-
}
50-
51-
52-
538
export interface TranspileOutput {
549
outputText: string;
5510
sourceMap: any;
@@ -89,13 +44,58 @@ export class TypeScriptFileRefactor {
8944
.concat(this._program.getDeclarationDiagnostics(this._sourceFile));
9045
}
9146

92-
appendAfter(node: ts.Node, text: string) {
47+
/**
48+
* Find all nodes from the AST in the subtree of node of SyntaxKind kind.
49+
* @param node
50+
* @param kind
51+
* @param recursive Whether to go in matched nodes to keep matching.
52+
* @param max The maximum number of items to return.
53+
* @return all nodes of kind, or [] if none is found
54+
*/
55+
findAstNodes(node: ts.Node,
56+
kind: ts.SyntaxKind,
57+
recursive = false,
58+
max: number = Infinity): ts.Node[] {
59+
if (!node || max == 0) {
60+
return [];
61+
}
62+
63+
let arr: ts.Node[] = [];
64+
if (node.kind === kind) {
65+
// If we're not recursively looking for children, stop here.
66+
if (!recursive) {
67+
return [node];
68+
}
69+
70+
arr.push(node);
71+
max--;
72+
}
73+
74+
if (max > 0) {
75+
for (const child of node.getChildren(this._sourceFile)) {
76+
this.findAstNodes(child, kind, recursive, max)
77+
.forEach((node: ts.Node) => {
78+
if (max > 0) {
79+
arr.push(node);
80+
}
81+
max--;
82+
});
83+
84+
if (max <= 0) {
85+
break;
86+
}
87+
}
88+
}
89+
return arr;
90+
}
91+
92+
appendAfter(node: ts.Node, text: string): void {
9393
this._sourceString.insertRight(node.getEnd(), text);
9494
}
9595

96-
insertImport(symbolName: string, modulePath: string) {
96+
insertImport(symbolName: string, modulePath: string): void {
9797
// Find all imports.
98-
const allImports = _findNodes(this._sourceFile, ts.SyntaxKind.ImportDeclaration);
98+
const allImports = this.findAstNodes(this._sourceFile, ts.SyntaxKind.ImportDeclaration);
9999
const maybeImports = allImports
100100
.filter((node: ts.ImportDeclaration) => {
101101
// Filter all imports that do not match the modulePath.

0 commit comments

Comments
 (0)