Skip to content
This repository has been archived by the owner on Aug 18, 2021. It is now read-only.

Commit

Permalink
Fix: improve handling of class properties (fixes #337) (#338)
Browse files Browse the repository at this point in the history
I added ClassProperty method into Referencer of escope. This method will
address class properties and those type anotations.
  • Loading branch information
mysticatea authored and hzoo committed Jul 9, 2016
1 parent 36a6a6d commit e535833
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 7 deletions.
15 changes: 8 additions & 7 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -229,13 +229,6 @@ function monkeypatch() {
// visit decorators that are in: ClassDeclaration / ClassExpression
var visitClass = referencer.prototype.visitClass;
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);
var typeParamScope;
if (node.typeParameters) {
Expand Down Expand Up @@ -268,6 +261,14 @@ function monkeypatch() {
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
var visitFunction = referencer.prototype.visitFunction;
referencer.prototype.visitFunction = function(node) {
Expand Down
48 changes: 48 additions & 0 deletions test/non-regression.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 () {
// verifyAndAssertMessages(
// "string.replace(/[\u{0000A0}-\u{10FFFF}<>\&]/gmiu, (char) => `&#x${char.codePointAt(0).toString(16)};`);",
Expand Down

0 comments on commit e535833

Please sign in to comment.