Skip to content

Commit f0cf362

Browse files
committed
fix(migrations): Fix typo for strict-template migration
Fix typo for strict-template migration
1 parent 836094c commit f0cf362

7 files changed

Lines changed: 286 additions & 175 deletions

File tree

packages/core/schematics/BUILD.bazel

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -126,8 +126,8 @@ bundle_entrypoints = [
126126
"packages/core/schematics/migrations/http-xhr-backend/index.js",
127127
],
128128
[
129-
"strict-templates",
130-
"packages/core/schematics/migrations/strict-template/index.js",
129+
"strict-templates-default",
130+
"packages/core/schematics/migrations/strict-templates-default/index.js",
131131
],
132132
[
133133
"can-match-snapshot-required",
@@ -152,7 +152,7 @@ rollup.rollup(
152152
"//packages/core/schematics/migrations/change-detection-eager",
153153
"//packages/core/schematics/migrations/http-xhr-backend",
154154
"//packages/core/schematics/migrations/incremental-hydration",
155-
"//packages/core/schematics/migrations/strict-template",
155+
"//packages/core/schematics/migrations/strict-templates-default",
156156
"//packages/core/schematics/ng-generate/cleanup-unused-imports",
157157
"//packages/core/schematics/ng-generate/common-to-standalone-migration",
158158
"//packages/core/schematics/ng-generate/control-flow-migration",

packages/core/schematics/migrations.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,10 @@
1010
"description": "Adds 'withXhr' to 'provideHttpClient' function calls when the 'HttpXhrBackend' is used. For more information see: https://angular.dev/api/common/http/withXhr",
1111
"factory": "./bundles/http-xhr-backend.cjs#migrate"
1212
},
13-
"strict-template": {
13+
"strict-templates-default": {
1414
"version": "22.0.0",
15-
"description": "Adds 'strictTemplates: true' in tsconfig.json.",
16-
"factory": "./bundles/strict-templates.cjs#migrate"
15+
"description": "Adds 'strictTemplates: false' in tsconfig.json when not set.",
16+
"factory": "./bundles/strict-templates-default.cjs#migrate"
1717
},
1818
"can-match-snapshot-required": {
1919
"version": "22.0.0",

packages/core/schematics/migrations/strict-template/index.ts

Lines changed: 0 additions & 58 deletions
This file was deleted.

packages/core/schematics/migrations/strict-template/strict-template.spec.ts

Lines changed: 0 additions & 109 deletions
This file was deleted.

packages/core/schematics/migrations/strict-template/BUILD.bazel renamed to packages/core/schematics/migrations/strict-templates-default/BUILD.bazel

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,16 @@ package(
88
)
99

1010
ts_project(
11-
name = "strict-template",
11+
name = "strict-templates-default",
1212
srcs = glob(
1313
["**/*.ts"],
1414
exclude = ["*.spec.ts"],
1515
),
1616
deps = [
17+
"//:node_modules/@angular-devkit/core",
1718
"//:node_modules/@angular-devkit/schematics",
19+
"//:node_modules/@schematics/angular",
20+
"//:node_modules/typescript",
1821
"//packages/core/schematics/utils",
1922
],
2023
)
@@ -24,9 +27,11 @@ ts_project(
2427
testonly = True,
2528
srcs = glob(["*.spec.ts"]),
2629
deps = [
27-
":strict-template",
30+
":strict-templates-default",
2831
"//:node_modules/@angular-devkit/core",
2932
"//:node_modules/@angular-devkit/schematics",
33+
"//:node_modules/@schematics/angular",
34+
"//:node_modules/typescript",
3035
"//packages/core/schematics/utils",
3136
],
3237
)
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
/**
2+
* @license
3+
* Copyright Google LLC All Rights Reserved.
4+
*
5+
* Use of this source code is governed by an MIT-style license that can be
6+
* found in the LICENSE file at https://angular.dev/license
7+
*/
8+
9+
import {Rule} from '@angular-devkit/schematics';
10+
import {getProjectTsConfigPaths} from '../../utils/project_tsconfig_paths';
11+
import {JSONFile} from '@schematics/angular/utility/json-file';
12+
import ts from 'typescript';
13+
import {dirname, join} from 'node:path';
14+
15+
function getResolvedAngularCompilerOptions(tree: any, tsconfigPath: string): Record<string, any> {
16+
if (!tree.exists(tsconfigPath)) return {};
17+
18+
const sourceFile = ts.readJsonConfigFile(tsconfigPath, (path) => tree.readText(path));
19+
const config = ts.convertToObject(sourceFile, []);
20+
21+
let angularOptions = config.angularCompilerOptions || {};
22+
23+
// Manually resolve inheritance for Angular-specific options.
24+
// Since the TypeScript API doesn't perform a deep merge of custom/non-standard keys
25+
// during config parsing, we must traverse the inheritance chain manually
26+
if (config.extends) {
27+
// Management extends property...
28+
const parentPath = join(dirname(tsconfigPath), config.extends);
29+
30+
const parentOptions = getResolvedAngularCompilerOptions(tree, parentPath);
31+
32+
// Merge: the options of the current file overwrite those of the parent
33+
angularOptions = {
34+
...parentOptions,
35+
...angularOptions,
36+
};
37+
}
38+
39+
return angularOptions;
40+
}
41+
42+
/**
43+
* Migration that adds `strictTemplates: false` to `tsconfig.json` files.
44+
*/
45+
export function migrate(): Rule {
46+
return async (tree) => {
47+
const {buildPaths, testPaths} = await getProjectTsConfigPaths(tree);
48+
const allPaths = [...new Set([...buildPaths, ...testPaths])];
49+
50+
for (const tsconfigPath of allPaths) {
51+
const json = new JSONFile(tree, tsconfigPath);
52+
const compilerOptions = json.get(['compilerOptions']);
53+
54+
if (
55+
!compilerOptions ||
56+
typeof compilerOptions !== 'object' ||
57+
Object.keys(compilerOptions).length === 0
58+
) {
59+
continue;
60+
}
61+
62+
const angularOptions = getResolvedAngularCompilerOptions(tree, tsconfigPath);
63+
64+
if (angularOptions['strictTemplates'] !== undefined) {
65+
continue;
66+
}
67+
68+
if (json.get(['angularCompilerOptions', 'strictTemplates']) === undefined) {
69+
json.modify(['angularCompilerOptions', 'strictTemplates'], false);
70+
}
71+
}
72+
};
73+
}

0 commit comments

Comments
 (0)