jest-preset-angular 14.2.2 → 14.2.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,206 +0,0 @@
1
- "use strict";
2
- var __importDefault = (this && this.__importDefault) || function (mod) {
3
- return (mod && mod.__esModule) ? mod : { "default": mod };
4
- };
5
- Object.defineProperty(exports, "__esModule", { value: true });
6
- exports.replaceResources = replaceResources;
7
- const typescript_1 = __importDefault(require("typescript"));
8
- const constants_1 = require("../constants");
9
- const isAfterVersion = (targetMajor, targetMinor) => {
10
- const [major, minor] = typescript_1.default.versionMajorMinor.split('.').map((part) => parseInt(part));
11
- if (major < targetMajor) {
12
- return false;
13
- }
14
- else if (major > targetMajor) {
15
- return true;
16
- }
17
- else {
18
- return minor >= targetMinor;
19
- }
20
- };
21
- const IS_TS_48 = isAfterVersion(4, 8);
22
- const IS_AFTER_TS_50 = isAfterVersion(5, 0);
23
- const shouldTransform = (fileName) => !fileName.endsWith('.ngfactory.ts') && !fileName.endsWith('.ngstyle.ts');
24
- function replaceResources(program) {
25
- return (context) => {
26
- const typeChecker = program.getTypeChecker();
27
- const resourceImportDeclarations = [];
28
- const moduleKind = context.getCompilerOptions().module;
29
- const nodeFactory = context.factory;
30
- const visitNode = (node) => {
31
- if (typescript_1.default.isClassDeclaration(node)) {
32
- return visitClassDeclaration(nodeFactory, typeChecker, node, resourceImportDeclarations, moduleKind);
33
- }
34
- return typescript_1.default.visitEachChild(node, visitNode, context);
35
- };
36
- return (sourceFile) => {
37
- if (!shouldTransform(sourceFile.fileName)) {
38
- return sourceFile;
39
- }
40
- const updatedSourceFile = typescript_1.default.visitNode(sourceFile, visitNode);
41
- if (resourceImportDeclarations.length) {
42
- return nodeFactory.updateSourceFile(updatedSourceFile, typescript_1.default.setTextRange(nodeFactory.createNodeArray([...resourceImportDeclarations, ...updatedSourceFile.statements]), updatedSourceFile.statements));
43
- }
44
- return updatedSourceFile;
45
- };
46
- };
47
- }
48
- function visitClassDeclaration(nodeFactory, typeChecker, node, resourceImportDeclarations, moduleKind) {
49
- var _a, _b, _c;
50
- let decorators;
51
- let modifiers;
52
- if (IS_TS_48) {
53
- (_a = node.modifiers) === null || _a === void 0 ? void 0 : _a.forEach((modifier) => {
54
- if (typescript_1.default.isDecorator(modifier)) {
55
- decorators !== null && decorators !== void 0 ? decorators : (decorators = []);
56
- decorators.push(modifier);
57
- }
58
- else {
59
- modifiers = modifiers !== null && modifiers !== void 0 ? modifiers : (modifiers = []);
60
- modifiers.push(modifier);
61
- }
62
- });
63
- }
64
- else {
65
- decorators = [...((_b = typescript_1.default.getDecorators(node)) !== null && _b !== void 0 ? _b : [])];
66
- modifiers = [...((_c = typescript_1.default.getModifiers(node)) !== null && _c !== void 0 ? _c : [])];
67
- }
68
- if (!decorators || !decorators.length) {
69
- return node;
70
- }
71
- decorators = decorators.map((current) => visitDecorator(nodeFactory, current, typeChecker, resourceImportDeclarations, moduleKind));
72
- return IS_TS_48
73
- ? nodeFactory.updateClassDeclaration(node, [...decorators, ...(modifiers !== null && modifiers !== void 0 ? modifiers : [])], node.name, node.typeParameters, node.heritageClauses, node.members)
74
- : nodeFactory.updateClassDeclaration(node, decorators, modifiers, node.name, node.typeParameters, node.heritageClauses, node.members);
75
- }
76
- function visitDecorator(nodeFactory, node, typeChecker, resourceImportDeclarations, moduleKind) {
77
- if (!isComponentDecorator(node, typeChecker)) {
78
- return node;
79
- }
80
- if (!typescript_1.default.isCallExpression(node.expression)) {
81
- return node;
82
- }
83
- const decoratorFactory = node.expression;
84
- const args = decoratorFactory.arguments;
85
- if (args.length !== 1 || !typescript_1.default.isObjectLiteralExpression(args[0])) {
86
- return node;
87
- }
88
- const objectExpression = args[0];
89
- const styleReplacements = [];
90
- let properties = typescript_1.default.visitNodes(objectExpression.properties, (objExpressionNode) => typescript_1.default.isObjectLiteralElementLike(objExpressionNode)
91
- ? visitComponentMetadata(nodeFactory, objExpressionNode, resourceImportDeclarations, moduleKind)
92
- : objExpressionNode);
93
- if (styleReplacements.length) {
94
- const styleProperty = nodeFactory.createPropertyAssignment(nodeFactory.createIdentifier(constants_1.STYLES), nodeFactory.createArrayLiteralExpression(styleReplacements));
95
- properties = nodeFactory.createNodeArray([...properties, styleProperty]);
96
- }
97
- return nodeFactory.updateDecorator(node, nodeFactory.updateCallExpression(decoratorFactory, decoratorFactory.expression, decoratorFactory.typeArguments, [
98
- nodeFactory.updateObjectLiteralExpression(objectExpression, properties),
99
- ]));
100
- }
101
- function visitComponentMetadata(nodeFactory, node, resourceImportDeclarations, moduleKind) {
102
- if (!typescript_1.default.isPropertyAssignment(node) || typescript_1.default.isComputedPropertyName(node.name)) {
103
- return node;
104
- }
105
- const name = node.name.text;
106
- switch (name) {
107
- case 'moduleId':
108
- return undefined;
109
- case constants_1.TEMPLATE_URL:
110
- const url = getResourceUrl(node.initializer);
111
- if (!url) {
112
- return node;
113
- }
114
- const importName = createResourceImport(nodeFactory, url, resourceImportDeclarations, moduleKind);
115
- if (!importName) {
116
- return node;
117
- }
118
- return nodeFactory.updatePropertyAssignment(node, nodeFactory.createIdentifier(constants_1.TEMPLATE), importName);
119
- case constants_1.STYLES:
120
- if (!typescript_1.default.isArrayLiteralExpression(node.initializer) && !typescript_1.default.isStringLiteral(node.initializer)) {
121
- return node;
122
- }
123
- return undefined;
124
- case constants_1.STYLE_URLS:
125
- if (!typescript_1.default.isArrayLiteralExpression(node.initializer)) {
126
- return node;
127
- }
128
- return undefined;
129
- case constants_1.STYLE_URL:
130
- if (!typescript_1.default.isStringLiteral(node.initializer)) {
131
- return node;
132
- }
133
- return undefined;
134
- default:
135
- return node;
136
- }
137
- }
138
- function getResourceUrl(node) {
139
- if (!typescript_1.default.isStringLiteral(node) && !typescript_1.default.isNoSubstitutionTemplateLiteral(node)) {
140
- return null;
141
- }
142
- return `${/^\.?\.\//.test(node.text) ? '' : './'}${node.text}`;
143
- }
144
- function isComponentDecorator(node, typeChecker) {
145
- if (!typescript_1.default.isDecorator(node)) {
146
- return false;
147
- }
148
- const origin = getDecoratorOrigin(node, typeChecker);
149
- return !!(origin && origin.module === '@angular/core' && origin.name === constants_1.COMPONENT);
150
- }
151
- function createResourceImport(nodeFactory, url, resourceImportDeclarations, moduleKind = typescript_1.default.ModuleKind.ES2015) {
152
- const urlLiteral = nodeFactory.createStringLiteral(url);
153
- if (moduleKind < typescript_1.default.ModuleKind.ES2015) {
154
- return nodeFactory.createCallExpression(nodeFactory.createIdentifier(constants_1.REQUIRE), [], [urlLiteral]);
155
- }
156
- else {
157
- const importName = nodeFactory.createIdentifier(`__NG_CLI_RESOURCE__${resourceImportDeclarations.length}`);
158
- let importDeclaration;
159
- if (IS_AFTER_TS_50) {
160
- importDeclaration = nodeFactory.createImportDeclaration(undefined, nodeFactory.createImportClause(false, importName, undefined), urlLiteral);
161
- }
162
- else {
163
- importDeclaration = nodeFactory.createImportDeclaration(undefined, undefined, nodeFactory.createImportClause(false, importName, undefined), urlLiteral);
164
- }
165
- resourceImportDeclarations.push(importDeclaration);
166
- return importName;
167
- }
168
- }
169
- function getDecoratorOrigin(decorator, typeChecker) {
170
- if (!typescript_1.default.isCallExpression(decorator.expression)) {
171
- return null;
172
- }
173
- let identifier;
174
- let name = '';
175
- if (typescript_1.default.isPropertyAccessExpression(decorator.expression.expression)) {
176
- identifier = decorator.expression.expression.expression;
177
- name = decorator.expression.expression.name.text;
178
- }
179
- else if (typescript_1.default.isIdentifier(decorator.expression.expression)) {
180
- identifier = decorator.expression.expression;
181
- }
182
- else {
183
- return null;
184
- }
185
- const symbol = typeChecker.getSymbolAtLocation(identifier);
186
- if (symbol && symbol.declarations && symbol.declarations.length > 0) {
187
- const declaration = symbol.declarations[0];
188
- let module;
189
- if (typescript_1.default.isImportSpecifier(declaration)) {
190
- name = (declaration.propertyName || declaration.name).text;
191
- module = declaration.parent.parent.parent.moduleSpecifier.text;
192
- }
193
- else if (typescript_1.default.isNamespaceImport(declaration)) {
194
- module = declaration.parent.parent.moduleSpecifier.text;
195
- }
196
- else if (typescript_1.default.isImportClause(declaration)) {
197
- name = declaration.name.text;
198
- module = declaration.parent.moduleSpecifier.text;
199
- }
200
- else {
201
- return null;
202
- }
203
- return { name, module };
204
- }
205
- return null;
206
- }
@@ -1,75 +0,0 @@
1
- "use strict";
2
- var __importDefault = (this && this.__importDefault) || function (mod) {
3
- return (mod && mod.__esModule) ? mod : { "default": mod };
4
- };
5
- Object.defineProperty(exports, "__esModule", { value: true });
6
- exports.runNgccJestProcessor = void 0;
7
- const child_process_1 = require("child_process");
8
- const path_1 = __importDefault(require("path"));
9
- const ANGULAR_COMPILER_CLI_PKG_NAME = '@angular/compiler-cli';
10
- let ngccPath = '';
11
- try {
12
- ngccPath = require.resolve('@angular/compiler-cli/ngcc/main-ngcc.js');
13
- }
14
- catch (_a) {
15
- try {
16
- const compilerCliNgccPath = require.resolve('@angular/compiler-cli/ngcc');
17
- const compilerCliNgccFolder = compilerCliNgccPath.substring(0, compilerCliNgccPath.lastIndexOf(path_1.default.sep));
18
- ngccPath = path_1.default.resolve(compilerCliNgccFolder, 'main-ngcc.js');
19
- }
20
- catch (_b) {
21
- }
22
- }
23
- function findNodeModulesDirectory() {
24
- return ngccPath.substring(0, ngccPath.indexOf(ANGULAR_COMPILER_CLI_PKG_NAME.replace('/', path_1.default.sep)));
25
- }
26
- function findAngularCompilerCliVersion() {
27
- const packagePath = require.resolve(ANGULAR_COMPILER_CLI_PKG_NAME);
28
- const substringLength = packagePath.indexOf(ANGULAR_COMPILER_CLI_PKG_NAME.replace('/', path_1.default.sep)) +
29
- ANGULAR_COMPILER_CLI_PKG_NAME.length;
30
- const ngCompilerCliFolder = packagePath.substring(0, substringLength);
31
- const ngCompilerCliPackageJson = `${ngCompilerCliFolder}/package.json`;
32
- const { version } = require(ngCompilerCliPackageJson);
33
- return version;
34
- }
35
- const nodeModuleDirPath = findNodeModulesDirectory();
36
- const runNgccJestProcessor = (tsconfigPath) => {
37
- var _a;
38
- if (nodeModuleDirPath) {
39
- process.stdout.write('\nngcc-jest-processor: running ngcc\n');
40
- const ngccBaseArgs = [
41
- ngccPath,
42
- '--source',
43
- nodeModuleDirPath,
44
- '--properties',
45
- ...['es2015', 'main'],
46
- '--first-only',
47
- 'false',
48
- '--async',
49
- ];
50
- if (tsconfigPath) {
51
- ngccBaseArgs.push(...['--tsconfig', tsconfigPath]);
52
- }
53
- const { status, error } = (0, child_process_1.spawnSync)(process.execPath, ngccBaseArgs, {
54
- stdio: ['inherit', process.stderr, process.stderr],
55
- });
56
- if (status !== 0) {
57
- const errorMessage = (_a = error === null || error === void 0 ? void 0 : error.message) !== null && _a !== void 0 ? _a : '';
58
- throw new Error(`${errorMessage} NGCC failed ${errorMessage ? ', see above' : ''}.`);
59
- }
60
- }
61
- else {
62
- const ngCompilerCliVersion = findAngularCompilerCliVersion();
63
- const [ngMajorVersion] = ngCompilerCliVersion.split('.');
64
- if (parseInt(ngMajorVersion, 10) < 16) {
65
- console.log(`Warning: Could not locate '@angular/compiler-cli' to run 'ngcc' automatically.` +
66
- `Please make sure you are running 'ngcc-jest-processor.js' from root level of your project.` +
67
- `'ngcc' must be run before running Jest`);
68
- }
69
- else {
70
- console.log(`@angular/compiler-cli@${ngCompilerCliVersion} detected. Skipping 'ngcc'`);
71
- console.log(`Tip: To avoid this message you can remove 'jest-preset-angular/global-setup' from your jest config`);
72
- }
73
- }
74
- };
75
- exports.runNgccJestProcessor = runNgccJestProcessor;