Skip to content
This repository was archived by the owner on Nov 27, 2023. It is now read-only.

Commit 0ed6ffc

Browse files
committed
feat: support output for react-like libraries
Closes #351
1 parent 532373a commit 0ed6ffc

7 files changed

+47
-14
lines changed

Diff for: cli.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,14 @@ var meow = require('meow');
44

55
const cli = meow(`
66
Usage
7-
$ react2dts [--module-name <name> | --top-level-module]
7+
$ react2dts [--module-name <name> | --top-level-module] [--react-import <name>]
88
99
react2dts reads from stdin to process a file.
1010
1111
Options
1212
--module-name, --name name of the module to create
1313
--top-level-module if the created module should live in top-level
14+
--react-import name of the react-like library to import (default to react)
1415
1516
Examples
1617
$ cat <some/react/component.jsx> |react2dts --module-name module-name

Diff for: src/index.ts

+11-8
Original file line numberDiff line numberDiff line change
@@ -52,21 +52,23 @@ export interface Options {
5252
export function cli(options: any): void {
5353
getStdin().then(stdinCode => {
5454
if (options.topLevelModule) {
55-
process.stdout.write(generateFromSource(null, stdinCode));
55+
process.stdout.write(generateFromSource(null, stdinCode, {}, options.reactImport || 'react'));
5656
} else if (options.moduleName) {
57-
process.stdout.write(generateFromSource(options.moduleName, stdinCode));
57+
process.stdout.write(generateFromSource(options.moduleName, stdinCode, {}, options.reactImport || 'react'));
5858
}
5959
});
6060
}
6161

62-
export function generateFromFile(moduleName: string|null, path: string, options: IOptions = {}): string {
62+
export function generateFromFile(moduleName: string|null, path: string, options: IOptions = {},
63+
reactImport = 'react'): string {
6364
if (!options.filename) {
6465
options.filename = path;
6566
}
66-
return generateFromSource(moduleName, fs.readFileSync(path).toString(), options);
67+
return generateFromSource(moduleName, fs.readFileSync(path).toString(), options, reactImport);
6768
}
6869

69-
export function generateFromSource(moduleName: string|null, code: string, options: IOptions = {}): string {
70+
export function generateFromSource(moduleName: string|null, code: string, options: IOptions = {},
71+
reactImport = 'react'): string {
7072
const ast = babylon.parse(code, {
7173
sourceType: 'module',
7274
allowReturnOutsideFunction: true,
@@ -92,12 +94,13 @@ export function generateFromSource(moduleName: string|null, code: string, option
9294
if (!options.source) {
9395
options.source = code;
9496
}
95-
return generateFromAst(moduleName, ast, options);
97+
return generateFromAst(moduleName, ast, options, reactImport);
9698
}
9799

98-
export function generateFromAst(moduleName: string|null, ast: any, options: IOptions = {}): string {
100+
export function generateFromAst(moduleName: string|null, ast: any, options: IOptions = {},
101+
reactImport = 'react'): string {
99102
if (options.generator) {
100103
return generateTypings(moduleName, ast, options);
101104
}
102-
return createTypings(moduleName, ast, options);
105+
return createTypings(moduleName, ast, options, reactImport);
103106
}

Diff for: src/typings.ts

+3-2
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@ export interface AstQuery {
1010
querySubtree(subtree: any, query: string): any[];
1111
}
1212

13-
export function createTypings(moduleName: string|null, programAst: any, options: IOptions): string {
13+
export function createTypings(moduleName: string|null, programAst: any, options: IOptions,
14+
reactImport: string): string {
1415
const astq = new ASTQ();
1516
const ast = {
1617
ast: programAst,
@@ -33,7 +34,7 @@ export function createTypings(moduleName: string|null, programAst: any, options:
3334

3435
const m = dom.create.module(moduleName || 'moduleName');
3536
if (hasReactClass(ast, reactComponentName)) {
36-
m.members.push(dom.create.importNamed('Component', 'react'));
37+
m.members.push(dom.create.importNamed('Component', reactImport));
3738
}
3839
if (importStatements.length > 0) {
3940
importStatements.forEach(importStatement => {

Diff for: tests/parsing-test.ts

+5-2
Original file line numberDiff line numberDiff line change
@@ -32,10 +32,10 @@ function textDiff(t: ContextualTestContext, actual: string, expected: string): v
3232
}
3333

3434
function compare(t: ContextualTestContext, moduleName: string|null, file1: string, file2: string,
35-
opts: react2dts.IOptions = {}): void {
35+
opts: react2dts.IOptions = {}, reactImport = 'react'): void {
3636
textDiff(
3737
t,
38-
react2dts.generateFromFile(moduleName, path.join(basedir, file1), opts),
38+
react2dts.generateFromFile(moduleName, path.join(basedir, file1), opts, reactImport),
3939
fs.readFileSync(path.join(basedir, file2)).toString()
4040
);
4141
}
@@ -85,3 +85,6 @@ test('Parsing should create definition from file with references in propTypes',
8585
test('Parsing should create definition from file with unnamed default export', t => {
8686
compare(t, 'path', 'unnamed-default-export.jsx', 'unnamed-default-export.d.ts');
8787
});
88+
test('Parsing should create preact definition', t => {
89+
compare(t, 'path', 'preact-definition.jsx', 'preact-definition.d.ts', {}, 'preact');
90+
});

Diff for: tests/preact-definition.d.ts

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
declare module 'path' {
2+
import {Component} from 'preact';
3+
4+
export interface SomeComponentProps {
5+
onClick?: (...args: any[]) => any;
6+
}
7+
8+
export class SomeComponent extends Component<SomeComponentProps, any> {
9+
render(): JSX.Element;
10+
}
11+
}

Diff for: tests/preact-definition.jsx

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import {Component, PropTypes} from 'react';
2+
3+
export class SomeComponent extends Component {
4+
5+
static propTypes = {
6+
onClick: PropTypes.func
7+
};
8+
9+
render() {
10+
return (
11+
<div></div>
12+
);
13+
}
14+
}

Diff for: tests/unnamed-default-export.d.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ declare module 'path' {
22
import {Component} from 'react';
33

44
export interface Props {
5-
onClick?: (...args: any[]) => any;
5+
onClick?: (...args: any[]) => any;
66
}
77

88
export default class extends Component<Props, any> {

0 commit comments

Comments
 (0)