Skip to content

Commit c61b739

Browse files
committed
chore: add eslint setup
1 parent db905f4 commit c61b739

18 files changed

+1947
-859
lines changed

.eslintrc.json

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
{
2+
"parser": "@typescript-eslint/parser",
3+
"plugins": ["@typescript-eslint", "prettier", "node"],
4+
"extends": [
5+
"eslint:recommended",
6+
"plugin:@typescript-eslint/recommended",
7+
"plugin:node/recommended",
8+
"plugin:prettier/recommended",
9+
"prettier"
10+
],
11+
"rules": {
12+
"prettier/prettier": "error",
13+
"node/no-unsupported-features/es-syntax": "off",
14+
"node/no-missing-import": "off",
15+
"@typescript-eslint/explicit-module-boundary-types": "off",
16+
"@typescript-eslint/ban-ts-comment": "off",
17+
"no-process-exit": "off",
18+
"node/shebang": "off",
19+
"@typescript-eslint/no-unused-vars": "off",
20+
"node/no-unpublished-import": "off",
21+
"node/no-unpublished-require": "off"
22+
}
23+
}

.prettierrc.json

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"trailingComma": "none",
3+
"tabWidth": 2,
4+
"semi": false,
5+
"singleQuote": true
6+
}

bin.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
#!/usr/bin/env node
2-
require('./dist/cli.js')
2+
require('./dist/cli.js')

package.json

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@
55
"bin": "bin.js",
66
"author": "Jakub Mazurek (@jayu) <jakub.mazurek.dev@gmail.com>",
77
"license": "MIT",
8+
"engines": {
9+
"node": ">=14"
10+
},
811
"devDependencies": {
912
"@babel/cli": "^7.16.0",
1013
"@babel/core": "^7.16.0",
@@ -33,7 +36,14 @@
3336
"typescript": "^4.5.2",
3437
"unzipper": "^0.10.11",
3538
"webpack": "^5.65.0",
36-
"webpack-cli": "^4.9.1"
39+
"webpack-cli": "^4.9.1",
40+
"@typescript-eslint/eslint-plugin": "^5.16.0",
41+
"@typescript-eslint/parser": "^5.16.0",
42+
"eslint": "^7.11.0",
43+
"eslint-config-prettier": "^8.5.0",
44+
"eslint-plugin-jest": "^24.1.0",
45+
"eslint-plugin-node": "^11.1.0",
46+
"eslint-plugin-prettier": "^4.0.0"
3747
},
3848
"dependencies": {
3949
"@babel/code-frame": "^7.16.0",
@@ -60,6 +70,8 @@
6070
"cli": "node ./dist/cli.js",
6171
"test": "yarn build && jest --passWithNoTests",
6272
"test:setup": "node ./dist/tools/getFixtures.js",
63-
"cruise": "depcruise --include-only \"^src\" --output-type dot src | dot -T svg > dependencygraph.svg"
73+
"cruise": "depcruise --include-only \"^src\" --output-type dot src | dot -T svg > dependencygraph.svg",
74+
"lint": "eslint --ext .js,.ts src",
75+
"lint:fix": "yarn lint --fix"
6476
}
65-
}
77+
}

src/astUtils.ts

Lines changed: 102 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,17 @@ import { wasmFns } from './wasm'
44
import { NODE_FIELDS } from '@babel/types'
55

66
export type Position = {
7-
line: number, column: number
7+
line: number
8+
column: number
89
}
910

1011
export 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

2930
export 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

4456
export const NodeConstructor = parse('').constructor //TODO: import proper constructor from somewhere
4557

@@ -48,20 +60,32 @@ export const isNode = (maybeNode: PoorNodeType) => {
4860
}
4961

5062
export 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

5470
const isNullOrUndef = (val: any) => val === null || val === undefined
5571

5672
const 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

6080
export 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

93124
export 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

97129
export 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

134166
export 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

Comments
 (0)