Merge pull request babel/babel-eslint#105 from hzoo/visit-decorators
patch escope to visit decorators - fixes babel/babel-eslint#72
This commit is contained in:
parent
80114dda35
commit
0e50a0b6e0
@ -54,6 +54,41 @@ function monkeypatch() {
|
||||
estraverse.VisitorKeys.TypeAlias = TypeAliasKeys;
|
||||
return results;
|
||||
};
|
||||
|
||||
// monkeypatch escope/referencer
|
||||
var referencerLoc;
|
||||
try {
|
||||
var referencerLoc = Module._resolveFilename("./referencer", escopeMod);
|
||||
} catch (err) {
|
||||
throw new ReferenceError("couldn't resolve escope/referencer");
|
||||
}
|
||||
var referencer = require(referencerLoc);
|
||||
|
||||
// if there are decotators, then visit each
|
||||
function visitDecorators(node) {
|
||||
if (!node.decorators) {
|
||||
return;
|
||||
}
|
||||
for (var i = 0; i < node.decorators.length; i++) {
|
||||
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) {
|
||||
|
||||
@ -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 },
|
||||
[]
|
||||
);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user