@@ -4,16 +4,17 @@ import { wasmFns } from './wasm'
44import { NODE_FIELDS } from '@babel/types'
55
66export type Position = {
7- line : number , column : number
7+ line : number
8+ column : number
89}
910
1011export type Match = {
11- start : number ,
12- end : number ,
12+ start : number
13+ end : number
1314 loc : {
14- start : Position ,
15- end : Position ,
16- } ,
15+ start : Position
16+ end : Position
17+ }
1718 code : string
1819 query : string
1920}
@@ -27,7 +28,7 @@ export const getBody = (fileNode: PoorNodeType) => {
2728}
2829
2930export const unwrapExpressionStatement = ( node : PoorNodeType ) => {
30- if ( typeof node !== " object" ) {
31+ if ( typeof node !== ' object' ) {
3132 return node
3233 }
3334
@@ -38,8 +39,19 @@ export const unwrapExpressionStatement = (node: PoorNodeType) => {
3839 return node as PoorNodeType
3940}
4041
41- export const astPropsToSkip = [ 'loc' , 'start' , 'end' , 'extra' , 'trailingComments' , 'leadingComments' ]
42- export const IdentifierTypes = [ 'Identifier' , 'JSXIdentifier' , 'TSTypeParameter' ]
42+ export const astPropsToSkip = [
43+ 'loc' ,
44+ 'start' ,
45+ 'end' ,
46+ 'extra' ,
47+ 'trailingComments' ,
48+ 'leadingComments'
49+ ]
50+ export const IdentifierTypes = [
51+ 'Identifier' ,
52+ 'JSXIdentifier' ,
53+ 'TSTypeParameter'
54+ ]
4355
4456export const NodeConstructor = parse ( '' ) . constructor //TODO: import proper constructor from somewhere
4557
@@ -48,20 +60,32 @@ export const isNode = (maybeNode: PoorNodeType) => {
4860}
4961
5062export const isNodeArray = ( maybeNodeArr : PoorNodeType [ ] ) => {
51- return Array . isArray ( maybeNodeArr ) && maybeNodeArr . length > 0 && isNode ( maybeNodeArr [ 0 ] )
63+ return (
64+ Array . isArray ( maybeNodeArr ) &&
65+ maybeNodeArr . length > 0 &&
66+ isNode ( maybeNodeArr [ 0 ] )
67+ )
5268}
5369
5470const isNullOrUndef = ( val : any ) => val === null || val === undefined
5571
5672const isNodeFieldOptional = ( nodeType : string , nodeFieldKey : string ) => {
57- return Boolean ( ( NODE_FIELDS [ nodeType ] as { [ key : string ] : { optional : boolean } } ) [ nodeFieldKey ] ?. optional ?? true )
73+ return Boolean (
74+ ( NODE_FIELDS [ nodeType ] as { [ key : string ] : { optional : boolean } } ) [
75+ nodeFieldKey
76+ ] ?. optional ?? true
77+ )
5878}
5979
6080export const getKeysToCompare = ( node : PoorNodeType ) => {
6181 return Object . keys ( node ) . filter ( ( key ) => ! astPropsToSkip . includes ( key ) )
6282}
6383
64- export const getSetsOfKeysToCompare = ( fileNode : PoorNodeType , queryNode : PoorNodeType , isExact : boolean ) => {
84+ export const getSetsOfKeysToCompare = (
85+ fileNode : PoorNodeType ,
86+ queryNode : PoorNodeType ,
87+ isExact : boolean
88+ ) => {
6589 const exactFileKeys = getKeysToCompare ( fileNode )
6690 const exactQueryKeys = getKeysToCompare ( queryNode )
6791
@@ -74,28 +98,36 @@ export const getSetsOfKeysToCompare = (fileNode: PoorNodeType, queryNode: PoorNo
7498 * Exclude from file node all properties that
7599 * - are not present on query node or their value is falsy on query node (not specified)
76100 * - and are marked as optional in babel types
77- */
101+ */
78102
79- const fileKeysToRemove =
80- exactFileKeys . filter ( ( fileKey ) =>
81- ( ! exactQueryKeys . includes ( fileKey ) || isNullOrUndef ( queryNode [ fileKey ] ) )
82- && isNodeFieldOptional ( fileNode . type as string , fileKey )
83- )
103+ const fileKeysToRemove = exactFileKeys . filter (
104+ ( fileKey ) =>
105+ ( ! exactQueryKeys . includes ( fileKey ) ||
106+ isNullOrUndef ( queryNode [ fileKey ] ) ) &&
107+ isNodeFieldOptional ( fileNode . type as string , fileKey )
108+ )
84109
85- const includeFileKeys = exactFileKeys . filter ( ( fileKey ) => ! fileKeysToRemove . includes ( fileKey ) )
110+ const includeFileKeys = exactFileKeys . filter (
111+ ( fileKey ) => ! fileKeysToRemove . includes ( fileKey )
112+ )
86113
87114 // exclude all properties that has falsy value (otherwise properties set does not mach, if we remove these properties from file node)
88- const includeQueryKeys = exactQueryKeys . filter ( ( queryKey ) => ! fileKeysToRemove . includes ( queryKey ) && ! isNullOrUndef ( queryNode [ queryKey ] ) )
115+ const includeQueryKeys = exactQueryKeys . filter (
116+ ( queryKey ) =>
117+ ! fileKeysToRemove . includes ( queryKey ) &&
118+ ! isNullOrUndef ( queryNode [ queryKey ] )
119+ )
89120
90121 return [ includeFileKeys , includeQueryKeys ]
91122}
92123
93124export const SPACE_CHAR = ' '
94125
95- export const normalizeText = ( text : string ) => text . trim ( ) . replace ( / \s + / g, SPACE_CHAR )
126+ export const normalizeText = ( text : string ) =>
127+ text . trim ( ) . replace ( / \s + / g, SPACE_CHAR )
96128
97129export const sanitizeJSXText = ( node : PoorNodeType ) => {
98- wasmFns . trim_value ( node ) ;
130+ wasmFns . trim_value ( node )
99131 //@ts -ignore
100132 node . value = normalizeText ( node . value )
101133 //@ts -ignore
@@ -132,7 +164,6 @@ export const shouldCompareNode = (node: PoorNodeType) => {
132164}
133165
134166export const cleanupAst = ( ast : PoorNodeType ) => {
135-
136167 if ( ast . type === 'JSXText' ) {
137168 sanitizeJSXText ( ast )
138169 }
@@ -144,8 +175,9 @@ export const cleanupAst = (ast: PoorNodeType) => {
144175 cleanedAst [ key ] = cleanupAst ( cleanedAst [ key ] as PoorNodeType )
145176 }
146177 if ( isNodeArray ( cleanedAst [ key ] as PoorNodeType [ ] ) ) {
147-
148- cleanedAst [ key ] = ( cleanedAst [ key ] as PoorNodeType [ ] ) . filter ( shouldCompareNode ) . map ( ( subAst ) => cleanupAst ( subAst ) )
178+ cleanedAst [ key ] = ( cleanedAst [ key ] as PoorNodeType [ ] )
179+ . filter ( shouldCompareNode )
180+ . map ( ( subAst ) => cleanupAst ( subAst ) )
149181 }
150182 } )
151183
@@ -175,22 +207,35 @@ export const removeIdentifierRefFromWildcard = (name: string) => {
175207 return name
176208}
177209
178- // This is what happens if you write code at 01:30 at Friday after intensive week
179- export const sortByLeastIdentifierStrength = ( nodeA : PoorNodeType , nodeB : PoorNodeType ) => {
180- const aIsIdentifierWithWildcard = [ 'TSTypeReference' , ...IdentifierTypes ]
181- . includes ( nodeA . type as string ) && (
182- removeIdentifierRefFromWildcard ( nodeA . name as string ) ?. includes ( singleIdentifierWildcard )
183- || removeIdentifierRefFromWildcard ( ( nodeA as any ) ?. typeName ?. name as string ) ?. includes ( singleIdentifierWildcard )
184- )
185- const bIsIdentifierWithWildcard = [ 'TSTypeReference' , ...IdentifierTypes ]
186- . includes ( nodeB . type as string ) && (
187- removeIdentifierRefFromWildcard ( nodeB . name as string ) ?. includes ( singleIdentifierWildcard )
188- || removeIdentifierRefFromWildcard ( ( nodeB as any ) ?. typeName ?. name as string ) ?. includes ( singleIdentifierWildcard )
189- )
210+ // This is what happens if you write code at 01:30 at Friday after intensive week
211+ export const sortByLeastIdentifierStrength = (
212+ nodeA : PoorNodeType ,
213+ nodeB : PoorNodeType
214+ ) => {
215+ const aIsIdentifierWithWildcard =
216+ [ 'TSTypeReference' , ...IdentifierTypes ] . includes ( nodeA . type as string ) &&
217+ ( removeIdentifierRefFromWildcard ( nodeA . name as string ) ?. includes (
218+ singleIdentifierWildcard
219+ ) ||
220+ removeIdentifierRefFromWildcard (
221+ ( nodeA as any ) ?. typeName ?. name as string
222+ ) ?. includes ( singleIdentifierWildcard ) )
223+ const bIsIdentifierWithWildcard =
224+ [ 'TSTypeReference' , ...IdentifierTypes ] . includes ( nodeB . type as string ) &&
225+ ( removeIdentifierRefFromWildcard ( nodeB . name as string ) ?. includes (
226+ singleIdentifierWildcard
227+ ) ||
228+ removeIdentifierRefFromWildcard (
229+ ( nodeB as any ) ?. typeName ?. name as string
230+ ) ?. includes ( singleIdentifierWildcard ) )
190231
191232 if ( aIsIdentifierWithWildcard && bIsIdentifierWithWildcard ) {
192- const idA = removeIdentifierRefFromWildcard ( nodeA . name as string ) || removeIdentifierRefFromWildcard ( ( nodeA as any ) ?. typeName ?. name as string )
193- const idB = removeIdentifierRefFromWildcard ( nodeB . name as string ) || removeIdentifierRefFromWildcard ( ( nodeB as any ) ?. typeName ?. name as string )
233+ const idA =
234+ removeIdentifierRefFromWildcard ( nodeA . name as string ) ||
235+ removeIdentifierRefFromWildcard ( ( nodeA as any ) ?. typeName ?. name as string )
236+ const idB =
237+ removeIdentifierRefFromWildcard ( nodeB . name as string ) ||
238+ removeIdentifierRefFromWildcard ( ( nodeB as any ) ?. typeName ?. name as string )
194239
195240 if ( idA === doubleIdentifierWildcard ) {
196241 return 1
@@ -200,11 +245,16 @@ export const sortByLeastIdentifierStrength = (nodeA: PoorNodeType, nodeB: PoorNo
200245 return - 1
201246 }
202247
203- const aNonWildcardCharsLen = idA . split ( singleIdentifierWildcard ) . map ( ( str ) => str . length ) . reduce ( ( sum , len ) => sum + len , 0 )
204- const bNonWildcardCharsLen = idB . split ( singleIdentifierWildcard ) . map ( ( str ) => str . length ) . reduce ( ( sum , len ) => sum + len , 0 )
248+ const aNonWildcardCharsLen = idA
249+ . split ( singleIdentifierWildcard )
250+ . map ( ( str ) => str . length )
251+ . reduce ( ( sum , len ) => sum + len , 0 )
252+ const bNonWildcardCharsLen = idB
253+ . split ( singleIdentifierWildcard )
254+ . map ( ( str ) => str . length )
255+ . reduce ( ( sum , len ) => sum + len , 0 )
205256
206257 return bNonWildcardCharsLen - aNonWildcardCharsLen
207-
208258 }
209259
210260 if ( aIsIdentifierWithWildcard ) {
@@ -218,13 +268,20 @@ export const sortByLeastIdentifierStrength = (nodeA: PoorNodeType, nodeB: PoorNo
218268 return 0
219269}
220270
221- export const prepareCodeResult = ( { fileContent, start, end, loc } : { fileContent : string } & Omit < Match , 'code' | 'query' > ) => {
271+ export const prepareCodeResult = ( {
272+ fileContent,
273+ start,
274+ end,
275+ loc
276+ } : { fileContent : string } & Omit < Match , 'code' | 'query' > ) => {
222277 const frame = fileContent . substring ( start - loc . start . column , end )
223278 const firstLineWhiteCharsCountRegExp = new RegExp ( `^\\s*` )
224279
225280 const firstLine = frame . split ( '\n' ) [ 0 ]
226281 const lines = frame . substr ( loc . start . column ) . split ( '\n' )
227- const firstLineWhiteCharsCount = ( firstLine ?. match ( firstLineWhiteCharsCountRegExp ) as [ string ] ) [ 0 ] ?. length
282+ const firstLineWhiteCharsCount = (
283+ firstLine ?. match ( firstLineWhiteCharsCountRegExp ) as [ string ]
284+ ) [ 0 ] ?. length
228285
229286 const replaceRegex = new RegExp ( `^\\s{0,${ firstLineWhiteCharsCount } }` )
230287
@@ -233,4 +290,4 @@ export const prepareCodeResult = ({ fileContent, start, end, loc }: { fileConten
233290 }
234291
235292 return lines . join ( '\n' )
236- }
293+ }
0 commit comments