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

Commit 9b1096d

Browse files
committed
fix: for SFCs use triple slash directive instead of react import
fixes #469 closes #471
1 parent 9e15917 commit 9b1096d

6 files changed

+15
-24
lines changed

Diff for: package-lock.json

+3-3
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Diff for: package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@
6969
"babel-generator": "^6.26.0",
7070
"babylon": "7.0.0-beta.34",
7171
"chalk": "^2.3.0",
72-
"dts-dom": "^1.0.0",
72+
"dts-dom": "^2.0.1",
7373
"get-stdin": "^5.0.1",
7474
"meow": "^4.0.0",
7575
"pascal-case": "2.0.1",

Diff for: src/typings.ts

+8-14
Original file line numberDiff line numberDiff line change
@@ -44,9 +44,14 @@ export function createTypings(moduleName: string|null, programAst: any, options:
4444
...getComponentNamesByStaticPropTypeAttribute(ast),
4545
...getComponentNamesByJsxInBody(ast)
4646
]);
47+
const tripleSlashDirectives: dom.TripleSlashDirective[] = [];
4748
const m = dom.create.module(moduleName || 'moduleName');
4849

49-
createReactImport(m, ast, reactImport, reactComponentName);
50+
if (hasReactClass(ast, reactComponentName)) {
51+
m.members.push(dom.create.importNamed('Component', reactImport));
52+
} else {
53+
tripleSlashDirectives.push(dom.create.tripleSlashReferenceTypesDirective('react'));
54+
}
5055

5156
if (importStatements.length > 0) {
5257
importStatements.forEach(importStatement => {
@@ -67,20 +72,9 @@ export function createTypings(moduleName: string|null, programAst: any, options:
6772
});
6873

6974
if (moduleName === null) {
70-
return m.members
71-
.map(member => dom.emit(member, dom.ContextFlags.None))
72-
.join('');
73-
} else {
74-
return dom.emit(m, dom.ContextFlags.Module);
75-
}
76-
}
77-
78-
function createReactImport(m: dom.ModuleDeclaration, ast: AstQuery, reactImport: string,
79-
reactComponentName: string|undefined): void {
80-
if (hasReactClass(ast, reactComponentName)) {
81-
m.members.push(dom.create.importNamed('Component', reactImport));
75+
return m.members.map(member => dom.emit(member)).join('');
8276
} else {
83-
m.members.push(dom.create.import(reactImport));
77+
return dom.emit(m, { rootFlags: dom.ContextFlags.Module, tripleSlashDirectives });
8478
}
8579
}
8680

Diff for: tests/named-export-specifiers.d.ts

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
1+
/// <reference types="react" />
12
declare module 'component' {
2-
import 'react';
3-
43
export interface ComponentProps {
54
optionalAny?: any;
65
}

Diff for: tests/stateless-export-as-default.d.ts

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
1+
/// <reference types="react" />
12
declare module 'component' {
2-
import 'react';
3-
43
export interface ComponentProps {
54
text: string;
65
className?: string;

Diff for: tests/stateless.d.ts

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
1+
/// <reference types="react" />
12
declare module 'component' {
2-
import 'react';
3-
43
export interface ComponentProps {
54
optionalAny?: any;
65
}

0 commit comments

Comments
 (0)