File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -26,6 +26,8 @@ export class Config implements IConfig {
2626
2727 path : string ;
2828
29+ newJsx : boolean ;
30+
2931 jsxExt : boolean ;
3032
3133 typescript : boolean ;
@@ -57,6 +59,7 @@ export class Config implements IConfig {
5759 // Boolean
5860
5961 this . jsxExt = config . jsxExt ?? this . jsxExt ;
62+ this . newJsx = config . newJsx ?? this . newJsx ;
6063 this . typescript = config . typescript ?? this . typescript ;
6164 this . wrapFolder = config . wrapFolder ?? this . wrapFolder ;
6265 this . cssModules = config . cssModules ?? this . cssModules ;
Original file line number Diff line number Diff line change @@ -8,6 +8,7 @@ export const DEFAULT_CONFIG: IConfig = {
88 styles : 'scss' ,
99 typescript : false ,
1010 jsxExt : true ,
11+ newJsx : false ,
1112 wrapFolder : true ,
1213 fileNameCase : 'pascal' ,
1314 path : 'src/components' ,
Original file line number Diff line number Diff line change 11import * as t from '@babel/types' ;
22import { ReactHook , Variables } from '@/types' ;
3+ import { config } from '@/config' ;
34import * as c from './temlpates/shared' ;
45
56/* istanbul ignore next */
@@ -10,7 +11,19 @@ export class TemplateBase {
1011 this . vars = vars ;
1112 }
1213
13- protected getReactImportSpecifier ( ) : t . ImportSpecifier [ ] {
14+ protected getReactImport ( ) : t . ImportDeclaration | null {
15+ const reactImportSpecifiers = this . getReactImportSpecifiers ( ) ;
16+
17+ if ( config . newJsx ) {
18+ if ( reactImportSpecifiers . length ) {
19+ return c . importNamed ( reactImportSpecifiers , 'react' ) ;
20+ }
21+ return null ;
22+ }
23+ return c . importDefault ( 'React' , 'react' , reactImportSpecifiers ) ;
24+ }
25+
26+ protected getReactImportSpecifiers ( ) : t . ImportSpecifier [ ] {
1427 return this . vars . hooks . map ( hook => c . importSpec ( hook ) ) ;
1528 }
1629
Original file line number Diff line number Diff line change @@ -48,9 +48,10 @@ export class ComponentTemplate
4848 generateAST ( ) {
4949 const body : t . Statement [ ] = [ ] ;
5050
51- body . push (
52- c . importDefault ( 'React' , 'react' , this . getReactImportSpecifier ( ) )
53- ) ;
51+ const reactImport = this . getReactImport ( ) ;
52+ if ( reactImport ) {
53+ body . push ( reactImport ) ;
54+ }
5455
5556 if ( this . hasMod ( 'propTypes' ) ) {
5657 body . push ( c . importDefault ( 'PropTypes' , 'prop-types' ) ) ;
Original file line number Diff line number Diff line change @@ -16,7 +16,10 @@ export class ComponentTestTemplate
1616 const componentPath = `./${ this . vars . componentName } ` ;
1717 const body : t . Statement [ ] = [ ] ;
1818
19- body . push ( c . importDefault ( 'React' , 'react' ) ) ;
19+ const reactImport = this . getReactImport ( ) ;
20+ if ( reactImport ) {
21+ body . push ( reactImport ) ;
22+ }
2023 body . push (
2124 c . importNamed ( [ c . importSpec ( 'render' ) ] , '@testing-library/react' )
2225 ) ;
Original file line number Diff line number Diff line change @@ -13,9 +13,10 @@ export class HOCTemplate extends TemplateBase implements Template {
1313 generateAST ( ) : t . File {
1414 const body : t . Statement [ ] = [ ] ;
1515
16- body . push (
17- c . importDefault ( 'React' , 'react' , this . getReactImportSpecifier ( ) )
18- ) ;
16+ const reactImport = this . getReactImport ( ) ;
17+ if ( reactImport ) {
18+ body . push ( reactImport ) ;
19+ }
1920 body . push ( t . emptyStatement ( ) ) ;
2021
2122 if ( this . hasHook ( 'useReducer' ) ) {
Original file line number Diff line number Diff line change @@ -13,12 +13,11 @@ export class HookTemplate extends TemplateBase implements Template {
1313 generateAST ( ) : t . File {
1414 const body : t . Statement [ ] = [ ] ;
1515
16- const reactImportSpecifiers = this . getReactImportSpecifier ( ) ;
17-
18- if ( reactImportSpecifiers . length ) {
19- body . push ( c . importNamed ( reactImportSpecifiers , 'react' ) ) ;
20- body . push ( t . emptyStatement ( ) ) ;
16+ const reactImport = this . getReactImport ( ) ;
17+ if ( reactImport ) {
18+ body . push ( reactImport ) ;
2119 }
20+ body . push ( t . emptyStatement ( ) ) ;
2221
2322 if ( this . hasHook ( 'useReducer' ) ) {
2423 body . push ( c . useReducerInit ( ) ) ;
Original file line number Diff line number Diff line change @@ -14,6 +14,7 @@ export type ReactHooks = ReactHook[];
1414export interface IConfig {
1515 styles ?: StyleFormats ;
1616 typescript ?: boolean ;
17+ newJsx ?: boolean ;
1718 jsxExt ?: boolean ;
1819 fileNameCase ?: FileNameCase ;
1920 path : string ;
You can’t perform that action at this time.
0 commit comments