@@ -10,137 +10,133 @@ import {
10
10
isPrivateMember ,
11
11
stripQuotes ,
12
12
} from '../../ast-helpers.js' ;
13
+ import { isModule } from '../helpers.js' ;
13
14
import { exportVisitor as visit } from '../index.js' ;
14
15
15
- export default visit (
16
- ( ) => true ,
17
- ( node , { isFixExports, isFixTypes, isReportClassMembers } ) => {
18
- const exportKeyword = getExportKeywordNode ( node ) ;
16
+ export default visit ( isModule , ( node , { isFixExports, isFixTypes, isReportClassMembers } ) => {
17
+ const exportKeyword = getExportKeywordNode ( node ) ;
19
18
20
- if ( exportKeyword ) {
21
- const getFix = ( node : ts . Node , defaultKeyword ?: ts . Node ) : Fix =>
22
- isFixExports ? [ node . getStart ( ) , ( defaultKeyword ?? node ) . getEnd ( ) + 1 , FIX_FLAGS . NONE ] : undefined ;
19
+ if ( exportKeyword ) {
20
+ const getFix = ( node : ts . Node , defaultKeyword ?: ts . Node ) : Fix =>
21
+ isFixExports ? [ node . getStart ( ) , ( defaultKeyword ?? node ) . getEnd ( ) + 1 , FIX_FLAGS . NONE ] : undefined ;
23
22
24
- const getTypeFix = ( node : ts . Node ) : Fix =>
25
- isFixTypes ? [ node . getStart ( ) , node . getEnd ( ) + 1 , FIX_FLAGS . NONE ] : undefined ;
23
+ const getTypeFix = ( node : ts . Node ) : Fix =>
24
+ isFixTypes ? [ node . getStart ( ) , node . getEnd ( ) + 1 , FIX_FLAGS . NONE ] : undefined ;
26
25
27
- if ( ts . isVariableStatement ( node ) ) {
28
- // @ts -expect-error TODO Issue seems caused by mismatch between returned `node` types (but all ts.Node)
29
- return node . declarationList . declarations . flatMap ( declaration => {
30
- if ( ts . isObjectBindingPattern ( declaration . name ) ) {
31
- // Pattern: export const { name1, name2 } = {};
32
- return compact (
33
- declaration . name . elements . map ( element => {
34
- if ( ts . isIdentifier ( element . name ) ) {
35
- const fix = isFixExports
36
- ? [ element . getStart ( ) , element . getEnd ( ) , FIX_FLAGS . OBJECT_BINDING ]
37
- : undefined ;
38
- return {
39
- node : element ,
40
- // @ts -expect-error We'll use the symbol in `findInternalReferences`
41
- symbol : element . symbol ,
42
- identifier : element . name . escapedText . toString ( ) ,
43
- type : SymbolType . UNKNOWN ,
44
- pos : element . name . getStart ( ) ,
45
- fix,
46
- } ;
47
- }
48
- } )
49
- ) ;
50
- }
51
- if ( ts . isArrayBindingPattern ( declaration . name ) ) {
52
- // Pattern: export const [name1, name2] = [];
53
- return compact (
54
- declaration . name . elements . map ( element => {
55
- if ( ts . isBindingElement ( element ) ) {
56
- const fix = isFixExports ? [ element . getStart ( ) , element . getEnd ( ) , FIX_FLAGS . NONE ] : undefined ;
57
- return {
58
- node : element ,
59
- // @ts -expect-error We'll use the symbol in `findInternalReferences`
60
- symbol : element . symbol ,
61
- identifier : element . getText ( ) ,
62
- type : SymbolType . UNKNOWN ,
63
- pos : element . getStart ( ) ,
64
- fix,
65
- } ;
66
- }
67
- } )
68
- ) ;
69
- }
26
+ if ( ts . isVariableStatement ( node ) ) {
27
+ // @ts -expect-error TODO Issue seems caused by mismatch between returned `node` types (but all ts.Node)
28
+ return node . declarationList . declarations . flatMap ( declaration => {
29
+ if ( ts . isObjectBindingPattern ( declaration . name ) ) {
30
+ // Pattern: export const { name1, name2 } = {};
31
+ return compact (
32
+ declaration . name . elements . map ( element => {
33
+ if ( ts . isIdentifier ( element . name ) ) {
34
+ const fix = isFixExports ? [ element . getStart ( ) , element . getEnd ( ) , FIX_FLAGS . OBJECT_BINDING ] : undefined ;
35
+ return {
36
+ node : element ,
37
+ // @ts -expect-error We'll use the symbol in `findInternalReferences`
38
+ symbol : element . symbol ,
39
+ identifier : element . name . escapedText . toString ( ) ,
40
+ type : SymbolType . UNKNOWN ,
41
+ pos : element . name . getStart ( ) ,
42
+ fix,
43
+ } ;
44
+ }
45
+ } )
46
+ ) ;
47
+ }
48
+ if ( ts . isArrayBindingPattern ( declaration . name ) ) {
49
+ // Pattern: export const [name1, name2] = [];
50
+ return compact (
51
+ declaration . name . elements . map ( element => {
52
+ if ( ts . isBindingElement ( element ) ) {
53
+ const fix = isFixExports ? [ element . getStart ( ) , element . getEnd ( ) , FIX_FLAGS . NONE ] : undefined ;
54
+ return {
55
+ node : element ,
56
+ // @ts -expect-error We'll use the symbol in `findInternalReferences`
57
+ symbol : element . symbol ,
58
+ identifier : element . getText ( ) ,
59
+ type : SymbolType . UNKNOWN ,
60
+ pos : element . getStart ( ) ,
61
+ fix,
62
+ } ;
63
+ }
64
+ } )
65
+ ) ;
66
+ }
70
67
71
- // Pattern: export const MyVar = 1;
72
- const identifier = declaration . name . getText ( ) ;
73
- const pos = declaration . name . getStart ( ) ;
74
- const fix = getFix ( exportKeyword ) ;
75
- return { node : declaration , identifier, type : SymbolType . UNKNOWN , pos, fix } ;
76
- } ) ;
77
- }
68
+ // Pattern: export const MyVar = 1;
69
+ const identifier = declaration . name . getText ( ) ;
70
+ const pos = declaration . name . getStart ( ) ;
71
+ const fix = getFix ( exportKeyword ) ;
72
+ return { node : declaration , identifier, type : SymbolType . UNKNOWN , pos, fix } ;
73
+ } ) ;
74
+ }
78
75
79
- const defaultKeyword = getDefaultKeywordNode ( node ) ;
76
+ const defaultKeyword = getDefaultKeywordNode ( node ) ;
80
77
81
- if ( ts . isFunctionDeclaration ( node ) && node . name ) {
82
- const identifier = defaultKeyword ? 'default' : node . name . getText ( ) ;
83
- const pos = ( node . name ?? node . body ?? node ) . getStart ( ) ;
84
- const fix = getFix ( exportKeyword , defaultKeyword ) ;
85
- return { node, identifier, pos, type : SymbolType . FUNCTION , fix } ;
86
- }
78
+ if ( ts . isFunctionDeclaration ( node ) && node . name ) {
79
+ const identifier = defaultKeyword ? 'default' : node . name . getText ( ) ;
80
+ const pos = ( node . name ?? node . body ?? node ) . getStart ( ) ;
81
+ const fix = getFix ( exportKeyword , defaultKeyword ) ;
82
+ return { node, identifier, pos, type : SymbolType . FUNCTION , fix } ;
83
+ }
87
84
88
- if ( ts . isClassDeclaration ( node ) && node . name ) {
89
- const identifier = defaultKeyword ? 'default' : node . name . getText ( ) ;
90
- const pos = ( node . name ?? node ) . getStart ( ) ;
91
- const fix = getFix ( exportKeyword , defaultKeyword ) ;
92
- const members = isReportClassMembers
93
- ? node . members
94
- . filter (
95
- ( member ) : member is ts . MethodDeclaration | ts . PropertyDeclaration =>
96
- ( ts . isPropertyDeclaration ( member ) ||
97
- ts . isMethodDeclaration ( member ) ||
98
- isGetOrSetAccessorDeclaration ( member ) ) &&
99
- ! isPrivateMember ( member )
100
- )
101
- . map ( member => ( {
102
- node : member ,
103
- identifier : member . name . getText ( ) ,
104
- // Naive, but [does.the.job()]
105
- pos : member . name . getStart ( ) + ( ts . isComputedPropertyName ( member . name ) ? 1 : 0 ) ,
106
- type : SymbolType . MEMBER ,
107
- fix : isFixTypes ? ( [ member . getStart ( ) , member . getEnd ( ) , FIX_FLAGS . NONE ] as Fix ) : undefined ,
108
- } ) )
109
- : [ ] ;
85
+ if ( ts . isClassDeclaration ( node ) && node . name ) {
86
+ const identifier = defaultKeyword ? 'default' : node . name . getText ( ) ;
87
+ const pos = ( node . name ?? node ) . getStart ( ) ;
88
+ const fix = getFix ( exportKeyword , defaultKeyword ) ;
89
+ const members = isReportClassMembers
90
+ ? node . members
91
+ . filter (
92
+ ( member ) : member is ts . MethodDeclaration | ts . PropertyDeclaration =>
93
+ ( ts . isPropertyDeclaration ( member ) ||
94
+ ts . isMethodDeclaration ( member ) ||
95
+ isGetOrSetAccessorDeclaration ( member ) ) &&
96
+ ! isPrivateMember ( member )
97
+ )
98
+ . map ( member => ( {
99
+ node : member ,
100
+ identifier : member . name . getText ( ) ,
101
+ // Naive, but [does.the.job()]
102
+ pos : member . name . getStart ( ) + ( ts . isComputedPropertyName ( member . name ) ? 1 : 0 ) ,
103
+ type : SymbolType . MEMBER ,
104
+ fix : isFixTypes ? ( [ member . getStart ( ) , member . getEnd ( ) , FIX_FLAGS . NONE ] as Fix ) : undefined ,
105
+ } ) )
106
+ : [ ] ;
110
107
111
- return { node, identifier, type : SymbolType . CLASS , pos, members, fix } ;
112
- }
108
+ return { node, identifier, type : SymbolType . CLASS , pos, members, fix } ;
109
+ }
113
110
114
- if ( ts . isTypeAliasDeclaration ( node ) ) {
115
- const identifier = node . name . getText ( ) ;
116
- const pos = node . name . getStart ( ) ;
117
- const fix = getTypeFix ( exportKeyword ) ;
118
- return { node, identifier, type : SymbolType . TYPE , pos, fix } ;
119
- }
111
+ if ( ts . isTypeAliasDeclaration ( node ) ) {
112
+ const identifier = node . name . getText ( ) ;
113
+ const pos = node . name . getStart ( ) ;
114
+ const fix = getTypeFix ( exportKeyword ) ;
115
+ return { node, identifier, type : SymbolType . TYPE , pos, fix } ;
116
+ }
120
117
121
- if ( ts . isInterfaceDeclaration ( node ) ) {
122
- const identifier = defaultKeyword ? 'default' : node . name . getText ( ) ;
123
- const pos = node . name . getStart ( ) ;
124
- const fix = getTypeFix ( exportKeyword ) ;
125
- return { node, identifier, type : SymbolType . INTERFACE , pos, fix } ;
126
- }
118
+ if ( ts . isInterfaceDeclaration ( node ) ) {
119
+ const identifier = defaultKeyword ? 'default' : node . name . getText ( ) ;
120
+ const pos = node . name . getStart ( ) ;
121
+ const fix = getTypeFix ( exportKeyword ) ;
122
+ return { node, identifier, type : SymbolType . INTERFACE , pos, fix } ;
123
+ }
127
124
128
- if ( ts . isEnumDeclaration ( node ) ) {
129
- const identifier = node . name . getText ( ) ;
130
- const pos = node . name . getStart ( ) ;
131
- const fix = getTypeFix ( exportKeyword ) ;
132
- const members = node . members . map ( member => ( {
133
- node : member ,
134
- identifier : stripQuotes ( member . name . getText ( ) ) ,
135
- pos : member . name . getStart ( ) ,
136
- type : SymbolType . MEMBER ,
137
- fix : isFixTypes
138
- ? ( [ member . getStart ( ) , member . getEnd ( ) , FIX_FLAGS . OBJECT_BINDING | FIX_FLAGS . WITH_NEWLINE ] as Fix )
139
- : undefined ,
140
- } ) ) ;
125
+ if ( ts . isEnumDeclaration ( node ) ) {
126
+ const identifier = node . name . getText ( ) ;
127
+ const pos = node . name . getStart ( ) ;
128
+ const fix = getTypeFix ( exportKeyword ) ;
129
+ const members = node . members . map ( member => ( {
130
+ node : member ,
131
+ identifier : stripQuotes ( member . name . getText ( ) ) ,
132
+ pos : member . name . getStart ( ) ,
133
+ type : SymbolType . MEMBER ,
134
+ fix : isFixTypes
135
+ ? ( [ member . getStart ( ) , member . getEnd ( ) , FIX_FLAGS . OBJECT_BINDING | FIX_FLAGS . WITH_NEWLINE ] as Fix )
136
+ : undefined ,
137
+ } ) ) ;
141
138
142
- return { node, identifier, type : SymbolType . ENUM , pos, members, fix } ;
143
- }
139
+ return { node, identifier, type : SymbolType . ENUM , pos, members, fix } ;
144
140
}
145
141
}
146
- ) ;
142
+ } ) ;
0 commit comments