Skip to content
This repository was archived by the owner on Jan 19, 2019. It is now read-only.
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 328259f

Browse files
JamesHenrynzakas
authored andcommittedAug 25, 2016
New: Add param decorators to the AST (fixes #68) (#69)
1 parent 8b97fe7 commit 328259f

14 files changed

+3359
-5
lines changed
 

‎.gitignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,4 @@ coverage
33
node_modules
44
npm-debug.log
55
_test.js
6-
6+
.DS_Store

‎lib/ast-converter.js

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -956,7 +956,6 @@ module.exports = function(ast, extra) {
956956
method = {
957957
type: "FunctionExpression",
958958
id: null,
959-
params: node.parameters.map(convertChild),
960959
generator: false,
961960
expression: false,
962961
body: convertChild(node.body),
@@ -975,6 +974,9 @@ module.exports = function(ast, extra) {
975974
}
976975

977976
if (parent.kind === SyntaxKind.ObjectLiteralExpression) {
977+
978+
method.params = node.parameters.map(convertChild);
979+
978980
assign(result, {
979981
type: "Property",
980982
key: convertChild(node.name),
@@ -986,7 +988,20 @@ module.exports = function(ast, extra) {
986988
});
987989

988990
} else { // class
991+
992+
/**
993+
* Unlinke in object literal methods, class method params can have decorators
994+
*/
995+
method.params = node.parameters.map(function(param) {
996+
var convertedParam = convertChild(param);
997+
convertedParam.decorators = (param.decorators) ? param.decorators.map(function(d) {
998+
return convertChild(d.expression);
999+
}) : [];
1000+
return convertedParam;
1001+
});
1002+
9891003
var methodNameIsComputed = (node.name.kind === SyntaxKind.ComputedPropertyName);
1004+
9901005
assign(result, {
9911006
type: "MethodDefinition",
9921007
key: convertChild(node.name),
@@ -998,6 +1013,7 @@ module.exports = function(ast, extra) {
9981013
return convertChild(d.expression);
9991014
}) : []
10001015
});
1016+
10011017
}
10021018

10031019
if (node.kind === SyntaxKind.GetAccessor) {
@@ -1020,7 +1036,13 @@ module.exports = function(ast, extra) {
10201036
constructor = {
10211037
type: "FunctionExpression",
10221038
id: null,
1023-
params: node.parameters.map(convertChild),
1039+
params: node.parameters.map(function(param) {
1040+
var convertedParam = convertChild(param);
1041+
convertedParam.decorators = (param.decorators) ? param.decorators.map(function(d) {
1042+
return convertChild(d.expression);
1043+
}) : [];
1044+
return convertedParam;
1045+
}),
10241046
generator: false,
10251047
expression: false,
10261048
body: convertChild(node.body),

‎tests/fixtures/ecma-features/classes/class-accessor-properties.result.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -221,7 +221,8 @@ module.exports = {
221221
25,
222222
26
223223
],
224-
"name": "c"
224+
"name": "c",
225+
"decorators": []
225226
}
226227
],
227228
"body": {

‎tests/fixtures/ecma-features/classes/class-static-methods-and-accessor-properties.result.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -299,7 +299,8 @@ module.exports = {
299299
52,
300300
53
301301
],
302-
"name": "b"
302+
"name": "b",
303+
"decorators": []
303304
}
304305
],
305306
"body": {

‎tests/fixtures/typescript/decorators/parameter-decorators/parameter-decorator-constructor.result.js

Lines changed: 825 additions & 0 deletions
Large diffs are not rendered by default.
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
class Service {
2+
constructor(@Inject(APP_CONFIG) config: AppConfig) {
3+
this.title = config.title;
4+
}
5+
}

‎tests/fixtures/typescript/decorators/parameter-decorators/parameter-decorator-decorator-instance-member.result.js

Lines changed: 540 additions & 0 deletions
Large diffs are not rendered by default.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
class Foo {
2+
bar(@special(true) baz: number) {}
3+
}

‎tests/fixtures/typescript/decorators/parameter-decorators/parameter-decorator-decorator-static-member.result.js

Lines changed: 558 additions & 0 deletions
Large diffs are not rendered by default.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
class StaticFoo {
2+
static bar(@special(true) baz: number) {}
3+
}

‎tests/fixtures/typescript/decorators/parameter-decorators/parameter-decorator-instance-member.result.js

Lines changed: 684 additions & 0 deletions
Large diffs are not rendered by default.
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
class Greeter {
2+
greet(@required name: string) {
3+
return "Hello " + name + "!";
4+
}
5+
}

‎tests/fixtures/typescript/decorators/parameter-decorators/parameter-decorator-static-member.result.js

Lines changed: 702 additions & 0 deletions
Large diffs are not rendered by default.
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
class StaticGreeter {
2+
static greet(@required name: string) {
3+
return "Hello " + name + "!";
4+
}
5+
}

0 commit comments

Comments
 (0)
This repository has been archived.