UNPKG

10.7 kBJavaScriptView Raw
1"use strict";
2var __generator = (this && this.__generator) || function (thisArg, body) {
3 var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
4 return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
5 function verb(n) { return function (v) { return step([n, v]); }; }
6 function step(op) {
7 if (f) throw new TypeError("Generator is already executing.");
8 while (_) try {
9 if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
10 if (y = 0, t) op = [op[0] & 2, t.value];
11 switch (op[0]) {
12 case 0: case 1: t = op; break;
13 case 4: _.label++; return { value: op[1], done: false };
14 case 5: _.label++; y = op[1]; op = [0]; continue;
15 case 7: op = _.ops.pop(); _.trys.pop(); continue;
16 default:
17 if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
18 if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
19 if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
20 if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
21 if (t[2]) _.ops.pop();
22 _.trys.pop(); continue;
23 }
24 op = body.call(thisArg, _);
25 } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
26 if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
27 }
28};
29var __values = (this && this.__values) || function(o) {
30 var s = typeof Symbol === "function" && Symbol.iterator, m = s && o[s], i = 0;
31 if (m) return m.call(o);
32 if (o && typeof o.length === "number") return {
33 next: function () {
34 if (o && i >= o.length) o = void 0;
35 return { value: o && o[i++], done: !o };
36 }
37 };
38 throw new TypeError(s ? "Object is not iterable." : "Symbol.iterator is not defined.");
39};
40var __read = (this && this.__read) || function (o, n) {
41 var m = typeof Symbol === "function" && o[Symbol.iterator];
42 if (!m) return o;
43 var i = m.call(o), r, ar = [], e;
44 try {
45 while ((n === void 0 || n-- > 0) && !(r = i.next()).done) ar.push(r.value);
46 }
47 catch (error) { e = { error: error }; }
48 finally {
49 try {
50 if (r && !r.done && (m = i["return"])) m.call(i);
51 }
52 finally { if (e) throw e.error; }
53 }
54 return ar;
55};
56exports.__esModule = true;
57exports.visitDecorator = exports.visitNgModules = exports.visitComponents = exports.visitNgModuleExports = exports.visitNgModuleImports = exports.visitTemplates = exports.visitTSSourceFiles = void 0;
58var ts = require("typescript");
59var core_1 = require("@angular-devkit/core");
60function visitTSSourceFiles(tree, visitor) {
61 var e_1, _a;
62 var result = undefined;
63 try {
64 for (var _b = __values(visit(tree.root)), _c = _b.next(); !_c.done; _c = _b.next()) {
65 var sourceFile = _c.value;
66 result = visitor(sourceFile, tree, result);
67 }
68 }
69 catch (e_1_1) { e_1 = { error: e_1_1 }; }
70 finally {
71 try {
72 if (_c && !_c.done && (_a = _b["return"])) _a.call(_b);
73 }
74 finally { if (e_1) throw e_1.error; }
75 }
76 return result;
77}
78exports.visitTSSourceFiles = visitTSSourceFiles;
79function visitTemplates(tree, visitor) {
80 visitTSSourceFiles(tree, function (source) {
81 visitComponents(source, function (_, decoratorExpressionNode) {
82 ts.forEachChild(decoratorExpressionNode, function findTemplates(n) {
83 if (ts.isPropertyAssignment(n) && ts.isIdentifier(n.name)) {
84 if (n.name.text === 'template' &&
85 ts.isStringLiteralLike(n.initializer)) {
86 // Need to add an offset of one to the start because the template quotes are
87 // not part of the template content.
88 var templateStartIdx = n.initializer.getStart() + 1;
89 visitor({
90 fileName: source.fileName,
91 content: n.initializer.text,
92 inline: true,
93 start: templateStartIdx
94 }, tree);
95 return;
96 }
97 else if (n.name.text === 'templateUrl' &&
98 ts.isStringLiteralLike(n.initializer)) {
99 var parts = core_1.normalize(source.fileName).split('/').slice(0, -1);
100 var templatePath = core_1.resolve(core_1.normalize(parts.join('/')), core_1.normalize(n.initializer.text));
101 if (!tree.exists(templatePath)) {
102 return;
103 }
104 var fileContent = tree.read(templatePath);
105 if (!fileContent) {
106 return;
107 }
108 visitor({
109 fileName: templatePath,
110 content: fileContent.toString(),
111 inline: false,
112 start: 0
113 }, tree);
114 return;
115 }
116 }
117 ts.forEachChild(n, findTemplates);
118 });
119 });
120 });
121}
122exports.visitTemplates = visitTemplates;
123function visitNgModuleImports(sourceFile, callback) {
124 visitNgModuleProperty(sourceFile, callback, 'imports');
125}
126exports.visitNgModuleImports = visitNgModuleImports;
127function visitNgModuleExports(sourceFile, callback) {
128 visitNgModuleProperty(sourceFile, callback, 'exports');
129}
130exports.visitNgModuleExports = visitNgModuleExports;
131function visitNgModuleProperty(sourceFile, callback, property) {
132 visitNgModules(sourceFile, function (_, decoratorExpressionNode) {
133 ts.forEachChild(decoratorExpressionNode, function findTemplates(n) {
134 if (ts.isPropertyAssignment(n) &&
135 ts.isIdentifier(n.name) &&
136 n.name.text === property &&
137 ts.isArrayLiteralExpression(n.initializer)) {
138 callback(n, n.initializer.elements);
139 return;
140 }
141 ts.forEachChild(n, findTemplates);
142 });
143 });
144}
145function visitComponents(sourceFile, callback) {
146 visitDecorator(sourceFile, 'Component', callback);
147}
148exports.visitComponents = visitComponents;
149function visitNgModules(sourceFile, callback) {
150 visitDecorator(sourceFile, 'NgModule', callback);
151}
152exports.visitNgModules = visitNgModules;
153function visitDecorator(sourceFile, decoratorName, callback) {
154 ts.forEachChild(sourceFile, function findClassDeclaration(node) {
155 if (!ts.isClassDeclaration(node)) {
156 ts.forEachChild(node, findClassDeclaration);
157 }
158 var classDeclarationNode = node;
159 if (!classDeclarationNode.decorators ||
160 !classDeclarationNode.decorators.length) {
161 return;
162 }
163 var componentDecorator = classDeclarationNode.decorators.find(function (d) {
164 return (ts.isCallExpression(d.expression) &&
165 ts.isIdentifier(d.expression.expression) &&
166 d.expression.expression.text === decoratorName);
167 });
168 if (!componentDecorator) {
169 return;
170 }
171 var expression = componentDecorator.expression;
172 if (!ts.isCallExpression(expression)) {
173 return;
174 }
175 var _a = __read(expression.arguments, 1), arg = _a[0];
176 if (!arg || !ts.isObjectLiteralExpression(arg)) {
177 return;
178 }
179 callback(classDeclarationNode, arg);
180 });
181}
182exports.visitDecorator = visitDecorator;
183function visit(directory) {
184 var _a, _b, path, entry, content, source, e_2_1, _c, _d, path, e_3_1;
185 var e_2, _e, e_3, _f;
186 return __generator(this, function (_g) {
187 switch (_g.label) {
188 case 0:
189 _g.trys.push([0, 5, 6, 7]);
190 _a = __values(directory.subfiles), _b = _a.next();
191 _g.label = 1;
192 case 1:
193 if (!!_b.done) return [3 /*break*/, 4];
194 path = _b.value;
195 if (!(path.endsWith('.ts') && !path.endsWith('.d.ts'))) return [3 /*break*/, 3];
196 entry = directory.file(path);
197 if (!entry) return [3 /*break*/, 3];
198 content = entry.content;
199 source = ts.createSourceFile(entry.path, content.toString().replace(/^\uFEFF/, ''), ts.ScriptTarget.Latest, true);
200 return [4 /*yield*/, source];
201 case 2:
202 _g.sent();
203 _g.label = 3;
204 case 3:
205 _b = _a.next();
206 return [3 /*break*/, 1];
207 case 4: return [3 /*break*/, 7];
208 case 5:
209 e_2_1 = _g.sent();
210 e_2 = { error: e_2_1 };
211 return [3 /*break*/, 7];
212 case 6:
213 try {
214 if (_b && !_b.done && (_e = _a["return"])) _e.call(_a);
215 }
216 finally { if (e_2) throw e_2.error; }
217 return [7 /*endfinally*/];
218 case 7:
219 _g.trys.push([7, 12, 13, 14]);
220 _c = __values(directory.subdirs), _d = _c.next();
221 _g.label = 8;
222 case 8:
223 if (!!_d.done) return [3 /*break*/, 11];
224 path = _d.value;
225 if (path === 'node_modules') {
226 return [3 /*break*/, 10];
227 }
228 return [5 /*yield**/, __values(visit(directory.dir(path)))];
229 case 9:
230 _g.sent();
231 _g.label = 10;
232 case 10:
233 _d = _c.next();
234 return [3 /*break*/, 8];
235 case 11: return [3 /*break*/, 14];
236 case 12:
237 e_3_1 = _g.sent();
238 e_3 = { error: e_3_1 };
239 return [3 /*break*/, 14];
240 case 13:
241 try {
242 if (_d && !_d.done && (_f = _c["return"])) _f.call(_c);
243 }
244 finally { if (e_3) throw e_3.error; }
245 return [7 /*endfinally*/];
246 case 14: return [2 /*return*/];
247 }
248 });
249}
250//# sourceMappingURL=visitors.js.map
\No newline at end of file