Fix: improve handling of class properties (fixes babel/babel-eslint#337) (babel/babel-eslint#338)

I added ClassProperty method into Referencer of escope. This method will
address class properties and those type anotations.
This commit is contained in:
Toru Nagashima 2016-07-10 00:40:08 +09:00
parent 52d9c34465
commit 1766a21145
2 changed files with 56 additions and 7 deletions

View File

@ -229,13 +229,6 @@ function monkeypatch() {
// visit decorators that are in: ClassDeclaration / ClassExpression // visit decorators that are in: ClassDeclaration / ClassExpression
var visitClass = referencer.prototype.visitClass; var visitClass = referencer.prototype.visitClass;
referencer.prototype.visitClass = function(node) { referencer.prototype.visitClass = function(node) {
var classBody = node.body.body;
for (var a = 0; a < classBody.length; a++) {
if (classBody[a].type === "ClassProperty") {
createScopeVariable.call(this, classBody[a], classBody[a].key);
}
}
visitDecorators.call(this, node); visitDecorators.call(this, node);
var typeParamScope; var typeParamScope;
if (node.typeParameters) { if (node.typeParameters) {
@ -268,6 +261,14 @@ function monkeypatch() {
visitProperty.call(this, node); visitProperty.call(this, node);
}; };
// visit ClassProperty as a Property.
referencer.prototype.ClassProperty = function(node) {
if (node.typeAnnotation) {
visitTypeAnnotation.call(this, node.typeAnnotation);
}
this.visitProperty(node);
};
// visit flow type in FunctionDeclaration, FunctionExpression, ArrowFunctionExpression // visit flow type in FunctionDeclaration, FunctionExpression, ArrowFunctionExpression
var visitFunction = referencer.prototype.visitFunction; var visitFunction = referencer.prototype.visitFunction;
referencer.prototype.visitFunction = function(node) { referencer.prototype.visitFunction = function(node) {

View File

@ -1525,6 +1525,54 @@ describe("verify", function () {
); );
}); });
describe("Class Property Declarations", function() {
it("no-redeclare false positive 1", function() {
verifyAndAssertMessages(
[
"class Group {",
" static propTypes = {};",
"}",
"class TypicalForm {",
" static propTypes = {};",
"}"
].join("\n"),
{ "no-redeclare": 1 },
[]
);
});
it("no-redeclare false positive 2", function() {
verifyAndAssertMessages(
[
"function validate() {}",
"class MyComponent {",
" static validate = validate;",
"}"
].join("\n"),
{ "no-redeclare": 1 },
[]
);
});
it("check references", function() {
verifyAndAssertMessages(
[
"var a;",
"class A {",
" prop1;",
" prop2 = a;",
" prop3 = b;",
"}",
"new A"
].join("\n"),
{ "no-undef": 1, "no-unused-vars": 1, "no-redeclare": 1 },
[
"5:11 'b' is not defined. no-undef"
]
);
});
});
// it("regex with es6 unicodeCodePointEscapes", function () { // it("regex with es6 unicodeCodePointEscapes", function () {
// verifyAndAssertMessages( // verifyAndAssertMessages(
// "string.replace(/[\u{0000A0}-\u{10FFFF}<>\&]/gmiu, (char) => `&#x${char.codePointAt(0).toString(16)};`);", // "string.replace(/[\u{0000A0}-\u{10FFFF}<>\&]/gmiu, (char) => `&#x${char.codePointAt(0).toString(16)};`);",