From 1696da14f230e70f4a90d61c5ba52b69982cff17 Mon Sep 17 00:00:00 2001 From: Sheetal Nandi Date: Thu, 31 May 2018 12:40:08 -0700 Subject: [PATCH] Do not emit trailing comma in json module Fixes #24530 --- src/compiler/emitter.ts | 2 +- .../requireOfJsonFileWithTraillingComma.js | 32 +++++++++++++ ...equireOfJsonFileWithTraillingComma.symbols | 38 ++++++++++++++++ .../requireOfJsonFileWithTraillingComma.types | 45 +++++++++++++++++++ .../requireOfJsonFileWithTraillingComma.ts | 20 +++++++++ 5 files changed, 136 insertions(+), 1 deletion(-) create mode 100644 tests/baselines/reference/requireOfJsonFileWithTraillingComma.js create mode 100644 tests/baselines/reference/requireOfJsonFileWithTraillingComma.symbols create mode 100644 tests/baselines/reference/requireOfJsonFileWithTraillingComma.types create mode 100644 tests/cases/compiler/requireOfJsonFileWithTraillingComma.ts diff --git a/src/compiler/emitter.ts b/src/compiler/emitter.ts index 5e53605dec..5af58a1167 100644 --- a/src/compiler/emitter.ts +++ b/src/compiler/emitter.ts @@ -1458,7 +1458,7 @@ namespace ts { } const preferNewLine = node.multiLine ? ListFormat.PreferNewLine : ListFormat.None; - const allowTrailingComma = currentSourceFile.languageVersion >= ScriptTarget.ES5 ? ListFormat.AllowTrailingComma : ListFormat.None; + const allowTrailingComma = currentSourceFile.languageVersion >= ScriptTarget.ES5 && !isJsonSourceFile(currentSourceFile) ? ListFormat.AllowTrailingComma : ListFormat.None; emitList(node, node.properties, ListFormat.ObjectLiteralExpressionProperties | allowTrailingComma | preferNewLine); if (indentedFlag) { diff --git a/tests/baselines/reference/requireOfJsonFileWithTraillingComma.js b/tests/baselines/reference/requireOfJsonFileWithTraillingComma.js new file mode 100644 index 0000000000..332112b356 --- /dev/null +++ b/tests/baselines/reference/requireOfJsonFileWithTraillingComma.js @@ -0,0 +1,32 @@ +//// [tests/cases/compiler/requireOfJsonFileWithTraillingComma.ts] //// + +//// [file1.ts] +import b1 = require('./b.json'); +let x = b1.a; +import b2 = require('./b.json'); +if (x) { + let b = b2.b; + x = (b1.b === b); +} + +//// [b.json] +{ + "a": true, + "b": "hello", +} + +//// [out/b.json] +{ + "a": true, + "b": "hello" +} +//// [out/file1.js] +"use strict"; +exports.__esModule = true; +var b1 = require("./b.json"); +var x = b1.a; +var b2 = require("./b.json"); +if (x) { + var b = b2.b; + x = (b1.b === b); +} diff --git a/tests/baselines/reference/requireOfJsonFileWithTraillingComma.symbols b/tests/baselines/reference/requireOfJsonFileWithTraillingComma.symbols new file mode 100644 index 0000000000..79eac57c9f --- /dev/null +++ b/tests/baselines/reference/requireOfJsonFileWithTraillingComma.symbols @@ -0,0 +1,38 @@ +=== tests/cases/compiler/file1.ts === +import b1 = require('./b.json'); +>b1 : Symbol(b1, Decl(file1.ts, 0, 0)) + +let x = b1.a; +>x : Symbol(x, Decl(file1.ts, 1, 3)) +>b1.a : Symbol("a", Decl(b.json, 0, 1)) +>b1 : Symbol(b1, Decl(file1.ts, 0, 0)) +>a : Symbol("a", Decl(b.json, 0, 1)) + +import b2 = require('./b.json'); +>b2 : Symbol(b2, Decl(file1.ts, 1, 13)) + +if (x) { +>x : Symbol(x, Decl(file1.ts, 1, 3)) + + let b = b2.b; +>b : Symbol(b, Decl(file1.ts, 4, 7)) +>b2.b : Symbol("b", Decl(b.json, 1, 14)) +>b2 : Symbol(b2, Decl(file1.ts, 1, 13)) +>b : Symbol("b", Decl(b.json, 1, 14)) + + x = (b1.b === b); +>x : Symbol(x, Decl(file1.ts, 1, 3)) +>b1.b : Symbol("b", Decl(b.json, 1, 14)) +>b1 : Symbol(b1, Decl(file1.ts, 0, 0)) +>b : Symbol("b", Decl(b.json, 1, 14)) +>b : Symbol(b, Decl(file1.ts, 4, 7)) +} + +=== tests/cases/compiler/b.json === +{ + "a": true, +>"a" : Symbol("a", Decl(b.json, 0, 1)) + + "b": "hello", +>"b" : Symbol("b", Decl(b.json, 1, 14)) +} diff --git a/tests/baselines/reference/requireOfJsonFileWithTraillingComma.types b/tests/baselines/reference/requireOfJsonFileWithTraillingComma.types new file mode 100644 index 0000000000..17e637a6df --- /dev/null +++ b/tests/baselines/reference/requireOfJsonFileWithTraillingComma.types @@ -0,0 +1,45 @@ +=== tests/cases/compiler/file1.ts === +import b1 = require('./b.json'); +>b1 : { "a": boolean; "b": string; } + +let x = b1.a; +>x : boolean +>b1.a : boolean +>b1 : { "a": boolean; "b": string; } +>a : boolean + +import b2 = require('./b.json'); +>b2 : { "a": boolean; "b": string; } + +if (x) { +>x : boolean + + let b = b2.b; +>b : string +>b2.b : string +>b2 : { "a": boolean; "b": string; } +>b : string + + x = (b1.b === b); +>x = (b1.b === b) : boolean +>x : boolean +>(b1.b === b) : boolean +>b1.b === b : boolean +>b1.b : string +>b1 : { "a": boolean; "b": string; } +>b : string +>b : string +} + +=== tests/cases/compiler/b.json === +{ +>{ "a": true, "b": "hello",} : { "a": boolean; "b": string; } + + "a": true, +>"a" : boolean +>true : true + + "b": "hello", +>"b" : string +>"hello" : "hello" +} diff --git a/tests/cases/compiler/requireOfJsonFileWithTraillingComma.ts b/tests/cases/compiler/requireOfJsonFileWithTraillingComma.ts new file mode 100644 index 0000000000..de1f3b24a7 --- /dev/null +++ b/tests/cases/compiler/requireOfJsonFileWithTraillingComma.ts @@ -0,0 +1,20 @@ +// @module: commonjs +// @outdir: out/ +// @allowJs: true +// @fullEmitPaths: true +// @resolveJsonModule: true + +// @Filename: file1.ts +import b1 = require('./b.json'); +let x = b1.a; +import b2 = require('./b.json'); +if (x) { + let b = b2.b; + x = (b1.b === b); +} + +// @Filename: b.json +{ + "a": true, + "b": "hello", +} \ No newline at end of file