add support for object literal decorators - fixes #1154

This commit is contained in:
Sebastian McKenzie
2015-04-11 16:30:55 -07:00
parent 2a9777cc20
commit 98c5255b91
22 changed files with 270 additions and 51 deletions

View File

@@ -2787,6 +2787,66 @@ test('class Foo { @bar static foo = "bar"; }', {
features: { "es7.classProperties": true, "es7.decorators": true }
});
test("var obj = { @foo bar: 'wow' };", {
"start": 0,
"body": [{
"start": 0,
"declarations": [{
"start": 4,
"id": {
"start": 4,
"name": "obj",
"type": "Identifier",
"end": 7
},
"init": {
"start": 10,
"properties": [{
"start": 17,
"key": {
"start": 17,
"name": "bar",
"type": "Identifier",
"end": 20
},
"value": {
"start": 22,
"value": "wow",
"raw": "'wow'",
"type": "Literal",
"end": 27
},
"kind": "init",
"decorators": [{
"start": 12,
"expression": {
"start": 13,
"name": "foo",
"type": "Identifier",
"end": 16
},
"type": "Decorator",
"end": 16
}],
"type": "Property",
"end": 27
}],
"type": "ObjectExpression",
"end": 29
},
"type": "VariableDeclarator",
"end": 29
}],
"kind": "var",
"type": "VariableDeclaration",
"end": 30
}],
"type": "Program",
"end": 30
}, {
features: { "es7.decorators": true }
});
// ES7 export extensions - https://github.com/leebyron/ecmascript-more-export-from
test('export foo, { bar } from "bar";', {

View File

@@ -10,11 +10,11 @@ var Foo = (function () {
babelHelpers.createDecoratedClass(Foo, [{
key: "foo",
enumerable: true,
decorators: [bar],
initializer: function () {
return "Bar";
}
},
enumerable: true
}], null, _instanceInitializers);
return Foo;
})();
})();

View File

@@ -9,12 +9,12 @@ var Foo = (function () {
babelHelpers.createDecoratedClass(Foo, null, [{
key: "foo",
enumerable: true,
decorators: [bar],
initializer: function () {
return "Bar";
}
},
enumerable: true
}], null, _staticInitializers);
Foo.foo = _staticInitializers.foo.call(Foo);
return Foo;
})();
})();

View File

@@ -10,9 +10,9 @@ var Foo = (function () {
babelHelpers.createDecoratedClass(Foo, [{
key: "foo",
enumerable: true,
decorators: [bar],
initializer: function () {}
initializer: function () {},
enumerable: true
}], null, _instanceInitializers);
return Foo;
})();
})();

View File

@@ -9,10 +9,10 @@ var Foo = (function () {
babelHelpers.createDecoratedClass(Foo, null, [{
key: "foo",
enumerable: true,
decorators: [bar],
initializer: function () {}
initializer: function () {},
enumerable: true
}], null, _staticInitializers);
Foo.foo = _staticInitializers.foo.call(Foo);
return Foo;
})();
})();

View File

@@ -0,0 +1,20 @@
var autobind = function (target, name, descriptor) {
var fn = descriptor.value;
delete descriptor.value;
delete descriptor.writable;
descriptor.get = function () {
return fn.bind(this);
};
};
var person = {
first: "Sebastian",
last: "McKenzie",
@autobind
getName() {
return `${this.first} ${this.last}`;
}
}
assert.equal(person.getName.call(null), "Sebastian McKenzie")

View File

@@ -0,0 +1,11 @@
var obj = {
@foo
get foo() {
},
@foo
set foo() {
}
};

View File

@@ -0,0 +1,8 @@
"use strict";
var obj = babelHelpers.createDecoratedObject([{
key: "foo",
decorators: [foo, foo],
get: function get() {},
set: function set() {}
}]);

View File

@@ -0,0 +1,6 @@
var obj = {
@foo
get foo() {
}
};

View File

@@ -0,0 +1,7 @@
"use strict";
var obj = babelHelpers.createDecoratedObject([{
key: "foo",
decorators: [foo],
get: function get() {}
}]);

View File

@@ -0,0 +1,6 @@
var obj = {
@foo
set foo() {
}
};

View File

@@ -0,0 +1,7 @@
"use strict";
var obj = babelHelpers.createDecoratedObject([{
key: "foo",
decorators: [foo],
set: function set() {}
}]);

View File

@@ -0,0 +1,11 @@
var obj = {
@foo
bar() {
},
@bar
foo: "lol",
yes: "wow"
};

View File

@@ -0,0 +1,14 @@
"use strict";
var obj = babelHelpers.createDecoratedObject([{
key: "bar",
decorators: [foo],
value: function value() {}
}, {
key: "foo",
decorators: [bar],
value: "lol"
}, {
key: "yes",
value: "wow"
}]);