Skip to content

Commit b3770e7

Browse files
babakksjakebailey
andauthored
šŸ› Fix not emitting comments between sibling fields of object literals (#50097)
Co-authored-by: Jake Bailey <[email protected]>
1 parent e654f96 commit b3770e7

26 files changed

+100
-28
lines changed

ā€Žsrc/compiler/emitter.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5121,6 +5121,11 @@ export function createPrinter(printerOptions: PrinterOptions = {}, handlers: Pri
51215121
shouldDecreaseIndentAfterEmit = true;
51225122
}
51235123

5124+
if (shouldEmitInterveningComments && format & ListFormat.DelimitersMask && !positionIsSynthesized(child.pos)) {
5125+
const commentRange = getCommentRange(child);
5126+
emitTrailingCommentsOfPosition(commentRange.pos, /*prefixSpace*/ !!(format & ListFormat.SpaceBetweenSiblings), /*forceNoNewline*/ true);
5127+
}
5128+
51245129
writeLine(separatingLineTerminatorCount);
51255130
shouldEmitInterveningComments = false;
51265131
}

ā€Žtests/baselines/reference/arrowFunctionParsingDoesNotConfuseParenthesizedObjectForArrowHead.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ const test = () => ({
2020
//// [arrowFunctionParsingDoesNotConfuseParenthesizedObjectForArrowHead.js]
2121
var test = function () { return ({
2222
// "Identifier expected." error on "!" and two "Duplicate identifier '(Missing)'." errors on space.
23-
prop: !value,
23+
prop: !value, // remove ! to see that errors will be gone
2424
run: function () {
2525
// comment next line or remove "()" to see that errors will be gone
2626
if (!a.b()) {

ā€Žtests/baselines/reference/assignmentCompatBug3.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,8 @@ foo(x + y);
3030
//// [assignmentCompatBug3.js]
3131
function makePoint(x, y) {
3232
return {
33-
get x() { return x; },
34-
get y() { return y; },
33+
get x() { return x; }, // shouldn't be "void"
34+
get y() { return y; }, // shouldn't be "void"
3535
//x: "yo",
3636
//y: "boo",
3737
dist: function () {

ā€Žtests/baselines/reference/bindingPatternCannotBeOnlyInferenceSource.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ var _d = useReduxDispatch1(function (d, f) { return ({
4545
p[_i] = arguments[_i];
4646
}
4747
return d(f.funcA.apply(f, p));
48-
},
48+
}, // p should be inferrable
4949
funcB: function () {
5050
var p = [];
5151
for (var _i = 0; _i < arguments.length; _i++) {

ā€Žtests/baselines/reference/callSignaturesWithParameterInitializers2.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ c.foo(1);
4949
var b = {
5050
foo: function (x) {
5151
if (x === void 0) { x = 1; }
52-
},
52+
}, // error
5353
foo: function (x) {
5454
if (x === void 0) { x = 1; }
5555
},

ā€Žtests/baselines/reference/circularResolvedSignature.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
2424
exports.Component = void 0;
2525
function Component() {
2626
var _a = useState(function () { return ({
27-
value: "string",
27+
value: "string", // this should be a number
2828
foo: function (arg) { return setState(arg); },
2929
bar: function (arg) { return setState(arg); },
3030
}); }), state = _a[0], setState = _a[1];
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
//// [tests/cases/compiler/commentsOnObjectLiteral5.ts] ////
2+
3+
//// [commentsOnObjectLiteral5.ts]
4+
const a = {
5+
p0: 0, // Comment 0
6+
p1: 0, /* Comment 1
7+
A multiline comment. */
8+
p2: 0, // Comment 2
9+
};
10+
11+
12+
//// [commentsOnObjectLiteral5.js]
13+
var a = {
14+
p0: 0, // Comment 0
15+
p1: 0, /* Comment 1
16+
A multiline comment. */
17+
p2: 0, // Comment 2
18+
};
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
//// [tests/cases/compiler/commentsOnObjectLiteral5.ts] ////
2+
3+
=== commentsOnObjectLiteral5.ts ===
4+
const a = {
5+
>a : Symbol(a, Decl(commentsOnObjectLiteral5.ts, 0, 5))
6+
7+
p0: 0, // Comment 0
8+
>p0 : Symbol(p0, Decl(commentsOnObjectLiteral5.ts, 0, 11))
9+
10+
p1: 0, /* Comment 1
11+
>p1 : Symbol(p1, Decl(commentsOnObjectLiteral5.ts, 1, 10))
12+
13+
A multiline comment. */
14+
p2: 0, // Comment 2
15+
>p2 : Symbol(p2, Decl(commentsOnObjectLiteral5.ts, 2, 10))
16+
17+
};
18+
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
//// [tests/cases/compiler/commentsOnObjectLiteral5.ts] ////
2+
3+
=== commentsOnObjectLiteral5.ts ===
4+
const a = {
5+
>a : { p0: number; p1: number; p2: number; }
6+
>{ p0: 0, // Comment 0 p1: 0, /* Comment 1 A multiline comment. */ p2: 0, // Comment 2} : { p0: number; p1: number; p2: number; }
7+
8+
p0: 0, // Comment 0
9+
>p0 : number
10+
>0 : 0
11+
12+
p1: 0, /* Comment 1
13+
>p1 : number
14+
>0 : 0
15+
16+
A multiline comment. */
17+
p2: 0, // Comment 2
18+
>p2 : number
19+
>0 : 0
20+
21+
};
22+

ā€Žtests/baselines/reference/divergentAccessorsTypes6.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,9 +49,9 @@ var o1 = {
4949
};
5050
// A setter annotation still implies the getter return type.
5151
var o2 = {
52-
get p1() { return 0; },
52+
get p1() { return 0; }, // error - no annotation means type is implied from the setter annotation
5353
set p1(value) { },
54-
get p2() { return 0; },
54+
get p2() { return 0; }, // ok - explicit annotation
5555
set p2(value) { },
5656
};
5757

ā€Žtests/baselines/reference/duplicateObjectLiteralProperty.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,9 @@ var y = {
2323
//// [duplicateObjectLiteralProperty.js]
2424
var x = {
2525
a: 1,
26-
b: true,
27-
a: 56,
28-
\u0061: "ss",
26+
b: true, // OK
27+
a: 56, // Duplicate
28+
\u0061: "ss", // Duplicate
2929
a: {
3030
c: 1,
3131
"c": 56, // Duplicate

ā€Žtests/baselines/reference/excessPropertyCheckWithMultipleDiscriminants.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
149149
var foo = {
150150
type: "number",
151151
value: 10,
152-
multipleOf: 5,
152+
multipleOf: 5, // excess property
153153
format: "what?"
154154
};
155155
// This has excess error because variant three is the only applicable case.

ā€Žtests/baselines/reference/excessPropertyCheckWithNestedArrayIntersection.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ const repro: BugRepro = {
2929
var repro = {
3030
dataType: {
3131
fields: [{
32-
key: 'bla',
32+
key: 'bla', // should be OK: Not excess
3333
value: null,
3434
}],
3535
}

ā€Žtests/baselines/reference/indexSignatures1.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -356,7 +356,7 @@ const y2 = dom.data123;
356356
dom = { data123: 'hello' };
357357
dom = { date123: 'hello' }; // Error
358358
const funcs = {
359-
sfoo: x => x.length,
359+
sfoo: x => x.length, // x: string
360360
nfoo: x => x * 2, // n: number
361361
};
362362
i1[s0]; // Error

ā€Žtests/baselines/reference/namespaceImportTypeQuery2.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,6 @@ exports.B = B;
3838
"use strict";
3939
Object.defineProperty(exports, "__esModule", { value: true });
4040
var t = {
41-
A: undefined,
41+
A: undefined, // ok
4242
B: undefined,
4343
};

ā€Žtests/baselines/reference/namespaceImportTypeQuery3.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,6 @@ exports.B = B;
3131
"use strict";
3232
Object.defineProperty(exports, "__esModule", { value: true });
3333
var t = {
34-
A: undefined,
34+
A: undefined, // ok
3535
B: undefined,
3636
};

ā€Žtests/baselines/reference/namespaceImportTypeQuery4.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,6 @@ exports.B = B;
2929
"use strict";
3030
Object.defineProperty(exports, "__esModule", { value: true });
3131
var t = {
32-
A: undefined,
32+
A: undefined, // error
3333
B: undefined,
3434
};

ā€Žtests/baselines/reference/objectLitArrayDeclNoNew.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ var Test;
4343
function bug() {
4444
var state = null;
4545
return {
46-
tokens: Gar[],
46+
tokens: Gar[], //IToken[], // Missing new. Correct syntax is: tokens: new IToken[]
4747
endState: state
4848
};
4949
}

ā€Žtests/baselines/reference/objectLiteralShorthandPropertiesErrorFromNoneExistingIdentifier.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,6 @@ var x = {
99

1010
//// [objectLiteralShorthandPropertiesErrorFromNoneExistingIdentifier.js]
1111
var x = {
12-
x: x,
12+
x: x, // OK
1313
undefinedVariable: undefinedVariable // Error
1414
};

ā€Žtests/baselines/reference/privateNameBadDeclaration.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,8 @@ class C {
2222
//// [privateNameBadDeclaration.js]
2323
function A() { }
2424
A.prototype = {
25-
: 1,
26-
: function () { },
25+
: 1, // Error
26+
: function () { }, // Error
2727
get () { return ""; } // Error
2828
};
2929
var B = /** @class */ (function () {
@@ -32,8 +32,8 @@ var B = /** @class */ (function () {
3232
return B;
3333
}());
3434
B.prototype = {
35-
: 2,
36-
: function () { },
35+
: 2, // Error
36+
: function () { }, // Error
3737
get () { return ""; } // Error
3838
};
3939
var C = /** @class */ (function () {

ā€Žtests/baselines/reference/thisTypeInFunctions.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -254,7 +254,7 @@ function implicitThis(n) {
254254
}
255255
var impl = {
256256
a: 12,
257-
explicitVoid2: function () { return _this.a; },
257+
explicitVoid2: function () { return _this.a; }, // ok, this: any because it refers to some outer object (window?)
258258
explicitVoid1: function () { return 12; },
259259
explicitStructural: function () {
260260
return this.a;

ā€Žtests/baselines/reference/thisTypeInFunctionsNegative.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -227,7 +227,7 @@ let impl = {
227227
explicitVoid1() {
228228
return this.a; // error, no 'a' in 'void'
229229
},
230-
explicitVoid2: () => this.a,
230+
explicitVoid2: () => this.a, // ok, `this:any` because it refers to an outer object
231231
explicitStructural: () => 12,
232232
explicitInterface: () => 12,
233233
explicitThis() {

ā€Žtests/baselines/reference/typeSatisfaction_contextualTyping2.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ let obj: { f(s: string): void } & Record<string, unknown> = {
1313
//// [typeSatisfaction_contextualTyping2.js]
1414
"use strict";
1515
var obj = {
16-
f: function (s) { },
16+
f: function (s) { }, // "incorrect" implicit any on 's'
1717
g: function (s) { }
1818
};
1919
// This needs to not crash (outer node is not expression)

ā€Žtests/baselines/reference/typeSatisfaction_propertyValueConformance3.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,6 @@ exports.Palette = void 0;
1818
// All of these should be Colors, but I only use some of them here.
1919
exports.Palette = {
2020
white: { r: 255, g: 255, b: 255 },
21-
black: { r: 0, g: 0, d: 0 },
21+
black: { r: 0, g: 0, d: 0 }, // <- oops! 'd' in place of 'b'
2222
blue: { r: 0, g: 0, b: 255 },
2323
};

ā€Žtests/baselines/reference/variableDeclaratorResolvedDuringContextualTyping.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,7 @@ var FileService = /** @class */ (function () {
153153
data: "someData"
154154
}).then(function (response) {
155155
var result = {
156-
stat: _this.jsonToStat(newFilePath, "someString"),
156+
stat: _this.jsonToStat(newFilePath, "someString"), // _this needs to be emitted to the js file
157157
isNew: response.status === 201
158158
};
159159
return WinJS.TPromise.as(result);
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
// @removeComments: false
2+
// @target: ES5
3+
4+
const a = {
5+
p0: 0, // Comment 0
6+
p1: 0, /* Comment 1
7+
A multiline comment. */
8+
p2: 0, // Comment 2
9+
};

0 commit comments

Comments
Ā (0)