Skip computed properties on declaration emit

This commit is contained in:
Jason Freeman
2015-01-20 18:29:04 -08:00
parent 26da3782b5
commit eb7798fbb2
14 changed files with 200 additions and 1 deletions

View File

@@ -930,6 +930,10 @@ module ts {
}
function emitPropertyDeclaration(node: Declaration) {
if (hasComputedNameButNotSymbol(node)) {
return;
}
emitJsDocComments(node);
emitClassMemberDeclarationFlags(node);
emitVariableDeclaration(<VariableDeclaration>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(<ClassDeclaration>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)) &&

View File

@@ -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 {
}

View File

@@ -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
}

View File

@@ -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 {
}

View File

@@ -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
}

View File

@@ -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.
}

View File

@@ -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.
}

View File

@@ -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: {};

View File

@@ -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
}

View File

@@ -0,0 +1,7 @@
// @target: es6
// @declaration: true
class C {
["" + ""]() { }
get ["" + ""]() { return 0; }
set ["" + ""](x) { }
}

View File

@@ -0,0 +1,7 @@
// @target: es6
// @declaration: true
class C {
static ["" + ""]() { }
static get ["" + ""]() { return 0; }
static set ["" + ""](x) { }
}

View File

@@ -0,0 +1,5 @@
// @target: es6
// @declaration: true
interface I {
["" + ""](): void;
}

View File

@@ -0,0 +1,5 @@
// @target: es6
// @declaration: true
var v: {
["" + ""](): void;
}

View File

@@ -0,0 +1,8 @@
// @target: es6
// @declaration: true
var v = {
["" + ""]: 0,
["" + ""]() { },
get ["" + ""]() { return 0; },
set ["" + ""](x) { }
}