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

Commit

Permalink
visit most flow types: fixes #108
Browse files Browse the repository at this point in the history
  • Loading branch information
hzoo committed May 21, 2015
1 parent fb5c8b9 commit 650c2ea
Show file tree
Hide file tree
Showing 3 changed files with 150 additions and 47 deletions.
12 changes: 0 additions & 12 deletions acorn-to-esprima.js
Original file line number Diff line number Diff line change
Expand Up @@ -183,20 +183,8 @@ var astTransformVisitor = {
delete node.argument;
}

if (t.isTypeCastExpression(node)) {
return node.expression;
}

if (t.isRestElement(node)) {
return node.argument;
}

// modules

if (t.isImportDeclaration(node)) {
delete node.isType;
}

if (t.isExportDeclaration(node)) {
if (t.isClassExpression(node.declaration)) {
node.declaration.type = "ClassDeclaration";
Expand Down
75 changes: 65 additions & 10 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,31 +76,86 @@ function monkeypatch() {
}
}

// monkeypatch referencer methods to visit decorators
// visit decorators that are in: ClassDeclaration / ClassExpression
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);
}
// visit decorators that are in: Property / MethodDefinition
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);
}

// monkeypatch referencer methods to visit flow type annotations
visitFunction = referencer.prototype.visitFunction;
referencer.prototype.visitFunction = function (node) {
for (var i = 0; i < node.params.length; i++) {
var p = node.params[i];
if (p.typeAnnotation && p.typeAnnotation.typeAnnotation) {
this.visit(p.typeAnnotation.typeAnnotation.id);
function visitTypeAnnotation(node) {
// visit function type parameters
if (node.typeParameters) {
visitTypeAnnotation.call(this, node.typeParameters);
}

// visit function parameters
if (node.params) {
for (var i = 0; i < node.params.length; i++) {
visitTypeAnnotation.call(this, node.params[i]);
}
}

// visit return type
if (node.returnType) {
visitTypeAnnotation.call(this, node.returnType);
}

// visit type
if (node.typeAnnotation) {
visitTypeAnnotation.call(this, node.typeAnnotation);
}

// visit rest
if (t.isFunctionTypeAnnotation(node) && node.rest) {
visitTypeAnnotation.call(this, node.rest);
}

// object properties - properties
if (t.isObjectTypeAnnotation(node) && node.properties) {
for (var i = 0; i < node.properties.length; i++) {
visitTypeAnnotation.call(this, node.properties[i].value);
}
}

// object properties - indexers
if (t.isObjectTypeAnnotation(node) && node.indexers) {
for (var i = 0; i < node.indexers.length; i++) {
visitTypeAnnotation.call(this, node.indexers[i].key);
visitTypeAnnotation.call(this, node.indexers[i].value);
}
}

if (node.id) {
this.visit(node.id);
} else if (node.type === 'Identifier') {
this.visit(node);
}
}

// visit flow type in FunctionDeclaration, FunctionExpression, ArrowFunctionExpression
var visitFunction = referencer.prototype.visitFunction;
referencer.prototype.visitFunction = function (node) {
visitTypeAnnotation.call(this, node);
visitFunction.call(this, node);
}

// visit flow type in VariableDeclaration
var variableDeclaration = referencer.prototype.VariableDeclaration;
referencer.prototype.VariableDeclaration = function (node) {
if (node.declarations) {
node.declarations.forEach(function (d) {
visitTypeAnnotation.call(this, d.id);
}.bind(this));
}
variableDeclaration.call(this, node);
}
}

exports.attachComments = function (ast, comments, tokens) {
Expand Down
110 changes: 85 additions & 25 deletions test/non-regression.js
Original file line number Diff line number Diff line change
Expand Up @@ -124,33 +124,93 @@ describe("verify", function () {
);
});

it("flow type", function () {
verifyAndAssertMessages(
"type SomeNewType = any;",
{},
[]
);
});
describe("flow", function () {
it("type alias", function () {
verifyAndAssertMessages(
"type SomeNewType = any;",
{},
[]
);
});

it("flow import type #108", function () {
verifyAndAssertMessages([
"import type Foo from 'foo';",
"function log(foo: Foo) {",
"console.log(foo);",
"}",
"log(a);"
].join("\n"),
{ "no-unused-vars": 1 },
[]
);
});
it("type cast expression #102", function () {
verifyAndAssertMessages(
"for (let a of (a: Array)) {}",
{},
[]
);
});

it("type cast expression #102", function () {
verifyAndAssertMessages(
"for (let a of (a: Array)) {}",
{},
[]
);
it("multiple nullable type annotations and return #108", function () {
verifyAndAssertMessages([
"import type Foo from 'foo';",
"import type Foo2 from 'foo';",
"import type Foo3 from 'foo';",
"function log(foo: ?Foo, foo2: ?Foo2): ?Foo3 {",
"console.log(foo, foo2);",
"}",
"log(1, 2);"
].join("\n"),
{ "no-unused-vars": 1 },
[]
);
});

it("type parameters #108", function () {
verifyAndAssertMessages([
"import type Foo from 'foo';",
"import type Foo2 from 'foo';",
"function log<Foo, Foo2>() {",
"foo();",
"}",
].join("\n"),
{ "no-unused-vars": 1 },
[]
);
});

it("nested type annotations #108", function () {
verifyAndAssertMessages([
"import type Foo from 'foo';",
"function foo(callback: () => Foo){",
"return callback();",
"}"
].join("\n"),
{ "no-unused-vars": 1 },
[]
);
});

it("type in var declaration #108", function () {
verifyAndAssertMessages([
"import type Foo from 'foo';",
"var x: Foo = 1;",
"x;"
].join("\n"),
{ "no-unused-vars": 1 },
[]
);
});

it("type properties #108", function () {
verifyAndAssertMessages([
"import type Foo from 'foo';",
"var a: {numVal: Foo};"
].join("\n"),
{ "no-unused-vars": 1 },
[]
);
});

it("object type annotation #108", function () {
verifyAndAssertMessages([
"import type Foo from 'foo';",
"var a: {numVal: Foo};"
].join("\n"),
{ "no-unused-vars": 1 },
[]
);
});
});

it("class usage", function () {
Expand Down

0 comments on commit 650c2ea

Please sign in to comment.