Skip to content

Commit f6b01f3

Browse files
committed
fix(@schematics/angular): use null objects and callbacks in karma-to-vitest migration
The karma-to-vitest migration schematic previously used plain JavaScript objects as dictionaries when processing custom build options and analyzing Karma AST configurations. In environments where the input workspace files contain custom keys such as "__proto__", these assignments would leak properties onto the global Object prototype.
1 parent b0dcf00 commit f6b01f3

3 files changed

Lines changed: 8 additions & 5 deletions

File tree

packages/schematics/angular/migrations/migrate-karma-to-vitest/karma-config-analyzer.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,7 @@ export function analyzeKarmaConfig(content: string): KarmaConfigAnalysis {
142142
case ts.SyntaxKind.ArrayLiteralExpression:
143143
return (node as ts.ArrayLiteralExpression).elements.map(extractValue);
144144
case ts.SyntaxKind.ObjectLiteralExpression: {
145-
const obj: { [key: string]: KarmaConfigValue } = {};
145+
const obj: { [key: string]: KarmaConfigValue } = Object.create(null);
146146
for (const prop of (node as ts.ObjectLiteralExpression).properties) {
147147
if (isSupportedPropertyAssignment(prop)) {
148148
// Recursively extract values for nested objects.

packages/schematics/angular/migrations/migrate-karma-to-vitest/karma-config-comparer.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -46,11 +46,10 @@ export async function generateDefaultKarmaConfig(
4646

4747
// TODO: Replace this with the actual schematic templating logic.
4848
template = template
49-
.replace(
50-
/<%= relativePathToWorkspaceRoot %>/g,
49+
.replace(/<%= relativePathToWorkspaceRoot %>/g, () =>
5150
path.normalize(relativePathToWorkspaceRoot).replace(/\\/g, '/'),
5251
)
53-
.replace(/<%= folderName %>/g, projectName);
52+
.replace(/<%= folderName %>/g, () => projectName);
5453

5554
const devkitPluginRegex = /<% if \(needDevkitPlugin\) { %>(.*?)<% } %>/gs;
5655
const replacement = needDevkitPlugin ? '$1' : '';

packages/schematics/angular/migrations/migrate-karma-to-vitest/migration.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ async function processTestTargetOptions(
3232
let needsIstanbul = false;
3333
for (const [configName, options] of allTargetOptions(testTarget, false)) {
3434
const configKey = configName || '';
35+
3536
if (!customBuildOptions[configKey]) {
3637
// Match Karma behavior where AOT was disabled by default
3738
customBuildOptions[configKey] = {
@@ -276,7 +277,10 @@ function updateProjects(tree: Tree, context: SchematicContext): Rule {
276277
tsConfigsToUpdate.add(join(project.root, 'tsconfig.spec.json'));
277278

278279
// Store custom build options to move to a new build configuration if needed
279-
const customBuildOptions: Record<string, Record<string, json.JsonValue | undefined>> = {};
280+
const customBuildOptions: Record<
281+
string,
282+
Record<string, json.JsonValue | undefined>
283+
> = Object.create(null);
280284

281285
const projectCoverageInfo = await processTestTargetOptions(
282286
testTarget,

0 commit comments

Comments
 (0)