Skip to content

Commit 4119a3f

Browse files
committed
[Transforms] Fix emit comment in synthesized function expression (microsoft#8234)
* Do not emit leading comment of synthesized function expression in object literal property assignment * Update baselines
1 parent 3667b30 commit 4119a3f

File tree

4 files changed

+22
-8
lines changed

4 files changed

+22
-8
lines changed

src/compiler/printer.ts

+3-1
Original file line numberDiff line numberDiff line change
@@ -1981,7 +1981,9 @@ const _super = (function (geti, seti) {
19811981
// }
19821982
// "comment1" is not considered to be leading comment for node.initializer
19831983
// but rather a trailing comment on the previous node.
1984-
emitLeadingComments(node.initializer, getTrailingComments(collapseRangeToStart(node.initializer)));
1984+
if (!shouldSkipLeadingCommentsForNode(node.initializer)) {
1985+
emitLeadingComments(node.initializer, getTrailingComments(collapseRangeToStart(node.initializer)));
1986+
}
19851987
emitExpression(node.initializer);
19861988
}
19871989

src/compiler/transformers/es6.ts

+17-3
Original file line numberDiff line numberDiff line change
@@ -1175,13 +1175,25 @@ namespace ts {
11751175
const propertyName = createExpressionForPropertyName(visitNode(firstAccessor.name, visitor, isPropertyName));
11761176
propertyName.end = firstAccessor.name.end;
11771177

1178+
let getAccessorExpression: FunctionExpression;
1179+
if (getAccessor) {
1180+
getAccessorExpression = transformFunctionLikeToExpression(getAccessor, /*location*/ getAccessor, /*name*/ undefined);
1181+
setNodeEmitFlags(getAccessorExpression, NodeEmitFlags.NoLeadingComments | getNodeEmitFlags(getAccessorExpression));
1182+
}
1183+
1184+
let setAccessorExpression: FunctionExpression;
1185+
if (setAccessor) {
1186+
setAccessorExpression = transformFunctionLikeToExpression(setAccessor, /*location*/ setAccessor, /*name*/ undefined);
1187+
setNodeEmitFlags(setAccessorExpression, NodeEmitFlags.NoLeadingComments | getNodeEmitFlags(setAccessorExpression));
1188+
}
1189+
11781190
return setNodeEmitFlags(
11791191
createObjectDefineProperty(
11801192
target,
11811193
propertyName,
11821194
/*descriptor*/ {
1183-
get: getAccessor && transformFunctionLikeToExpression(getAccessor, /*location*/ getAccessor, /*name*/ undefined),
1184-
set: setAccessor && transformFunctionLikeToExpression(setAccessor, /*location*/ setAccessor, /*name*/ undefined),
1195+
get: getAccessorExpression,
1196+
set: setAccessorExpression,
11851197
enumerable: true,
11861198
configurable: true
11871199
},
@@ -2283,9 +2295,11 @@ namespace ts {
22832295
// Methods on classes are handled in visitClassDeclaration/visitClassExpression.
22842296
// Methods with computed property names are handled in visitObjectLiteralExpression.
22852297
Debug.assert(!isComputedPropertyName(node.name));
2298+
const functionExpression = transformFunctionLikeToExpression(node, /*location*/ node, /*name*/ undefined);
2299+
setNodeEmitFlags(functionExpression, NodeEmitFlags.NoLeadingComments | getNodeEmitFlags(functionExpression));
22862300
return createPropertyAssignment(
22872301
node.name,
2288-
transformFunctionLikeToExpression(node, /*location*/ node, /*name*/ undefined),
2302+
functionExpression,
22892303
/*location*/ node
22902304
);
22912305
}

tests/baselines/reference/callSignaturesWithParameterInitializers2.js

+1-2
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,7 @@ var b = {
4848
foo: function (x) {
4949
if (x === void 0) { x = 1; }
5050
},
51-
foo: // error
52-
function (x) {
51+
foo: function (x) {
5352
if (x === void 0) { x = 1; }
5453
}
5554
};

tests/baselines/reference/stringIndexerConstrainsPropertyDeclarations.js

+1-2
Original file line numberDiff line numberDiff line change
@@ -103,8 +103,7 @@ var C = (function () {
103103
function C() {
104104
} // ok
105105
Object.defineProperty(C.prototype, "X", {
106-
get: // error
107-
function () {
106+
get: function () {
108107
return '';
109108
},
110109
set: function (v) { } // ok

0 commit comments

Comments
 (0)