diff --git a/src/compiler/emitter.ts b/src/compiler/emitter.ts index d82e577971d..7c457126f19 100644 --- a/src/compiler/emitter.ts +++ b/src/compiler/emitter.ts @@ -930,6 +930,10 @@ module ts { } function emitPropertyDeclaration(node: Declaration) { + if (hasComputedNameButNotSymbol(node)) { + return; + } + emitJsDocComments(node); emitClassMemberDeclarationFlags(node); emitVariableDeclaration(node); @@ -937,11 +941,13 @@ module ts { writeLine(); } - // TODO(jfreeman): Factor out common part of property definition, but treat name differently function emitVariableDeclaration(node: VariableDeclaration) { // If we are emitting property it isn't moduleElement and hence we already know it needs to be emitted // so there is no check needed to see if declaration is visible if (node.kind !== SyntaxKind.VariableDeclaration || resolver.isDeclarationVisible(node)) { + // If this node is a computed name, it can only be a symbol, because we've already skipped + // it if it's not a well known symbol. In that case, the text of the name will be exactly + // what we want, namely the name expression enclosed in brackets. writeTextOfNode(currentSourceFile, node.name); // If optional property emit ? if ((node.kind === SyntaxKind.PropertyDeclaration || node.kind === SyntaxKind.PropertySignature) && hasQuestionToken(node)) { @@ -1028,6 +1034,10 @@ module ts { } function emitAccessorDeclaration(node: AccessorDeclaration) { + if (hasComputedNameButNotSymbol(node)) { + return; + } + var accessors = getAllAccessorDeclarations(node.parent, node); if (node === accessors.firstAccessor) { emitJsDocComments(accessors.getAccessor); @@ -1105,6 +1115,10 @@ module ts { } function emitFunctionDeclaration(node: FunctionLikeDeclaration) { + if (hasComputedNameButNotSymbol(node)) { + return; + } + // If we are emitting Method/Constructor it isn't moduleElement and hence already determined to be emitting // so no need to verify if the declaration is visible if ((node.kind !== SyntaxKind.FunctionDeclaration || resolver.isDeclarationVisible(node)) && diff --git a/tests/baselines/reference/computedPropertyNamesDeclarationEmit1.js b/tests/baselines/reference/computedPropertyNamesDeclarationEmit1.js new file mode 100644 index 00000000000..bc8b5d0e910 --- /dev/null +++ b/tests/baselines/reference/computedPropertyNamesDeclarationEmit1.js @@ -0,0 +1,33 @@ +//// [computedPropertyNamesDeclarationEmit1.ts] +class C { + ["" + ""]() { } + get ["" + ""]() { return 0; } + set ["" + ""](x) { } +} + +//// [computedPropertyNamesDeclarationEmit1.js] +var C = (function () { + function C() { + } + C.prototype["" + ""] = function () { + }; + Object.defineProperty(C.prototype, "" + "", { + get: function () { + return 0; + }, + enumerable: true, + configurable: true + }); + Object.defineProperty(C.prototype, "" + "", { + set: function (x) { + }, + enumerable: true, + configurable: true + }); + return C; +})(); + + +//// [computedPropertyNamesDeclarationEmit1.d.ts] +declare class C { +} diff --git a/tests/baselines/reference/computedPropertyNamesDeclarationEmit1.types b/tests/baselines/reference/computedPropertyNamesDeclarationEmit1.types new file mode 100644 index 00000000000..e8e184a652e --- /dev/null +++ b/tests/baselines/reference/computedPropertyNamesDeclarationEmit1.types @@ -0,0 +1,14 @@ +=== tests/cases/conformance/es6/computedProperties/computedPropertyNamesDeclarationEmit1.ts === +class C { +>C : C + + ["" + ""]() { } +>"" + "" : string + + get ["" + ""]() { return 0; } +>"" + "" : string + + set ["" + ""](x) { } +>"" + "" : string +>x : any +} diff --git a/tests/baselines/reference/computedPropertyNamesDeclarationEmit2.js b/tests/baselines/reference/computedPropertyNamesDeclarationEmit2.js new file mode 100644 index 00000000000..647f3fab135 --- /dev/null +++ b/tests/baselines/reference/computedPropertyNamesDeclarationEmit2.js @@ -0,0 +1,33 @@ +//// [computedPropertyNamesDeclarationEmit2.ts] +class C { + static ["" + ""]() { } + static get ["" + ""]() { return 0; } + static set ["" + ""](x) { } +} + +//// [computedPropertyNamesDeclarationEmit2.js] +var C = (function () { + function C() { + } + C["" + ""] = function () { + }; + Object.defineProperty(C, "" + "", { + get: function () { + return 0; + }, + enumerable: true, + configurable: true + }); + Object.defineProperty(C, "" + "", { + set: function (x) { + }, + enumerable: true, + configurable: true + }); + return C; +})(); + + +//// [computedPropertyNamesDeclarationEmit2.d.ts] +declare class C { +} diff --git a/tests/baselines/reference/computedPropertyNamesDeclarationEmit2.types b/tests/baselines/reference/computedPropertyNamesDeclarationEmit2.types new file mode 100644 index 00000000000..df1da17abea --- /dev/null +++ b/tests/baselines/reference/computedPropertyNamesDeclarationEmit2.types @@ -0,0 +1,14 @@ +=== tests/cases/conformance/es6/computedProperties/computedPropertyNamesDeclarationEmit2.ts === +class C { +>C : C + + static ["" + ""]() { } +>"" + "" : string + + static get ["" + ""]() { return 0; } +>"" + "" : string + + static set ["" + ""](x) { } +>"" + "" : string +>x : any +} diff --git a/tests/baselines/reference/computedPropertyNamesDeclarationEmit3.errors.txt b/tests/baselines/reference/computedPropertyNamesDeclarationEmit3.errors.txt new file mode 100644 index 00000000000..69ff2b9e452 --- /dev/null +++ b/tests/baselines/reference/computedPropertyNamesDeclarationEmit3.errors.txt @@ -0,0 +1,9 @@ +tests/cases/conformance/es6/computedProperties/computedPropertyNamesDeclarationEmit3.ts(2,5): error TS1169: Computed property names are not allowed in interfaces. + + +==== tests/cases/conformance/es6/computedProperties/computedPropertyNamesDeclarationEmit3.ts (1 errors) ==== + interface I { + ["" + ""](): void; + ~~~~~~~~~ +!!! error TS1169: Computed property names are not allowed in interfaces. + } \ No newline at end of file diff --git a/tests/baselines/reference/computedPropertyNamesDeclarationEmit4.errors.txt b/tests/baselines/reference/computedPropertyNamesDeclarationEmit4.errors.txt new file mode 100644 index 00000000000..53eb057f8a1 --- /dev/null +++ b/tests/baselines/reference/computedPropertyNamesDeclarationEmit4.errors.txt @@ -0,0 +1,9 @@ +tests/cases/conformance/es6/computedProperties/computedPropertyNamesDeclarationEmit4.ts(2,5): error TS1170: Computed property names are not allowed in type literals. + + +==== tests/cases/conformance/es6/computedProperties/computedPropertyNamesDeclarationEmit4.ts (1 errors) ==== + var v: { + ["" + ""](): void; + ~~~~~~~~~ +!!! error TS1170: Computed property names are not allowed in type literals. + } \ No newline at end of file diff --git a/tests/baselines/reference/computedPropertyNamesDeclarationEmit5.js b/tests/baselines/reference/computedPropertyNamesDeclarationEmit5.js new file mode 100644 index 00000000000..d7ac3319eca --- /dev/null +++ b/tests/baselines/reference/computedPropertyNamesDeclarationEmit5.js @@ -0,0 +1,23 @@ +//// [computedPropertyNamesDeclarationEmit5.ts] +var v = { + ["" + ""]: 0, + ["" + ""]() { }, + get ["" + ""]() { return 0; }, + set ["" + ""](x) { } +} + +//// [computedPropertyNamesDeclarationEmit5.js] +var v = { + ["" + ""]: 0, + ["" + ""]() { + }, + get ["" + ""]() { + return 0; + }, + set ["" + ""](x) { + } +}; + + +//// [computedPropertyNamesDeclarationEmit5.d.ts] +declare var v: {}; diff --git a/tests/baselines/reference/computedPropertyNamesDeclarationEmit5.types b/tests/baselines/reference/computedPropertyNamesDeclarationEmit5.types new file mode 100644 index 00000000000..c63d6d0d5c3 --- /dev/null +++ b/tests/baselines/reference/computedPropertyNamesDeclarationEmit5.types @@ -0,0 +1,18 @@ +=== tests/cases/conformance/es6/computedProperties/computedPropertyNamesDeclarationEmit5.ts === +var v = { +>v : {} +>{ ["" + ""]: 0, ["" + ""]() { }, get ["" + ""]() { return 0; }, set ["" + ""](x) { }} : {} + + ["" + ""]: 0, +>"" + "" : string + + ["" + ""]() { }, +>"" + "" : string + + get ["" + ""]() { return 0; }, +>"" + "" : string + + set ["" + ""](x) { } +>"" + "" : string +>x : any +} diff --git a/tests/cases/conformance/es6/computedProperties/computedPropertyNamesDeclarationEmit1.ts b/tests/cases/conformance/es6/computedProperties/computedPropertyNamesDeclarationEmit1.ts new file mode 100644 index 00000000000..100c68653c3 --- /dev/null +++ b/tests/cases/conformance/es6/computedProperties/computedPropertyNamesDeclarationEmit1.ts @@ -0,0 +1,7 @@ +// @target: es6 +// @declaration: true +class C { + ["" + ""]() { } + get ["" + ""]() { return 0; } + set ["" + ""](x) { } +} \ No newline at end of file diff --git a/tests/cases/conformance/es6/computedProperties/computedPropertyNamesDeclarationEmit2.ts b/tests/cases/conformance/es6/computedProperties/computedPropertyNamesDeclarationEmit2.ts new file mode 100644 index 00000000000..2f353b854cd --- /dev/null +++ b/tests/cases/conformance/es6/computedProperties/computedPropertyNamesDeclarationEmit2.ts @@ -0,0 +1,7 @@ +// @target: es6 +// @declaration: true +class C { + static ["" + ""]() { } + static get ["" + ""]() { return 0; } + static set ["" + ""](x) { } +} \ No newline at end of file diff --git a/tests/cases/conformance/es6/computedProperties/computedPropertyNamesDeclarationEmit3.ts b/tests/cases/conformance/es6/computedProperties/computedPropertyNamesDeclarationEmit3.ts new file mode 100644 index 00000000000..041a2f153e7 --- /dev/null +++ b/tests/cases/conformance/es6/computedProperties/computedPropertyNamesDeclarationEmit3.ts @@ -0,0 +1,5 @@ +// @target: es6 +// @declaration: true +interface I { + ["" + ""](): void; +} \ No newline at end of file diff --git a/tests/cases/conformance/es6/computedProperties/computedPropertyNamesDeclarationEmit4.ts b/tests/cases/conformance/es6/computedProperties/computedPropertyNamesDeclarationEmit4.ts new file mode 100644 index 00000000000..59a5fe71267 --- /dev/null +++ b/tests/cases/conformance/es6/computedProperties/computedPropertyNamesDeclarationEmit4.ts @@ -0,0 +1,5 @@ +// @target: es6 +// @declaration: true +var v: { + ["" + ""](): void; +} \ No newline at end of file diff --git a/tests/cases/conformance/es6/computedProperties/computedPropertyNamesDeclarationEmit5.ts b/tests/cases/conformance/es6/computedProperties/computedPropertyNamesDeclarationEmit5.ts new file mode 100644 index 00000000000..3f24075abbf --- /dev/null +++ b/tests/cases/conformance/es6/computedProperties/computedPropertyNamesDeclarationEmit5.ts @@ -0,0 +1,8 @@ +// @target: es6 +// @declaration: true +var v = { + ["" + ""]: 0, + ["" + ""]() { }, + get ["" + ""]() { return 0; }, + set ["" + ""](x) { } +} \ No newline at end of file