Skip to content
This repository has been archived by the owner on Apr 26, 2024. It is now read-only.

Commit

Permalink
Fixes #41, handle declare keyword (#92)
Browse files Browse the repository at this point in the history
* Fixes #41, handle declare keyword

* add comments to 'declare' tests
  • Loading branch information
kevinbarabash authored May 18, 2019
1 parent 987f873 commit 66a5ae9
Show file tree
Hide file tree
Showing 9 changed files with 123 additions and 0 deletions.
80 changes: 80 additions & 0 deletions src/transform.js
Original file line number Diff line number Diff line change
Expand Up @@ -640,6 +640,86 @@ const transform = {
exit(path) {
path.node.importKind = "value";
}
},
DeclareVariable: {
exit(path) {
const { id } = path.node;

// TODO: patch @babel/types - t.variableDeclaration omits declare param
// const declaration = t.variableDeclaration("var", [
// t.variableDeclarator(id),
// ], true),

path.replaceWith({
type: "VariableDeclaration",
kind: "var",
declarations: [t.variableDeclarator(id)],
declare: true
});
}
},
DeclareClass: {
exit(path) {
const { id, body, typeParameters } = path.node;
const superClass =
path.node.extends.length > 0 ? path.node.extends[0] : undefined;

// TODO: patch @babel/types - t.classDeclaration omits typescript params
// t.classDeclaration(id, superClass, body, [], false, true, [], undefined)

path.replaceWith({
type: "ClassDeclaration",
id,
typeParameters,
superClass,
superClassTypeParameters: superClass
? superClass.typeParameters
: undefined,
body,
declare: true
});
}
},
DeclareFunction: {
exit(path) {
const { id } = path.node;
const { name, typeAnnotation } = id;

// TSFunctionType
const functionType = typeAnnotation.typeAnnotation;

// TODO: patch @babel/types - t.tsDeclaration only accepts 4 params but should accept 7
// t.tsDeclareFunction(
// t.identifier(name),
// t.noop(),
// functionType.parameters,
// functionType.typeAnnotation,
// false, // async
// true,
// false, // generator
// ),

path.replaceWith({
type: "TSDeclareFunction",
id: t.identifier(name),
typeParameters: functionType.typeParameters,
params: functionType.parameters,
returnType: functionType.typeAnnotation,
declare: !t.isDeclareExportDeclaration(path.parent),
async: false, // TODO
generator: false // TODO
});
}
},
DeclareExportDeclaration: {
exit(path) {
const { declaration, default: _default } = path.node;

path.replaceWith({
type: _default ? "ExportDefaultDeclaration" : "ExportNamedDeclaration",
declaration
});
}
}
};

Expand Down
5 changes: 5 additions & 0 deletions test/fixtures/convert/declare/declare-class/flow.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
// @flow
declare class Path<T> extends Node<U> {
// converts Path to a string
toString(): string;
}
5 changes: 5 additions & 0 deletions test/fixtures/convert/declare/declare-class/ts.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@

declare class Path<T> extends Node<U> {
// converts Path to a string
toString(): string;
}
3 changes: 3 additions & 0 deletions test/fixtures/convert/declare/declare-function/flow.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
// @flow
// Adds two numbers
declare function foo(a: number, b: number): number;
3 changes: 3 additions & 0 deletions test/fixtures/convert/declare/declare-function/ts.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@

// Adds two numbers
declare function foo(a: number, b: number): number;
8 changes: 8 additions & 0 deletions test/fixtures/convert/declare/declare-module/flow.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
// @flow
declare module "some-commonjs-module" {
// foo
declare export default function foo(): string;

// bar
declare export function bar(): boolean;
}
8 changes: 8 additions & 0 deletions test/fixtures/convert/declare/declare-module/ts.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@

declare module "some-commonjs-module" {
// foo
export default function foo(): string;

// bar
export function bar(): boolean;
}
5 changes: 5 additions & 0 deletions test/fixtures/convert/declare/declare-var/flow.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
// @flow
/**
* The constant PI ~ 3.141592
*/
declare var PI: number;
6 changes: 6 additions & 0 deletions test/fixtures/convert/declare/declare-var/ts.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@


/**
* The constant PI ~ 3.141592
*/
declare var PI: number;

0 comments on commit 66a5ae9

Please sign in to comment.