Skip to content

Commit 2529b86

Browse files
committed
Retain synthetic comments on classes and their properties.
1 parent 1bd79af commit 2529b86

File tree

4 files changed

+44
-0
lines changed

4 files changed

+44
-0
lines changed

src/compiler/transformers/ts.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1245,6 +1245,7 @@ namespace ts {
12451245
const statement = createExpressionStatement(transformInitializedProperty(property, receiver));
12461246
setSourceMapRange(statement, moveRangePastModifiers(property));
12471247
setCommentRange(statement, property);
1248+
setOriginalNode(statement, property);
12481249
statements.push(statement);
12491250
}
12501251
}
@@ -1262,6 +1263,7 @@ namespace ts {
12621263
startOnNewLine(expression);
12631264
setSourceMapRange(expression, moveRangePastModifiers(property));
12641265
setCommentRange(expression, property);
1266+
setOriginalNode(expression, property);
12651267
expressions.push(expression);
12661268
}
12671269

src/testRunner/unittests/transform.ts

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -334,6 +334,29 @@ export {Value};
334334
}
335335
}).outputText;
336336
});
337+
338+
// https://github.com/Microsoft/TypeScript/issues/17594
339+
testBaseline("transformAddCommentToProperties", () => {
340+
return transpileModule(`
341+
// class comment.
342+
class Clazz {
343+
// original comment 1.
344+
static staticProp: number = 1;
345+
// original comment 2.
346+
instanceProp: number = 2;
347+
// original comment 3.
348+
constructor(readonly field = 1) {}
349+
}
350+
`, {
351+
transformers: {
352+
before: [addSyntheticComment(n => isPropertyDeclaration(n) || isParameterPropertyDeclaration(n) || isClassDeclaration(n) || isConstructorDeclaration(n))],
353+
},
354+
compilerOptions: {
355+
target: ScriptTarget.ES2015,
356+
newLine: NewLineKind.CarriageReturnLineFeed,
357+
}
358+
}).outputText;
359+
});
337360
});
338361
}
339362

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
class X {
2+
constructor() {
3+
// new comment!
4+
// original comment.
5+
this.foo = 1;
6+
}
7+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
/*comment*/
2+
class Clazz {
3+
/*comment*/
4+
constructor(/*comment*/
5+
field = 1) {
6+
this.field = field;
7+
/*comment*/
8+
this.instanceProp = 2;
9+
}
10+
}
11+
/*comment*/
12+
Clazz.staticProp = 1;

0 commit comments

Comments
 (0)