eslint-plugin-ziloen 0.0.110 → 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.vscode/settings.json +7 -0
- package/build.config.ts +12 -0
- package/package.json +16 -23
- package/src/index.ts +14 -0
- package/src/rules/generic-spacing.test.ts +40 -0
- package/src/rules/generic-spacing.ts +118 -0
- package/src/rules/jsx-strict-logical-expressions.ts +159 -0
- package/src/tsutils.ts +33 -0
- package/src/utils.ts +3 -0
- package/tsconfig.json +18 -0
- package/dist/index.cjs +0 -221
- package/dist/index.mjs +0 -219
package/build.config.ts
ADDED
package/package.json
CHANGED
|
@@ -1,28 +1,21 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "eslint-plugin-ziloen",
|
|
3
|
-
"version": "0.0
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
"module": "./dist/index.mjs",
|
|
9
|
-
"files": [
|
|
10
|
-
"dist"
|
|
11
|
-
],
|
|
12
|
-
"dependencies": {
|
|
13
|
-
"@typescript-eslint/utils": "^6.7.5",
|
|
14
|
-
"typescript": "^5.2.2"
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"main": "./dist/index.js",
|
|
5
|
+
"scripts": {
|
|
6
|
+
"build": "rimraf dist && tsup src/index.ts --format esm,cjs --clean",
|
|
7
|
+
"release": "pnpm run build && bumpp --commit --no-push && npm publish"
|
|
15
8
|
},
|
|
16
|
-
"
|
|
17
|
-
"@types/
|
|
18
|
-
"@
|
|
9
|
+
"dependencies": {
|
|
10
|
+
"@types/eslint": "^8.44.6",
|
|
11
|
+
"@types/node": "^20.8.7",
|
|
12
|
+
"@typescript-eslint/rule-tester": "^6.8.0",
|
|
13
|
+
"@typescript-eslint/utils": "^6.8.0",
|
|
14
|
+
"bumpp": "^9.2.0",
|
|
15
|
+
"eslint": "^8.52.0",
|
|
19
16
|
"rimraf": "^5.0.5",
|
|
20
|
-
"
|
|
21
|
-
"
|
|
22
|
-
|
|
23
|
-
"scripts": {
|
|
24
|
-
"build": "rimraf dist && unbuild",
|
|
25
|
-
"stub": "unbuild --stub",
|
|
26
|
-
"test": "vitest"
|
|
17
|
+
"tsup": "^7.2.0",
|
|
18
|
+
"typescript": "^5.2.2",
|
|
19
|
+
"unbuild": "^2.0.0"
|
|
27
20
|
}
|
|
28
|
-
}
|
|
21
|
+
}
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import genericSpacing from './rules/generic-spacing'
|
|
2
|
+
import jsxStrictLogicalExpressions from './rules/jsx-strict-logical-expressions'
|
|
3
|
+
|
|
4
|
+
const ziloen = {
|
|
5
|
+
meta: {
|
|
6
|
+
name: "ziloen"
|
|
7
|
+
},
|
|
8
|
+
rules: {
|
|
9
|
+
'generic-spacing': genericSpacing,
|
|
10
|
+
'jsx-strict-logical-expressions': jsxStrictLogicalExpressions,
|
|
11
|
+
}
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
export default ziloen
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
import { RuleTester } from '@typescript-eslint/rule-tester'
|
|
2
|
+
import rule, { RULE_NAME, messageId } from './generic-spacing'
|
|
3
|
+
|
|
4
|
+
const valid = [
|
|
5
|
+
`type Foo<T, K> = T`,
|
|
6
|
+
`type Foo<
|
|
7
|
+
T,
|
|
8
|
+
K,
|
|
9
|
+
> = T`,
|
|
10
|
+
`type Foo<T, K> = Record<T, K>`,
|
|
11
|
+
`type Foo<T, K> = Record<
|
|
12
|
+
T,
|
|
13
|
+
K
|
|
14
|
+
>`
|
|
15
|
+
]
|
|
16
|
+
|
|
17
|
+
const invalid = [
|
|
18
|
+
[`type Foo< T, K > = T`, `type Foo<T, K> = T`, 2],
|
|
19
|
+
[`type Foo<T, K> = Record< T, K >`, `type Foo<T, K> = Record<T, K>`, 2],
|
|
20
|
+
[`type Foo< T, K > = Record< T, K >`, `type Foo<T, K> = Record<T, K>`, 4],
|
|
21
|
+
[`type Foo<
|
|
22
|
+
T, K
|
|
23
|
+
> = Record< T, K >`, `type Foo<
|
|
24
|
+
T, K
|
|
25
|
+
> = Record<T, K>`, 2],
|
|
26
|
+
[`type Foo<T=any, K=any> = T`, `type Foo<T = any, K = any> = T`, 2]
|
|
27
|
+
] as const
|
|
28
|
+
|
|
29
|
+
const ruleTester: RuleTester = new RuleTester({
|
|
30
|
+
parser: require.resolve('@typescript-eslint/parser')
|
|
31
|
+
})
|
|
32
|
+
|
|
33
|
+
ruleTester.run(RULE_NAME, rule, {
|
|
34
|
+
valid,
|
|
35
|
+
invalid: invalid.map(i => ({
|
|
36
|
+
code: i[0],
|
|
37
|
+
output: i[1].trim(),
|
|
38
|
+
errors: Array.from({ length: i[2] || 1 }, () => ({ messageId }))
|
|
39
|
+
}))
|
|
40
|
+
})
|
|
@@ -0,0 +1,118 @@
|
|
|
1
|
+
import type { TSESTree } from '@typescript-eslint/utils'
|
|
2
|
+
import type { RuleContext } from '@typescript-eslint/utils/ts-eslint'
|
|
3
|
+
import { createEslintRule } from '../utils'
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
export const RULE_NAME = 'generic-spacing'
|
|
7
|
+
export type MessageIds = 'genericSpacingMismatch'
|
|
8
|
+
export type Options = []
|
|
9
|
+
|
|
10
|
+
export const messageId = 'genericSpacingMismatch'
|
|
11
|
+
|
|
12
|
+
function removeSpaceAround(
|
|
13
|
+
node: TSESTree.TSTypeParameterInstantiation | TSESTree.TSTypeParameterDeclaration,
|
|
14
|
+
context: Readonly<RuleContext<MessageIds, []>>
|
|
15
|
+
) {
|
|
16
|
+
const params = node.params
|
|
17
|
+
if (!params.length) return
|
|
18
|
+
|
|
19
|
+
const sourceCode = context.sourceCode
|
|
20
|
+
|
|
21
|
+
const start = node.range[0] + 1
|
|
22
|
+
const end = node.range[1] - 1
|
|
23
|
+
|
|
24
|
+
const startNode = params[0]
|
|
25
|
+
const endNode = params.at(-1)!
|
|
26
|
+
|
|
27
|
+
const textBeforeFirst = sourceCode.text.slice(start, startNode.range[0])
|
|
28
|
+
const textAfterLast = sourceCode.text.slice(endNode.range[1], end)
|
|
29
|
+
|
|
30
|
+
// if same line, remove spaces
|
|
31
|
+
if (textBeforeFirst.length && !textBeforeFirst.includes('\n')) {
|
|
32
|
+
context.report({
|
|
33
|
+
node,
|
|
34
|
+
messageId,
|
|
35
|
+
*fix(fixer) {
|
|
36
|
+
yield fixer.replaceTextRange([start, startNode.range[0]], '')
|
|
37
|
+
},
|
|
38
|
+
loc: {
|
|
39
|
+
start: {
|
|
40
|
+
column: node.loc.start.column + 1,
|
|
41
|
+
line: node.loc.start.line
|
|
42
|
+
},
|
|
43
|
+
end: startNode.loc.start
|
|
44
|
+
}
|
|
45
|
+
})
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
// if same line, remove spaces
|
|
49
|
+
if (textAfterLast.length && !textAfterLast.includes('\n')) {
|
|
50
|
+
context.report({
|
|
51
|
+
node,
|
|
52
|
+
messageId,
|
|
53
|
+
*fix(fixer) {
|
|
54
|
+
yield fixer.replaceTextRange([endNode.range[1], end], '')
|
|
55
|
+
},
|
|
56
|
+
loc: {
|
|
57
|
+
start: endNode.loc.end,
|
|
58
|
+
end: {
|
|
59
|
+
column: node.loc.end.column - 1,
|
|
60
|
+
line: node.loc.end.line
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
})
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
const genericSpacing = createEslintRule<Options, MessageIds>({
|
|
68
|
+
name: RULE_NAME,
|
|
69
|
+
meta: {
|
|
70
|
+
type: 'layout',
|
|
71
|
+
docs: {
|
|
72
|
+
description: '',
|
|
73
|
+
recommended: 'stylistic',
|
|
74
|
+
},
|
|
75
|
+
fixable: 'code',
|
|
76
|
+
schema: [],
|
|
77
|
+
messages: {
|
|
78
|
+
genericSpacingMismatch: 'spacing mismatch'
|
|
79
|
+
}
|
|
80
|
+
},
|
|
81
|
+
defaultOptions: [],
|
|
82
|
+
create(context) {
|
|
83
|
+
const sourceCode = context.sourceCode
|
|
84
|
+
|
|
85
|
+
return {
|
|
86
|
+
// type T<"K, S"> = any
|
|
87
|
+
TSTypeParameterDeclaration: node => removeSpaceAround(node, context),
|
|
88
|
+
// type T = Record<"K, S">
|
|
89
|
+
TSTypeParameterInstantiation: node => removeSpaceAround(node, context),
|
|
90
|
+
// type T<"K = any", "S = any"> = any
|
|
91
|
+
TSTypeParameter(node) {
|
|
92
|
+
if (!node.default) return
|
|
93
|
+
const startNode = node.constraint ?? node.name
|
|
94
|
+
const endNode = node.default
|
|
95
|
+
|
|
96
|
+
const start = startNode.range[1]
|
|
97
|
+
const end = endNode.range[0]
|
|
98
|
+
|
|
99
|
+
// replace all content between name and default with " = "
|
|
100
|
+
if (sourceCode.text.slice(start, end) !== ' = ') {
|
|
101
|
+
context.report({
|
|
102
|
+
node,
|
|
103
|
+
messageId,
|
|
104
|
+
*fix(fixer) {
|
|
105
|
+
yield fixer.replaceTextRange([start, end], ' = ')
|
|
106
|
+
},
|
|
107
|
+
loc: {
|
|
108
|
+
start: startNode.loc.end,
|
|
109
|
+
end: endNode.loc.start
|
|
110
|
+
}
|
|
111
|
+
})
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
})
|
|
117
|
+
|
|
118
|
+
export default genericSpacing
|
|
@@ -0,0 +1,159 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Copied from https://github.com/hluisson/eslint-plugin-jsx-expressions
|
|
3
|
+
*/
|
|
4
|
+
|
|
5
|
+
import { ESLintUtils, TSESTree } from '@typescript-eslint/utils'
|
|
6
|
+
import { TypeFlags } from 'typescript'
|
|
7
|
+
import { getConstrainedTypeAtLocation, isTypeFlagSet, unionTypeParts } from '../tsutils'
|
|
8
|
+
import { createEslintRule } from '../utils'
|
|
9
|
+
|
|
10
|
+
type Options = [
|
|
11
|
+
{
|
|
12
|
+
allowString?: boolean
|
|
13
|
+
allowNumber?: boolean
|
|
14
|
+
}
|
|
15
|
+
]
|
|
16
|
+
|
|
17
|
+
type MessageIds = 'conditionErrorFalseyString' | 'conditionErrorFalseyNumber'
|
|
18
|
+
|
|
19
|
+
export const RULE_NAME = 'jsx-strict-logical-expressions'
|
|
20
|
+
|
|
21
|
+
//------------------------------------------------------------------------------
|
|
22
|
+
// Rule Definition
|
|
23
|
+
//------------------------------------------------------------------------------
|
|
24
|
+
|
|
25
|
+
const jsxStrictLogicalExpressions = createEslintRule<Options, MessageIds>({
|
|
26
|
+
name: RULE_NAME,
|
|
27
|
+
defaultOptions: [{ allowString: false, allowNumber: false }],
|
|
28
|
+
meta: {
|
|
29
|
+
docs: {
|
|
30
|
+
description: 'Forbid non-boolean falsey values in inline expressions',
|
|
31
|
+
recommended: 'strict',
|
|
32
|
+
},
|
|
33
|
+
fixable: 'code',
|
|
34
|
+
type: 'problem',
|
|
35
|
+
schema: [
|
|
36
|
+
{
|
|
37
|
+
type: 'object',
|
|
38
|
+
properties: {
|
|
39
|
+
allowString: { type: 'boolean' },
|
|
40
|
+
allowNumber: { type: 'boolean' },
|
|
41
|
+
},
|
|
42
|
+
additionalProperties: false,
|
|
43
|
+
},
|
|
44
|
+
],
|
|
45
|
+
messages: {
|
|
46
|
+
conditionErrorFalseyString:
|
|
47
|
+
'Potentially falsey string in logical AND expression. Please use boolean.',
|
|
48
|
+
conditionErrorFalseyNumber:
|
|
49
|
+
'Potentially falsey number in logical AND expression. Please use boolean.',
|
|
50
|
+
},
|
|
51
|
+
},
|
|
52
|
+
|
|
53
|
+
create(context, [options]) {
|
|
54
|
+
const parserServices = ESLintUtils.getParserServices(context)
|
|
55
|
+
const typeChecker = parserServices.program.getTypeChecker()
|
|
56
|
+
|
|
57
|
+
function checkIdentifier(
|
|
58
|
+
node: TSESTree.Identifier
|
|
59
|
+
): MessageIds | undefined {
|
|
60
|
+
const tsNode = parserServices.esTreeNodeToTSNodeMap.get(node)
|
|
61
|
+
const types = unionTypeParts(
|
|
62
|
+
getConstrainedTypeAtLocation(typeChecker, tsNode)
|
|
63
|
+
)
|
|
64
|
+
|
|
65
|
+
const hasPotentiallyFalseyString = types.some(
|
|
66
|
+
type =>
|
|
67
|
+
isTypeFlagSet(type, TypeFlags.StringLike) &&
|
|
68
|
+
(!type.isStringLiteral() || type.value === '')
|
|
69
|
+
)
|
|
70
|
+
|
|
71
|
+
if (!options.allowString && hasPotentiallyFalseyString) {
|
|
72
|
+
return 'conditionErrorFalseyString'
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
const hasPotentiallyFalseyNumber = types.some(
|
|
76
|
+
type =>
|
|
77
|
+
isTypeFlagSet(
|
|
78
|
+
type,
|
|
79
|
+
TypeFlags.NumberLike | TypeFlags.BigIntLike
|
|
80
|
+
) &&
|
|
81
|
+
(!type.isNumberLiteral() || type.value === 0)
|
|
82
|
+
)
|
|
83
|
+
|
|
84
|
+
if (!options.allowNumber && hasPotentiallyFalseyNumber) {
|
|
85
|
+
return 'conditionErrorFalseyNumber'
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
return
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
function checkAndReportIdentifier(
|
|
92
|
+
node: TSESTree.Identifier,
|
|
93
|
+
fixNode: TSESTree.Expression
|
|
94
|
+
) {
|
|
95
|
+
const errorId = checkIdentifier(node)
|
|
96
|
+
if (errorId) {
|
|
97
|
+
context.report({
|
|
98
|
+
node,
|
|
99
|
+
messageId: errorId,
|
|
100
|
+
fix: fixer => fixer.insertTextBefore(fixNode, '!!'),
|
|
101
|
+
})
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
// Return the core identifier or expression
|
|
106
|
+
function determineNode(originalNode: TSESTree.Expression) {
|
|
107
|
+
let nodeToEvaluate = originalNode
|
|
108
|
+
if (nodeToEvaluate.type === TSESTree.AST_NODE_TYPES.ChainExpression) {
|
|
109
|
+
nodeToEvaluate = nodeToEvaluate.expression
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
if (
|
|
113
|
+
nodeToEvaluate.type === TSESTree.AST_NODE_TYPES.MemberExpression &&
|
|
114
|
+
nodeToEvaluate.property.type !==
|
|
115
|
+
TSESTree.AST_NODE_TYPES.PrivateIdentifier
|
|
116
|
+
) {
|
|
117
|
+
nodeToEvaluate = nodeToEvaluate.property
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
return nodeToEvaluate
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
function checkLogicalExpression(
|
|
124
|
+
expressionNode: TSESTree.LogicalExpression,
|
|
125
|
+
checkRightNode: boolean
|
|
126
|
+
) {
|
|
127
|
+
const leftNode = determineNode(expressionNode.left)
|
|
128
|
+
|
|
129
|
+
if (leftNode.type === TSESTree.AST_NODE_TYPES.LogicalExpression) {
|
|
130
|
+
checkLogicalExpression(leftNode, true)
|
|
131
|
+
} else if (leftNode.type === TSESTree.AST_NODE_TYPES.Identifier) {
|
|
132
|
+
checkAndReportIdentifier(leftNode, expressionNode.left)
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
if (checkRightNode) {
|
|
136
|
+
const rightNode = determineNode(expressionNode.right)
|
|
137
|
+
|
|
138
|
+
if (rightNode.type === TSESTree.AST_NODE_TYPES.Identifier) {
|
|
139
|
+
checkAndReportIdentifier(rightNode, expressionNode.right)
|
|
140
|
+
}
|
|
141
|
+
}
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
function checkJSXExpression(node: TSESTree.JSXExpressionContainer) {
|
|
145
|
+
if (
|
|
146
|
+
node.expression.type === TSESTree.AST_NODE_TYPES.LogicalExpression &&
|
|
147
|
+
node.expression.operator === '&&'
|
|
148
|
+
) {
|
|
149
|
+
checkLogicalExpression(node.expression, false)
|
|
150
|
+
}
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
return {
|
|
154
|
+
JSXExpressionContainer: checkJSXExpression,
|
|
155
|
+
}
|
|
156
|
+
},
|
|
157
|
+
})
|
|
158
|
+
|
|
159
|
+
export default jsxStrictLogicalExpressions
|
package/src/tsutils.ts
ADDED
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
import { TypeFlags, type Node, type Type, type TypeChecker, type UnionType } from 'typescript'
|
|
2
|
+
|
|
3
|
+
|
|
4
|
+
export function isUnionType(type: Type): type is UnionType {
|
|
5
|
+
return (type.flags & TypeFlags.Union) !== 0
|
|
6
|
+
}
|
|
7
|
+
|
|
8
|
+
/** Returns all types of a union type or an array containing `type` itself if it's no union type. */
|
|
9
|
+
export function unionTypeParts(type: Type): Type[] {
|
|
10
|
+
return isUnionType(type) ? type.types : [type]
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
function isFlagSet(obj: { flags: number }, flag: number) {
|
|
15
|
+
return (obj.flags & flag) !== 0
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
export const isTypeFlagSet: (type: Type, flag: TypeFlags) => boolean = isFlagSet
|
|
19
|
+
|
|
20
|
+
|
|
21
|
+
|
|
22
|
+
/**
|
|
23
|
+
* Resolves the given node's type. Will resolve to the type's generic constraint, if it has one.
|
|
24
|
+
*/
|
|
25
|
+
export function getConstrainedTypeAtLocation(
|
|
26
|
+
checker: TypeChecker,
|
|
27
|
+
node: Node
|
|
28
|
+
): Type {
|
|
29
|
+
const nodeType = checker.getTypeAtLocation(node)
|
|
30
|
+
const constrained = checker.getBaseConstraintOfType(nodeType)
|
|
31
|
+
|
|
32
|
+
return constrained ?? nodeType
|
|
33
|
+
}
|
package/src/utils.ts
ADDED
package/tsconfig.json
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
{
|
|
2
|
+
"compilerOptions": {
|
|
3
|
+
"target": "ESNext",
|
|
4
|
+
"lib": [
|
|
5
|
+
"ESNext"
|
|
6
|
+
],
|
|
7
|
+
"module": "ESNext",
|
|
8
|
+
"moduleResolution": "Bundler",
|
|
9
|
+
"resolveJsonModule": true,
|
|
10
|
+
"strict": true,
|
|
11
|
+
"esModuleInterop": true,
|
|
12
|
+
"skipDefaultLibCheck": true,
|
|
13
|
+
"skipLibCheck": true
|
|
14
|
+
},
|
|
15
|
+
"include": [
|
|
16
|
+
"src"
|
|
17
|
+
]
|
|
18
|
+
}
|
package/dist/index.cjs
DELETED
|
@@ -1,221 +0,0 @@
|
|
|
1
|
-
'use strict';
|
|
2
|
-
|
|
3
|
-
const utils = require('@typescript-eslint/utils');
|
|
4
|
-
const typescript = require('typescript');
|
|
5
|
-
|
|
6
|
-
const createEslintRule = utils.ESLintUtils.RuleCreator((ruleName) => ruleName);
|
|
7
|
-
|
|
8
|
-
const RULE_NAME$1 = "generic-spacing";
|
|
9
|
-
const messageId = "genericSpacingMismatch";
|
|
10
|
-
function removeSpaceAround(node, context) {
|
|
11
|
-
const params = node.params;
|
|
12
|
-
if (!params.length)
|
|
13
|
-
return;
|
|
14
|
-
const sourceCode = context.sourceCode;
|
|
15
|
-
const start = node.range[0] + 1;
|
|
16
|
-
const end = node.range[1] - 1;
|
|
17
|
-
const startNode = params[0];
|
|
18
|
-
const endNode = params.at(-1);
|
|
19
|
-
const textBeforeFirst = sourceCode.text.slice(start, startNode.range[0]);
|
|
20
|
-
const textAfterLast = sourceCode.text.slice(endNode.range[1], end);
|
|
21
|
-
if (textBeforeFirst.length && !textBeforeFirst.includes("\n")) {
|
|
22
|
-
context.report({
|
|
23
|
-
node,
|
|
24
|
-
messageId,
|
|
25
|
-
*fix(fixer) {
|
|
26
|
-
yield fixer.replaceTextRange([start, startNode.range[0]], "");
|
|
27
|
-
},
|
|
28
|
-
loc: {
|
|
29
|
-
start: {
|
|
30
|
-
column: node.loc.start.column + 1,
|
|
31
|
-
line: node.loc.start.line
|
|
32
|
-
},
|
|
33
|
-
end: startNode.loc.start
|
|
34
|
-
}
|
|
35
|
-
});
|
|
36
|
-
}
|
|
37
|
-
if (textAfterLast.length && !textAfterLast.includes("\n")) {
|
|
38
|
-
context.report({
|
|
39
|
-
node,
|
|
40
|
-
messageId,
|
|
41
|
-
*fix(fixer) {
|
|
42
|
-
yield fixer.replaceTextRange([endNode.range[1], end], "");
|
|
43
|
-
},
|
|
44
|
-
loc: {
|
|
45
|
-
start: endNode.loc.end,
|
|
46
|
-
end: {
|
|
47
|
-
column: node.loc.end.column - 1,
|
|
48
|
-
line: node.loc.end.line
|
|
49
|
-
}
|
|
50
|
-
}
|
|
51
|
-
});
|
|
52
|
-
}
|
|
53
|
-
}
|
|
54
|
-
const genericSpacing = createEslintRule({
|
|
55
|
-
name: RULE_NAME$1,
|
|
56
|
-
meta: {
|
|
57
|
-
type: "layout",
|
|
58
|
-
docs: {
|
|
59
|
-
description: "",
|
|
60
|
-
recommended: "stylistic"
|
|
61
|
-
},
|
|
62
|
-
fixable: "code",
|
|
63
|
-
schema: [],
|
|
64
|
-
messages: {
|
|
65
|
-
genericSpacingMismatch: "spacing mismatch"
|
|
66
|
-
}
|
|
67
|
-
},
|
|
68
|
-
defaultOptions: [],
|
|
69
|
-
create(context) {
|
|
70
|
-
const sourceCode = context.sourceCode;
|
|
71
|
-
return {
|
|
72
|
-
// type T<"K, S"> = any
|
|
73
|
-
TSTypeParameterDeclaration: (node) => removeSpaceAround(node, context),
|
|
74
|
-
// type T = Record<"K, S">
|
|
75
|
-
TSTypeParameterInstantiation: (node) => removeSpaceAround(node, context),
|
|
76
|
-
// type T<"K = any", "S = any"> = any
|
|
77
|
-
TSTypeParameter(node) {
|
|
78
|
-
if (!node.default)
|
|
79
|
-
return;
|
|
80
|
-
const startNode = node.constraint || node.name;
|
|
81
|
-
const endNode = node.default;
|
|
82
|
-
const start = startNode.range[1];
|
|
83
|
-
const end = endNode.range[0];
|
|
84
|
-
if (sourceCode.text.slice(start, end) !== " = ") {
|
|
85
|
-
context.report({
|
|
86
|
-
node,
|
|
87
|
-
messageId,
|
|
88
|
-
*fix(fixer) {
|
|
89
|
-
yield fixer.replaceTextRange([start, end], " = ");
|
|
90
|
-
},
|
|
91
|
-
loc: {
|
|
92
|
-
start: startNode.loc.end,
|
|
93
|
-
end: endNode.loc.start
|
|
94
|
-
}
|
|
95
|
-
});
|
|
96
|
-
}
|
|
97
|
-
}
|
|
98
|
-
};
|
|
99
|
-
}
|
|
100
|
-
});
|
|
101
|
-
|
|
102
|
-
function isUnionType(type) {
|
|
103
|
-
return (type.flags & typescript.TypeFlags.Union) !== 0;
|
|
104
|
-
}
|
|
105
|
-
function unionTypeParts(type) {
|
|
106
|
-
return isUnionType(type) ? type.types : [type];
|
|
107
|
-
}
|
|
108
|
-
function isFlagSet(obj, flag) {
|
|
109
|
-
return (obj.flags & flag) !== 0;
|
|
110
|
-
}
|
|
111
|
-
const isTypeFlagSet = isFlagSet;
|
|
112
|
-
function getConstrainedTypeAtLocation(checker, node) {
|
|
113
|
-
const nodeType = checker.getTypeAtLocation(node);
|
|
114
|
-
const constrained = checker.getBaseConstraintOfType(nodeType);
|
|
115
|
-
return constrained ?? nodeType;
|
|
116
|
-
}
|
|
117
|
-
|
|
118
|
-
const RULE_NAME = "jsx-strict-logical-expressions";
|
|
119
|
-
const jsxStrictLogicalExpressions = createEslintRule({
|
|
120
|
-
name: RULE_NAME,
|
|
121
|
-
defaultOptions: [{ allowString: false, allowNumber: false }],
|
|
122
|
-
meta: {
|
|
123
|
-
docs: {
|
|
124
|
-
description: "Forbid non-boolean falsey values in inline expressions",
|
|
125
|
-
recommended: "strict"
|
|
126
|
-
},
|
|
127
|
-
fixable: "code",
|
|
128
|
-
type: "problem",
|
|
129
|
-
schema: [
|
|
130
|
-
{
|
|
131
|
-
type: "object",
|
|
132
|
-
properties: {
|
|
133
|
-
allowString: { type: "boolean" },
|
|
134
|
-
allowNumber: { type: "boolean" }
|
|
135
|
-
},
|
|
136
|
-
additionalProperties: false
|
|
137
|
-
}
|
|
138
|
-
],
|
|
139
|
-
messages: {
|
|
140
|
-
conditionErrorFalseyString: "Potentially falsey string in logical AND expression. Please use boolean.",
|
|
141
|
-
conditionErrorFalseyNumber: "Potentially falsey number in logical AND expression. Please use boolean."
|
|
142
|
-
}
|
|
143
|
-
},
|
|
144
|
-
create(context, [options]) {
|
|
145
|
-
const parserServices = utils.ESLintUtils.getParserServices(context);
|
|
146
|
-
const typeChecker = parserServices.program.getTypeChecker();
|
|
147
|
-
function checkIdentifier(node) {
|
|
148
|
-
const tsNode = parserServices.esTreeNodeToTSNodeMap.get(node);
|
|
149
|
-
const types = unionTypeParts(
|
|
150
|
-
getConstrainedTypeAtLocation(typeChecker, tsNode)
|
|
151
|
-
);
|
|
152
|
-
const hasPotentiallyFalseyString = types.some(
|
|
153
|
-
(type) => isTypeFlagSet(type, typescript.TypeFlags.StringLike) && (!type.isStringLiteral() || type.value === "")
|
|
154
|
-
);
|
|
155
|
-
if (!options.allowString && hasPotentiallyFalseyString) {
|
|
156
|
-
return "conditionErrorFalseyString";
|
|
157
|
-
}
|
|
158
|
-
const hasPotentiallyFalseyNumber = types.some(
|
|
159
|
-
(type) => isTypeFlagSet(
|
|
160
|
-
type,
|
|
161
|
-
typescript.TypeFlags.NumberLike | typescript.TypeFlags.BigIntLike
|
|
162
|
-
) && (!type.isNumberLiteral() || type.value === 0)
|
|
163
|
-
);
|
|
164
|
-
if (!options.allowNumber && hasPotentiallyFalseyNumber) {
|
|
165
|
-
return "conditionErrorFalseyNumber";
|
|
166
|
-
}
|
|
167
|
-
return;
|
|
168
|
-
}
|
|
169
|
-
function checkAndReportIdentifier(node, fixNode) {
|
|
170
|
-
const errorId = checkIdentifier(node);
|
|
171
|
-
if (errorId) {
|
|
172
|
-
context.report({
|
|
173
|
-
node,
|
|
174
|
-
messageId: errorId,
|
|
175
|
-
fix: (fixer) => fixer.insertTextBefore(fixNode, "!!")
|
|
176
|
-
});
|
|
177
|
-
}
|
|
178
|
-
}
|
|
179
|
-
function determineNode(originalNode) {
|
|
180
|
-
let nodeToEvaluate = originalNode;
|
|
181
|
-
if (nodeToEvaluate.type === utils.TSESTree.AST_NODE_TYPES.ChainExpression) {
|
|
182
|
-
nodeToEvaluate = nodeToEvaluate.expression;
|
|
183
|
-
}
|
|
184
|
-
if (nodeToEvaluate.type === utils.TSESTree.AST_NODE_TYPES.MemberExpression && nodeToEvaluate.property.type !== utils.TSESTree.AST_NODE_TYPES.PrivateIdentifier) {
|
|
185
|
-
nodeToEvaluate = nodeToEvaluate.property;
|
|
186
|
-
}
|
|
187
|
-
return nodeToEvaluate;
|
|
188
|
-
}
|
|
189
|
-
function checkLogicalExpression(expressionNode, checkRightNode) {
|
|
190
|
-
const leftNode = determineNode(expressionNode.left);
|
|
191
|
-
if (leftNode.type === utils.TSESTree.AST_NODE_TYPES.LogicalExpression) {
|
|
192
|
-
checkLogicalExpression(leftNode, true);
|
|
193
|
-
} else if (leftNode.type === utils.TSESTree.AST_NODE_TYPES.Identifier) {
|
|
194
|
-
checkAndReportIdentifier(leftNode, expressionNode.left);
|
|
195
|
-
}
|
|
196
|
-
if (checkRightNode) {
|
|
197
|
-
const rightNode = determineNode(expressionNode.right);
|
|
198
|
-
if (rightNode.type === utils.TSESTree.AST_NODE_TYPES.Identifier) {
|
|
199
|
-
checkAndReportIdentifier(rightNode, expressionNode.right);
|
|
200
|
-
}
|
|
201
|
-
}
|
|
202
|
-
}
|
|
203
|
-
function checkJSXExpression(node) {
|
|
204
|
-
if (node.expression.type === utils.TSESTree.AST_NODE_TYPES.LogicalExpression && node.expression.operator === "&&") {
|
|
205
|
-
checkLogicalExpression(node.expression, false);
|
|
206
|
-
}
|
|
207
|
-
}
|
|
208
|
-
return {
|
|
209
|
-
JSXExpressionContainer: checkJSXExpression
|
|
210
|
-
};
|
|
211
|
-
}
|
|
212
|
-
});
|
|
213
|
-
|
|
214
|
-
const index = {
|
|
215
|
-
rules: {
|
|
216
|
-
"generic-spacing": genericSpacing,
|
|
217
|
-
"jsx-strict-logical-expressions": jsxStrictLogicalExpressions
|
|
218
|
-
}
|
|
219
|
-
};
|
|
220
|
-
|
|
221
|
-
module.exports = index;
|
package/dist/index.mjs
DELETED
|
@@ -1,219 +0,0 @@
|
|
|
1
|
-
import { ESLintUtils, TSESTree } from '@typescript-eslint/utils';
|
|
2
|
-
import { TypeFlags } from 'typescript';
|
|
3
|
-
|
|
4
|
-
const createEslintRule = ESLintUtils.RuleCreator((ruleName) => ruleName);
|
|
5
|
-
|
|
6
|
-
const RULE_NAME$1 = "generic-spacing";
|
|
7
|
-
const messageId = "genericSpacingMismatch";
|
|
8
|
-
function removeSpaceAround(node, context) {
|
|
9
|
-
const params = node.params;
|
|
10
|
-
if (!params.length)
|
|
11
|
-
return;
|
|
12
|
-
const sourceCode = context.sourceCode;
|
|
13
|
-
const start = node.range[0] + 1;
|
|
14
|
-
const end = node.range[1] - 1;
|
|
15
|
-
const startNode = params[0];
|
|
16
|
-
const endNode = params.at(-1);
|
|
17
|
-
const textBeforeFirst = sourceCode.text.slice(start, startNode.range[0]);
|
|
18
|
-
const textAfterLast = sourceCode.text.slice(endNode.range[1], end);
|
|
19
|
-
if (textBeforeFirst.length && !textBeforeFirst.includes("\n")) {
|
|
20
|
-
context.report({
|
|
21
|
-
node,
|
|
22
|
-
messageId,
|
|
23
|
-
*fix(fixer) {
|
|
24
|
-
yield fixer.replaceTextRange([start, startNode.range[0]], "");
|
|
25
|
-
},
|
|
26
|
-
loc: {
|
|
27
|
-
start: {
|
|
28
|
-
column: node.loc.start.column + 1,
|
|
29
|
-
line: node.loc.start.line
|
|
30
|
-
},
|
|
31
|
-
end: startNode.loc.start
|
|
32
|
-
}
|
|
33
|
-
});
|
|
34
|
-
}
|
|
35
|
-
if (textAfterLast.length && !textAfterLast.includes("\n")) {
|
|
36
|
-
context.report({
|
|
37
|
-
node,
|
|
38
|
-
messageId,
|
|
39
|
-
*fix(fixer) {
|
|
40
|
-
yield fixer.replaceTextRange([endNode.range[1], end], "");
|
|
41
|
-
},
|
|
42
|
-
loc: {
|
|
43
|
-
start: endNode.loc.end,
|
|
44
|
-
end: {
|
|
45
|
-
column: node.loc.end.column - 1,
|
|
46
|
-
line: node.loc.end.line
|
|
47
|
-
}
|
|
48
|
-
}
|
|
49
|
-
});
|
|
50
|
-
}
|
|
51
|
-
}
|
|
52
|
-
const genericSpacing = createEslintRule({
|
|
53
|
-
name: RULE_NAME$1,
|
|
54
|
-
meta: {
|
|
55
|
-
type: "layout",
|
|
56
|
-
docs: {
|
|
57
|
-
description: "",
|
|
58
|
-
recommended: "stylistic"
|
|
59
|
-
},
|
|
60
|
-
fixable: "code",
|
|
61
|
-
schema: [],
|
|
62
|
-
messages: {
|
|
63
|
-
genericSpacingMismatch: "spacing mismatch"
|
|
64
|
-
}
|
|
65
|
-
},
|
|
66
|
-
defaultOptions: [],
|
|
67
|
-
create(context) {
|
|
68
|
-
const sourceCode = context.sourceCode;
|
|
69
|
-
return {
|
|
70
|
-
// type T<"K, S"> = any
|
|
71
|
-
TSTypeParameterDeclaration: (node) => removeSpaceAround(node, context),
|
|
72
|
-
// type T = Record<"K, S">
|
|
73
|
-
TSTypeParameterInstantiation: (node) => removeSpaceAround(node, context),
|
|
74
|
-
// type T<"K = any", "S = any"> = any
|
|
75
|
-
TSTypeParameter(node) {
|
|
76
|
-
if (!node.default)
|
|
77
|
-
return;
|
|
78
|
-
const startNode = node.constraint || node.name;
|
|
79
|
-
const endNode = node.default;
|
|
80
|
-
const start = startNode.range[1];
|
|
81
|
-
const end = endNode.range[0];
|
|
82
|
-
if (sourceCode.text.slice(start, end) !== " = ") {
|
|
83
|
-
context.report({
|
|
84
|
-
node,
|
|
85
|
-
messageId,
|
|
86
|
-
*fix(fixer) {
|
|
87
|
-
yield fixer.replaceTextRange([start, end], " = ");
|
|
88
|
-
},
|
|
89
|
-
loc: {
|
|
90
|
-
start: startNode.loc.end,
|
|
91
|
-
end: endNode.loc.start
|
|
92
|
-
}
|
|
93
|
-
});
|
|
94
|
-
}
|
|
95
|
-
}
|
|
96
|
-
};
|
|
97
|
-
}
|
|
98
|
-
});
|
|
99
|
-
|
|
100
|
-
function isUnionType(type) {
|
|
101
|
-
return (type.flags & TypeFlags.Union) !== 0;
|
|
102
|
-
}
|
|
103
|
-
function unionTypeParts(type) {
|
|
104
|
-
return isUnionType(type) ? type.types : [type];
|
|
105
|
-
}
|
|
106
|
-
function isFlagSet(obj, flag) {
|
|
107
|
-
return (obj.flags & flag) !== 0;
|
|
108
|
-
}
|
|
109
|
-
const isTypeFlagSet = isFlagSet;
|
|
110
|
-
function getConstrainedTypeAtLocation(checker, node) {
|
|
111
|
-
const nodeType = checker.getTypeAtLocation(node);
|
|
112
|
-
const constrained = checker.getBaseConstraintOfType(nodeType);
|
|
113
|
-
return constrained ?? nodeType;
|
|
114
|
-
}
|
|
115
|
-
|
|
116
|
-
const RULE_NAME = "jsx-strict-logical-expressions";
|
|
117
|
-
const jsxStrictLogicalExpressions = createEslintRule({
|
|
118
|
-
name: RULE_NAME,
|
|
119
|
-
defaultOptions: [{ allowString: false, allowNumber: false }],
|
|
120
|
-
meta: {
|
|
121
|
-
docs: {
|
|
122
|
-
description: "Forbid non-boolean falsey values in inline expressions",
|
|
123
|
-
recommended: "strict"
|
|
124
|
-
},
|
|
125
|
-
fixable: "code",
|
|
126
|
-
type: "problem",
|
|
127
|
-
schema: [
|
|
128
|
-
{
|
|
129
|
-
type: "object",
|
|
130
|
-
properties: {
|
|
131
|
-
allowString: { type: "boolean" },
|
|
132
|
-
allowNumber: { type: "boolean" }
|
|
133
|
-
},
|
|
134
|
-
additionalProperties: false
|
|
135
|
-
}
|
|
136
|
-
],
|
|
137
|
-
messages: {
|
|
138
|
-
conditionErrorFalseyString: "Potentially falsey string in logical AND expression. Please use boolean.",
|
|
139
|
-
conditionErrorFalseyNumber: "Potentially falsey number in logical AND expression. Please use boolean."
|
|
140
|
-
}
|
|
141
|
-
},
|
|
142
|
-
create(context, [options]) {
|
|
143
|
-
const parserServices = ESLintUtils.getParserServices(context);
|
|
144
|
-
const typeChecker = parserServices.program.getTypeChecker();
|
|
145
|
-
function checkIdentifier(node) {
|
|
146
|
-
const tsNode = parserServices.esTreeNodeToTSNodeMap.get(node);
|
|
147
|
-
const types = unionTypeParts(
|
|
148
|
-
getConstrainedTypeAtLocation(typeChecker, tsNode)
|
|
149
|
-
);
|
|
150
|
-
const hasPotentiallyFalseyString = types.some(
|
|
151
|
-
(type) => isTypeFlagSet(type, TypeFlags.StringLike) && (!type.isStringLiteral() || type.value === "")
|
|
152
|
-
);
|
|
153
|
-
if (!options.allowString && hasPotentiallyFalseyString) {
|
|
154
|
-
return "conditionErrorFalseyString";
|
|
155
|
-
}
|
|
156
|
-
const hasPotentiallyFalseyNumber = types.some(
|
|
157
|
-
(type) => isTypeFlagSet(
|
|
158
|
-
type,
|
|
159
|
-
TypeFlags.NumberLike | TypeFlags.BigIntLike
|
|
160
|
-
) && (!type.isNumberLiteral() || type.value === 0)
|
|
161
|
-
);
|
|
162
|
-
if (!options.allowNumber && hasPotentiallyFalseyNumber) {
|
|
163
|
-
return "conditionErrorFalseyNumber";
|
|
164
|
-
}
|
|
165
|
-
return;
|
|
166
|
-
}
|
|
167
|
-
function checkAndReportIdentifier(node, fixNode) {
|
|
168
|
-
const errorId = checkIdentifier(node);
|
|
169
|
-
if (errorId) {
|
|
170
|
-
context.report({
|
|
171
|
-
node,
|
|
172
|
-
messageId: errorId,
|
|
173
|
-
fix: (fixer) => fixer.insertTextBefore(fixNode, "!!")
|
|
174
|
-
});
|
|
175
|
-
}
|
|
176
|
-
}
|
|
177
|
-
function determineNode(originalNode) {
|
|
178
|
-
let nodeToEvaluate = originalNode;
|
|
179
|
-
if (nodeToEvaluate.type === TSESTree.AST_NODE_TYPES.ChainExpression) {
|
|
180
|
-
nodeToEvaluate = nodeToEvaluate.expression;
|
|
181
|
-
}
|
|
182
|
-
if (nodeToEvaluate.type === TSESTree.AST_NODE_TYPES.MemberExpression && nodeToEvaluate.property.type !== TSESTree.AST_NODE_TYPES.PrivateIdentifier) {
|
|
183
|
-
nodeToEvaluate = nodeToEvaluate.property;
|
|
184
|
-
}
|
|
185
|
-
return nodeToEvaluate;
|
|
186
|
-
}
|
|
187
|
-
function checkLogicalExpression(expressionNode, checkRightNode) {
|
|
188
|
-
const leftNode = determineNode(expressionNode.left);
|
|
189
|
-
if (leftNode.type === TSESTree.AST_NODE_TYPES.LogicalExpression) {
|
|
190
|
-
checkLogicalExpression(leftNode, true);
|
|
191
|
-
} else if (leftNode.type === TSESTree.AST_NODE_TYPES.Identifier) {
|
|
192
|
-
checkAndReportIdentifier(leftNode, expressionNode.left);
|
|
193
|
-
}
|
|
194
|
-
if (checkRightNode) {
|
|
195
|
-
const rightNode = determineNode(expressionNode.right);
|
|
196
|
-
if (rightNode.type === TSESTree.AST_NODE_TYPES.Identifier) {
|
|
197
|
-
checkAndReportIdentifier(rightNode, expressionNode.right);
|
|
198
|
-
}
|
|
199
|
-
}
|
|
200
|
-
}
|
|
201
|
-
function checkJSXExpression(node) {
|
|
202
|
-
if (node.expression.type === TSESTree.AST_NODE_TYPES.LogicalExpression && node.expression.operator === "&&") {
|
|
203
|
-
checkLogicalExpression(node.expression, false);
|
|
204
|
-
}
|
|
205
|
-
}
|
|
206
|
-
return {
|
|
207
|
-
JSXExpressionContainer: checkJSXExpression
|
|
208
|
-
};
|
|
209
|
-
}
|
|
210
|
-
});
|
|
211
|
-
|
|
212
|
-
const index = {
|
|
213
|
-
rules: {
|
|
214
|
-
"generic-spacing": genericSpacing,
|
|
215
|
-
"jsx-strict-logical-expressions": jsxStrictLogicalExpressions
|
|
216
|
-
}
|
|
217
|
-
};
|
|
218
|
-
|
|
219
|
-
export { index as default };
|