Skip to content

Commit

Permalink
patch escope to visit decorators - fixes babel#72
Browse files Browse the repository at this point in the history
  • Loading branch information
hzoo committed May 20, 2015
1 parent f9e5fe4 commit 79b5b4a
Show file tree
Hide file tree
Showing 2 changed files with 122 additions and 1 deletion.
29 changes: 29 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,35 @@ function monkeypatch() {
estraverse.VisitorKeys.TypeAlias = TypeAliasKeys;
return results;
};

// monkeypatch escope/referencer
var referencerLoc = Module._resolveFilename("./referencer", escopeMod);
var referencer = require(referencerLoc);

// if there are decotators, then visit each
function visitDecorators(node) {
if (node.decorators) {
for (var i in node.decorators) {
if (node.decorators[i].expression) {
this.visit(node.decorators[i]);
}
}
}
}

// monkeypatch referencer methods to visit decorators
var visitClass = referencer.prototype.visitClass;
referencer.prototype.visitClass = function (node) {
// visit decorators that are in: Class Declaration
visitDecorators.call(this, node);
visitClass.call(this, node);
}
var visitProperty = referencer.prototype.visitProperty;
referencer.prototype.visitProperty = function (node) {
// visit decorators that are in: Visit Property / MethodDefinition
visitDecorators.call(this, node);
visitProperty.call(this, node);
}
}

exports.attachComments = function (ast, comments, tokens) {
Expand Down
94 changes: 93 additions & 1 deletion test/non-regression.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ describe("verify", function () {
verifyAndAssertMessages(
"import Foo from 'foo';\n" +
"export default Foo;",
{ },
{},
[]
);
});
Expand Down Expand Up @@ -163,4 +163,96 @@ describe("verify", function () {
[]
);
});

describe("decorators #72", function () {
it("class declaration", function () {
verifyAndAssertMessages(
[
"import classDeclaration from 'decorator';",
"import decoratorParameter from 'decorator';",
"@classDeclaration(decoratorParameter)",
"@classDeclaration",
"class TextareaAutosize {}"
].join("\n"),
{ "no-unused-vars": 1 },
[]
);
});

it("method definition", function () {
verifyAndAssertMessages(
[
"import classMethodDeclarationA from 'decorator';",
"import decoratorParameter from 'decorator';",
"class TextareaAutosize {",
"@classMethodDeclarationA(decoratorParameter)",
"@classMethodDeclarationA",
"methodDeclaration(e) {",
"e();",
"}",
"}"
].join("\n"),
{ "no-unused-vars": 1 },
[]
);
});

it("method definition get/set", function () {
verifyAndAssertMessages(
[
"import classMethodDeclarationA from 'decorator';",
"import decoratorParameter from 'decorator';",
"class TextareaAutosize {",
"@classMethodDeclarationA(decoratorParameter)",
"@classMethodDeclarationA",
"get bar() { }",
"@classMethodDeclarationA(decoratorParameter)",
"@classMethodDeclarationA",
"set bar() { }",
"}"
].join("\n"),
{ "no-unused-vars": 1 },
[]
);
});

it("object property", function () {
verifyAndAssertMessages(
[
"import classMethodDeclarationA from 'decorator';",
"import decoratorParameter from 'decorator';",
"var obj = {",
"@classMethodDeclarationA(decoratorParameter)",
"@classMethodDeclarationA",
"methodDeclaration(e) {",
"e();",
"}",
"};",
"obj;"
].join("\n"),
{ "no-unused-vars": 1 },
[]
);
});

it("object property get/set", function () {
verifyAndAssertMessages(
[
"import classMethodDeclarationA from 'decorator';",
"import decoratorParameter from 'decorator';",
"var obj = {",
"@classMethodDeclarationA(decoratorParameter)",
"@classMethodDeclarationA",
"get bar() { },",
"@classMethodDeclarationA(decoratorParameter)",
"@classMethodDeclarationA",
"set bar() { }",
"};",
"obj;"
].join("\n"),
{ "no-unused-vars": 1 },
[]
);
});
});
});

0 comments on commit 79b5b4a

Please sign in to comment.