chunk-73XDHERB.js 14 KB

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