UNPKG

16.3 kBJavaScriptView Raw
1
2 import {createRequire as __cjsCompatRequire} from 'module';
3 const require = __cjsCompatRequire(import.meta.url);
4 const __ESM_IMPORT_META_URL__ = import.meta.url;
5
6import {
7 TypeScriptReflectionHost
8} from "./chunk-Q5GIQ3RV.js";
9import {
10 __spreadProps,
11 __spreadValues
12} from "./chunk-GMSUYBZP.js";
13
14// bazel-out/k8-fastbuild/bin/packages/compiler-cli/src/transformers/downlevel_decorators_transform/downlevel_decorators_transform.mjs
15import ts2 from "typescript";
16
17// bazel-out/k8-fastbuild/bin/packages/compiler-cli/src/transformers/downlevel_decorators_transform/patch_alias_reference_resolution.mjs
18import ts from "typescript";
19var patchedReferencedAliasesSymbol = Symbol("patchedReferencedAliases");
20function loadIsReferencedAliasDeclarationPatch(context) {
21 if (!isTransformationContextWithEmitResolver(context)) {
22 throwIncompatibleTransformationContextError();
23 }
24 const emitResolver = context.getEmitResolver();
25 const existingReferencedAliases = emitResolver[patchedReferencedAliasesSymbol];
26 if (existingReferencedAliases !== void 0) {
27 return existingReferencedAliases;
28 }
29 const originalIsReferencedAliasDeclaration = emitResolver.isReferencedAliasDeclaration;
30 if (originalIsReferencedAliasDeclaration === void 0) {
31 throwIncompatibleTransformationContextError();
32 }
33 const referencedAliases = /* @__PURE__ */ new Set();
34 emitResolver.isReferencedAliasDeclaration = function(node, ...args) {
35 if (isAliasImportDeclaration(node) && referencedAliases.has(node)) {
36 return true;
37 }
38 return originalIsReferencedAliasDeclaration.call(emitResolver, node, ...args);
39 };
40 return emitResolver[patchedReferencedAliasesSymbol] = referencedAliases;
41}
42function isAliasImportDeclaration(node) {
43 return ts.isImportSpecifier(node) || ts.isNamespaceImport(node) || ts.isImportClause(node);
44}
45function isTransformationContextWithEmitResolver(context) {
46 return context.getEmitResolver !== void 0;
47}
48function throwIncompatibleTransformationContextError() {
49 throw Error("Unable to downlevel Angular decorators due to an incompatible TypeScript version.\nIf you recently updated TypeScript and this issue surfaces now, consider downgrading.\n\nPlease report an issue on the Angular repositories when this issue surfaces and you are using a supposedly compatible TypeScript version.");
50}
51
52// bazel-out/k8-fastbuild/bin/packages/compiler-cli/src/transformers/downlevel_decorators_transform/downlevel_decorators_transform.mjs
53function isAngularDecorator(decorator, isCore) {
54 return isCore || decorator.import !== null && decorator.import.from === "@angular/core";
55}
56var DECORATOR_INVOCATION_JSDOC_TYPE = "!Array<{type: !Function, args: (undefined|!Array<?>)}>";
57function extractMetadataFromSingleDecorator(decorator, diagnostics) {
58 const metadataProperties = [];
59 const expr = decorator.expression;
60 switch (expr.kind) {
61 case ts2.SyntaxKind.Identifier:
62 metadataProperties.push(ts2.createPropertyAssignment("type", expr));
63 break;
64 case ts2.SyntaxKind.CallExpression:
65 const call = expr;
66 metadataProperties.push(ts2.createPropertyAssignment("type", call.expression));
67 if (call.arguments.length) {
68 const args = [];
69 for (const arg of call.arguments) {
70 args.push(arg);
71 }
72 const argsArrayLiteral = ts2.createArrayLiteral(ts2.createNodeArray(args, true));
73 metadataProperties.push(ts2.createPropertyAssignment("args", argsArrayLiteral));
74 }
75 break;
76 default:
77 diagnostics.push({
78 file: decorator.getSourceFile(),
79 start: decorator.getStart(),
80 length: decorator.getEnd() - decorator.getStart(),
81 messageText: `${ts2.SyntaxKind[decorator.kind]} not implemented in gathering decorator metadata.`,
82 category: ts2.DiagnosticCategory.Error,
83 code: 0
84 });
85 break;
86 }
87 return ts2.createObjectLiteral(metadataProperties);
88}
89function createCtorParametersClassProperty(diagnostics, entityNameToExpression, ctorParameters, isClosureCompilerEnabled) {
90 const params = [];
91 for (const ctorParam of ctorParameters) {
92 if (!ctorParam.type && ctorParam.decorators.length === 0) {
93 params.push(ts2.createNull());
94 continue;
95 }
96 const paramType = ctorParam.type ? typeReferenceToExpression(entityNameToExpression, ctorParam.type) : void 0;
97 const members = [ts2.createPropertyAssignment("type", paramType || ts2.createIdentifier("undefined"))];
98 const decorators = [];
99 for (const deco of ctorParam.decorators) {
100 decorators.push(extractMetadataFromSingleDecorator(deco, diagnostics));
101 }
102 if (decorators.length) {
103 members.push(ts2.createPropertyAssignment("decorators", ts2.createArrayLiteral(decorators)));
104 }
105 params.push(ts2.createObjectLiteral(members));
106 }
107 const initializer = ts2.createArrowFunction(void 0, void 0, [], void 0, ts2.createToken(ts2.SyntaxKind.EqualsGreaterThanToken), ts2.createArrayLiteral(params, true));
108 const ctorProp = ts2.createProperty(void 0, [ts2.createToken(ts2.SyntaxKind.StaticKeyword)], "ctorParameters", void 0, void 0, initializer);
109 if (isClosureCompilerEnabled) {
110 ts2.setSyntheticLeadingComments(ctorProp, [
111 {
112 kind: ts2.SyntaxKind.MultiLineCommentTrivia,
113 text: [
114 `*`,
115 ` * @type {function(): !Array<(null|{`,
116 ` * type: ?,`,
117 ` * decorators: (undefined|${DECORATOR_INVOCATION_JSDOC_TYPE}),`,
118 ` * })>}`,
119 ` * @nocollapse`,
120 ` `
121 ].join("\n"),
122 pos: -1,
123 end: -1,
124 hasTrailingNewLine: true
125 }
126 ]);
127 }
128 return ctorProp;
129}
130function typeReferenceToExpression(entityNameToExpression, node) {
131 let kind = node.kind;
132 if (ts2.isLiteralTypeNode(node)) {
133 kind = node.literal.kind;
134 }
135 switch (kind) {
136 case ts2.SyntaxKind.FunctionType:
137 case ts2.SyntaxKind.ConstructorType:
138 return ts2.createIdentifier("Function");
139 case ts2.SyntaxKind.ArrayType:
140 case ts2.SyntaxKind.TupleType:
141 return ts2.createIdentifier("Array");
142 case ts2.SyntaxKind.TypePredicate:
143 case ts2.SyntaxKind.TrueKeyword:
144 case ts2.SyntaxKind.FalseKeyword:
145 case ts2.SyntaxKind.BooleanKeyword:
146 return ts2.createIdentifier("Boolean");
147 case ts2.SyntaxKind.StringLiteral:
148 case ts2.SyntaxKind.StringKeyword:
149 return ts2.createIdentifier("String");
150 case ts2.SyntaxKind.ObjectKeyword:
151 return ts2.createIdentifier("Object");
152 case ts2.SyntaxKind.NumberKeyword:
153 case ts2.SyntaxKind.NumericLiteral:
154 return ts2.createIdentifier("Number");
155 case ts2.SyntaxKind.TypeReference:
156 const typeRef = node;
157 return entityNameToExpression(typeRef.typeName);
158 case ts2.SyntaxKind.UnionType:
159 const childTypeNodes = node.types.filter((t) => !(ts2.isLiteralTypeNode(t) && t.literal.kind === ts2.SyntaxKind.NullKeyword));
160 return childTypeNodes.length === 1 ? typeReferenceToExpression(entityNameToExpression, childTypeNodes[0]) : void 0;
161 default:
162 return void 0;
163 }
164}
165function symbolIsRuntimeValue(typeChecker, symbol) {
166 if (symbol.flags & ts2.SymbolFlags.Alias) {
167 symbol = typeChecker.getAliasedSymbol(symbol);
168 }
169 return (symbol.flags & ts2.SymbolFlags.Value & ts2.SymbolFlags.ConstEnumExcludes) !== 0;
170}
171function getDownlevelDecoratorsTransform(typeChecker, host, diagnostics, isCore, isClosureCompilerEnabled, skipClassDecorators) {
172 function addJSDocTypeAnnotation(node, jsdocType) {
173 if (!isClosureCompilerEnabled) {
174 return;
175 }
176 ts2.setSyntheticLeadingComments(node, [
177 {
178 kind: ts2.SyntaxKind.MultiLineCommentTrivia,
179 text: `* @type {${jsdocType}} `,
180 pos: -1,
181 end: -1,
182 hasTrailingNewLine: true
183 }
184 ]);
185 }
186 function createDecoratorClassProperty(decoratorList) {
187 const modifier = ts2.createToken(ts2.SyntaxKind.StaticKeyword);
188 const initializer = ts2.createArrayLiteral(decoratorList, true);
189 const prop = ts2.createProperty(void 0, [modifier], "decorators", void 0, void 0, initializer);
190 addJSDocTypeAnnotation(prop, DECORATOR_INVOCATION_JSDOC_TYPE);
191 return prop;
192 }
193 function createPropDecoratorsClassProperty(diagnostics2, properties) {
194 const entries = [];
195 for (const [name, decorators] of properties.entries()) {
196 entries.push(ts2.createPropertyAssignment(name, ts2.createArrayLiteral(decorators.map((deco) => extractMetadataFromSingleDecorator(deco, diagnostics2)))));
197 }
198 const initializer = ts2.createObjectLiteral(entries, true);
199 const prop = ts2.createProperty(void 0, [ts2.createToken(ts2.SyntaxKind.StaticKeyword)], "propDecorators", void 0, void 0, initializer);
200 addJSDocTypeAnnotation(prop, `!Object<string, ${DECORATOR_INVOCATION_JSDOC_TYPE}>`);
201 return prop;
202 }
203 return (context) => {
204 const referencedParameterTypes = loadIsReferencedAliasDeclarationPatch(context);
205 function entityNameToExpression(name) {
206 const symbol = typeChecker.getSymbolAtLocation(name);
207 if (!symbol || !symbolIsRuntimeValue(typeChecker, symbol) || !symbol.declarations || symbol.declarations.length === 0) {
208 return void 0;
209 }
210 if (ts2.isQualifiedName(name)) {
211 const containerExpr = entityNameToExpression(name.left);
212 if (containerExpr === void 0) {
213 return void 0;
214 }
215 return ts2.createPropertyAccess(containerExpr, name.right);
216 }
217 const decl = symbol.declarations[0];
218 if (isAliasImportDeclaration(decl)) {
219 referencedParameterTypes.add(decl);
220 if (decl.name !== void 0) {
221 return ts2.getMutableClone(decl.name);
222 }
223 }
224 return ts2.getMutableClone(name);
225 }
226 function transformClassElement(element) {
227 element = ts2.visitEachChild(element, decoratorDownlevelVisitor, context);
228 const decoratorsToKeep = [];
229 const toLower = [];
230 const decorators = host.getDecoratorsOfDeclaration(element) || [];
231 for (const decorator of decorators) {
232 const decoratorNode = decorator.node;
233 if (!isAngularDecorator(decorator, isCore)) {
234 decoratorsToKeep.push(decoratorNode);
235 continue;
236 }
237 toLower.push(decoratorNode);
238 }
239 if (!toLower.length)
240 return [void 0, element, []];
241 if (!element.name || !ts2.isIdentifier(element.name)) {
242 diagnostics.push({
243 file: element.getSourceFile(),
244 start: element.getStart(),
245 length: element.getEnd() - element.getStart(),
246 messageText: `Cannot process decorators for class element with non-analyzable name.`,
247 category: ts2.DiagnosticCategory.Error,
248 code: 0
249 });
250 return [void 0, element, []];
251 }
252 const name = element.name.text;
253 const mutable = ts2.getMutableClone(element);
254 mutable.decorators = decoratorsToKeep.length ? ts2.setTextRange(ts2.createNodeArray(decoratorsToKeep), mutable.decorators) : void 0;
255 return [name, mutable, toLower];
256 }
257 function transformConstructor(ctor) {
258 ctor = ts2.visitEachChild(ctor, decoratorDownlevelVisitor, context);
259 const newParameters = [];
260 const oldParameters = ctor.parameters;
261 const parametersInfo = [];
262 for (const param of oldParameters) {
263 const decoratorsToKeep = [];
264 const paramInfo = { decorators: [], type: null };
265 const decorators = host.getDecoratorsOfDeclaration(param) || [];
266 for (const decorator of decorators) {
267 const decoratorNode = decorator.node;
268 if (!isAngularDecorator(decorator, isCore)) {
269 decoratorsToKeep.push(decoratorNode);
270 continue;
271 }
272 paramInfo.decorators.push(decoratorNode);
273 }
274 if (param.type) {
275 paramInfo.type = param.type;
276 }
277 parametersInfo.push(paramInfo);
278 const newParam = ts2.updateParameter(param, decoratorsToKeep.length ? decoratorsToKeep : void 0, param.modifiers, param.dotDotDotToken, param.name, param.questionToken, param.type, param.initializer);
279 newParameters.push(newParam);
280 }
281 const updated = ts2.updateConstructor(ctor, ctor.decorators, ctor.modifiers, newParameters, ctor.body);
282 return [updated, parametersInfo];
283 }
284 function transformClassDeclaration(classDecl) {
285 classDecl = ts2.getMutableClone(classDecl);
286 const newMembers = [];
287 const decoratedProperties = /* @__PURE__ */ new Map();
288 let classParameters = null;
289 for (const member of classDecl.members) {
290 switch (member.kind) {
291 case ts2.SyntaxKind.PropertyDeclaration:
292 case ts2.SyntaxKind.GetAccessor:
293 case ts2.SyntaxKind.SetAccessor:
294 case ts2.SyntaxKind.MethodDeclaration: {
295 const [name, newMember, decorators] = transformClassElement(member);
296 newMembers.push(newMember);
297 if (name)
298 decoratedProperties.set(name, decorators);
299 continue;
300 }
301 case ts2.SyntaxKind.Constructor: {
302 const ctor = member;
303 if (!ctor.body)
304 break;
305 const [newMember, parametersInfo] = transformConstructor(member);
306 classParameters = parametersInfo;
307 newMembers.push(newMember);
308 continue;
309 }
310 default:
311 break;
312 }
313 newMembers.push(ts2.visitEachChild(member, decoratorDownlevelVisitor, context));
314 }
315 const decoratorsToKeep = new Set(classDecl.decorators);
316 const possibleAngularDecorators = host.getDecoratorsOfDeclaration(classDecl) || [];
317 let hasAngularDecorator = false;
318 const decoratorsToLower = [];
319 for (const decorator of possibleAngularDecorators) {
320 const decoratorNode = decorator.node;
321 const isNgDecorator = isAngularDecorator(decorator, isCore);
322 if (isNgDecorator) {
323 hasAngularDecorator = true;
324 }
325 if (isNgDecorator && !skipClassDecorators) {
326 decoratorsToLower.push(extractMetadataFromSingleDecorator(decoratorNode, diagnostics));
327 decoratorsToKeep.delete(decoratorNode);
328 }
329 }
330 if (decoratorsToLower.length) {
331 newMembers.push(createDecoratorClassProperty(decoratorsToLower));
332 }
333 if (classParameters) {
334 if (hasAngularDecorator || classParameters.some((p) => !!p.decorators.length)) {
335 newMembers.push(createCtorParametersClassProperty(diagnostics, entityNameToExpression, classParameters, isClosureCompilerEnabled));
336 }
337 }
338 if (decoratedProperties.size) {
339 newMembers.push(createPropDecoratorsClassProperty(diagnostics, decoratedProperties));
340 }
341 const members = ts2.setTextRange(ts2.createNodeArray(newMembers, classDecl.members.hasTrailingComma), classDecl.members);
342 return ts2.updateClassDeclaration(classDecl, decoratorsToKeep.size ? Array.from(decoratorsToKeep) : void 0, classDecl.modifiers, classDecl.name, classDecl.typeParameters, classDecl.heritageClauses, members);
343 }
344 function decoratorDownlevelVisitor(node) {
345 if (ts2.isClassDeclaration(node)) {
346 return transformClassDeclaration(node);
347 }
348 return ts2.visitEachChild(node, decoratorDownlevelVisitor, context);
349 }
350 return (sf) => {
351 return ts2.visitEachChild(sf, decoratorDownlevelVisitor, context);
352 };
353 };
354}
355
356// bazel-out/k8-fastbuild/bin/packages/compiler-cli/private/tooling.mjs
357var GLOBAL_DEFS_FOR_TERSER = {
358 ngDevMode: false,
359 ngI18nClosureMode: false
360};
361var GLOBAL_DEFS_FOR_TERSER_WITH_AOT = __spreadProps(__spreadValues({}, GLOBAL_DEFS_FOR_TERSER), {
362 ngJitMode: false
363});
364function constructorParametersDownlevelTransform(program) {
365 const typeChecker = program.getTypeChecker();
366 const reflectionHost = new TypeScriptReflectionHost(typeChecker);
367 return getDownlevelDecoratorsTransform(typeChecker, reflectionHost, [], false, false, true);
368}
369
370export {
371 GLOBAL_DEFS_FOR_TERSER,
372 GLOBAL_DEFS_FOR_TERSER_WITH_AOT,
373 constructorParametersDownlevelTransform
374};
375/**
376 * @license
377 * Copyright Google LLC All Rights Reserved.
378 *
379 * Use of this source code is governed by an MIT-style license that can be
380 * found in the LICENSE file at https://angular.io/license
381 */
382//# sourceMappingURL=chunk-HTCAZIYK.js.map