Skip to content

Commit f0b8109

Browse files
committed
fix(@schematics/angular): safely comment out multiline statements in refactor-jasmine-vitest
When refactoring unsupported or empty statements (such as `pending()`, `expect().nothing()`, and certain `jasmine` member assignments), the schematic previously commented out the code using a single-line comment prefix on the full text of the node. If the statement spanned multiple lines, only the first line was commented, leaving subsequent lines uncommented. This resulted in invalid syntax or unexpected compile errors.
1 parent 11a4438 commit f0b8109

5 files changed

Lines changed: 42 additions & 25 deletions

File tree

packages/schematics/angular/refactor/jasmine-vitest/transformers/jasmine-lifecycle.ts

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515

1616
import ts from 'typescript';
1717
import { createPropertyAccess } from '../utils/ast-helpers';
18-
import { addTodoComment } from '../utils/comment-helpers';
18+
import { addCommentedNodeText, addTodoComment } from '../utils/comment-helpers';
1919
import { RefactorContext } from '../utils/refactor-context';
2020

2121
const FOCUSED_SKIPPED_RENAMES = new Map<string, { newBase: string; newName: string }>([
@@ -77,7 +77,6 @@ export function transformPending(
7777
) {
7878
hasPending = true;
7979
const replacement = ts.factory.createEmptyStatement();
80-
const originalText = bodyNode.getFullText().trim();
8180

8281
reporter.reportTransformation(
8382
sourceFile,
@@ -87,12 +86,7 @@ export function transformPending(
8786
const category = 'pending';
8887
reporter.recordTodo(category, sourceFile, bodyNode);
8988
addTodoComment(replacement, category);
90-
ts.addSyntheticLeadingComment(
91-
replacement,
92-
ts.SyntaxKind.SingleLineCommentTrivia,
93-
` ${originalText}`,
94-
true,
95-
);
89+
addCommentedNodeText(replacement, bodyNode);
9690

9791
return replacement;
9892
}

packages/schematics/angular/refactor/jasmine-vitest/transformers/jasmine-matcher.ts

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ import {
2121
createPropertyAccess,
2222
} from '../utils/ast-helpers';
2323
import { getJasmineMethodName, isJasmineCallExpression } from '../utils/ast-validation';
24-
import { addTodoComment } from '../utils/comment-helpers';
24+
import { addCommentedNodeText, addTodoComment } from '../utils/comment-helpers';
2525
import { RefactorContext } from '../utils/refactor-context';
2626

2727
const SUGAR_MATCHER_CHANGES = new Map<string, { newName: string; newArgs?: ts.Expression[] }>([
@@ -607,18 +607,12 @@ export function transformExpectNothing(
607607

608608
// The statement is `expect().nothing()`, which can be removed.
609609
const replacement = ts.factory.createEmptyStatement();
610-
const originalText = node.getFullText().trim();
611610

612611
reporter.reportTransformation(sourceFile, node, 'Removed `expect().nothing()` statement.');
613612
const category = 'expect-nothing';
614613
reporter.recordTodo(category, sourceFile, node);
615614
addTodoComment(replacement, category);
616-
ts.addSyntheticLeadingComment(
617-
replacement,
618-
ts.SyntaxKind.SingleLineCommentTrivia,
619-
` ${originalText}`,
620-
true,
621-
);
615+
addCommentedNodeText(replacement, node);
622616

623617
return replacement;
624618
}

packages/schematics/angular/refactor/jasmine-vitest/transformers/jasmine-misc.ts

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
import ts from 'typescript';
1717
import { addVitestValueImport } from '../utils/ast-helpers';
1818
import { getJasmineMethodName, isJasmineCallExpression } from '../utils/ast-validation';
19-
import { addTodoComment } from '../utils/comment-helpers';
19+
import { addCommentedNodeText, addTodoComment } from '../utils/comment-helpers';
2020
import { RefactorContext } from '../utils/refactor-context';
2121
import { createViCallExpression } from '../utils/refactor-helpers';
2222
import { TodoCategory } from '../utils/todo-notes';
@@ -143,7 +143,6 @@ export function transformJasmineMembers(node: ts.Node, refactorCtx: RefactorCont
143143
case 'MAX_PRETTY_PRINT_DEPTH':
144144
case 'MAX_PRETTY_PRINT_CHARS': {
145145
const replacement = ts.factory.createEmptyStatement();
146-
const originalText = node.getFullText().trim();
147146

148147
reporter.reportTransformation(
149148
sourceFile,
@@ -153,12 +152,7 @@ export function transformJasmineMembers(node: ts.Node, refactorCtx: RefactorCont
153152
const category = 'unsupported-jasmine-member';
154153
reporter.recordTodo(category, sourceFile, node);
155154
addTodoComment(replacement, category, { name: memberName });
156-
ts.addSyntheticLeadingComment(
157-
replacement,
158-
ts.SyntaxKind.SingleLineCommentTrivia,
159-
` ${originalText}`,
160-
true,
161-
);
155+
addCommentedNodeText(replacement, node);
162156

163157
return replacement;
164158
}

packages/schematics/angular/refactor/jasmine-vitest/utils/comment-helpers.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,3 +70,23 @@ export function addTodoComment(
7070
true,
7171
);
7272
}
73+
74+
/**
75+
* Safely comments out the full text of a node line-by-line and attaches
76+
* it to a target node. This prevents multi-line statements from breaking
77+
* syntax when converted to single-line comments.
78+
* @param targetNode The node to which the comments will be added.
79+
* @param nodeToComment The original node whose text will be commented out.
80+
*/
81+
export function addCommentedNodeText(targetNode: ts.Node, nodeToComment: ts.Node): void {
82+
const originalText = nodeToComment.getFullText().trim();
83+
const lines = originalText.split('\n');
84+
for (const line of lines) {
85+
ts.addSyntheticLeadingComment(
86+
targetNode,
87+
ts.SyntaxKind.SingleLineCommentTrivia,
88+
` ${line.trim()}`,
89+
true,
90+
);
91+
}
92+
}

packages/schematics/angular/refactor/jasmine-vitest/utils/comment-helpers_spec.ts

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
*/
88

99
import ts from 'typescript';
10-
import { addTodoComment } from './comment-helpers';
10+
import { addCommentedNodeText, addTodoComment } from './comment-helpers';
1111

1212
describe('addTodoComment', () => {
1313
function createTestHarness(sourceText: string) {
@@ -65,4 +65,19 @@ describe('addTodoComment', () => {
6565
expect(result.trim().startsWith('// TODO')).toBe(true);
6666
expect(result).toContain('const mySpy = jasmine.createSpy()');
6767
});
68+
69+
describe('addCommentedNodeText', () => {
70+
it('should comment out a multiline node line-by-line', () => {
71+
const sourceText = `expect()\n .nothing();`;
72+
const sourceFile = ts.createSourceFile('test.ts', sourceText, ts.ScriptTarget.Latest, true);
73+
const statement = sourceFile.statements[0];
74+
const replacement = ts.factory.createEmptyStatement();
75+
const printer = ts.createPrinter({ newLine: ts.NewLineKind.LineFeed });
76+
77+
addCommentedNodeText(replacement, statement);
78+
79+
const result = printer.printNode(ts.EmitHint.Unspecified, replacement, sourceFile);
80+
expect(result).toContain('// expect()\n// .nothing();');
81+
});
82+
});
6883
});

0 commit comments

Comments
 (0)