eslint-rule-forge 0.0.0-stage → 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/LICENSE +201 -0
- package/README.md +190 -2
- package/dist/bindings.d.mts +23 -0
- package/dist/bindings.d.mts.map +1 -0
- package/dist/bindings.mjs +39 -0
- package/dist/bindings.mjs.map +1 -0
- package/dist/imports.d.mts +54 -0
- package/dist/imports.d.mts.map +1 -0
- package/dist/imports.mjs +90 -0
- package/dist/imports.mjs.map +1 -0
- package/dist/index.d.mts +5 -0
- package/dist/index.d.mts.map +1 -0
- package/dist/index.mjs +5 -0
- package/dist/index.mjs.map +1 -0
- package/dist/parentheses.d.mts +21 -0
- package/dist/parentheses.d.mts.map +1 -0
- package/dist/parentheses.mjs +47 -0
- package/dist/parentheses.mjs.map +1 -0
- package/dist/type-wrappers.d.mts +44 -0
- package/dist/type-wrappers.d.mts.map +1 -0
- package/dist/type-wrappers.mjs +39 -0
- package/dist/type-wrappers.mjs.map +1 -0
- package/package.json +85 -3
- package/src/bindings.mts +62 -0
- package/src/imports.mts +172 -0
- package/src/index.mts +4 -0
- package/src/parentheses.mts +74 -0
- package/src/type-wrappers.mts +73 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,201 @@
|
|
|
1
|
+
Apache License
|
|
2
|
+
Version 2.0, January 2004
|
|
3
|
+
http://www.apache.org/licenses/
|
|
4
|
+
|
|
5
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
6
|
+
|
|
7
|
+
1. Definitions.
|
|
8
|
+
|
|
9
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
10
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
11
|
+
|
|
12
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
13
|
+
the copyright owner that is granting the License.
|
|
14
|
+
|
|
15
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
16
|
+
other entities that control, are controlled by, or are under common
|
|
17
|
+
control with that entity. For the purposes of this definition,
|
|
18
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
19
|
+
direction or management of such entity, whether by contract or
|
|
20
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
21
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
22
|
+
|
|
23
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
24
|
+
exercising permissions granted by this License.
|
|
25
|
+
|
|
26
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
27
|
+
including but not limited to software source code, documentation
|
|
28
|
+
source, and configuration files.
|
|
29
|
+
|
|
30
|
+
"Object" form shall mean any form resulting from mechanical
|
|
31
|
+
transformation or translation of a Source form, including but
|
|
32
|
+
not limited to compiled object code, generated documentation,
|
|
33
|
+
and conversions to other media types.
|
|
34
|
+
|
|
35
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
36
|
+
Object form, made available under the License, as indicated by a
|
|
37
|
+
copyright notice that is included in or attached to the work
|
|
38
|
+
(an example is provided in the Appendix below).
|
|
39
|
+
|
|
40
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
41
|
+
form, that is based on (or derived from) the Work and for which the
|
|
42
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
43
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
44
|
+
of this License, Derivative Works shall not include works that remain
|
|
45
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
46
|
+
the Work and Derivative Works thereof.
|
|
47
|
+
|
|
48
|
+
"Contribution" shall mean any work of authorship, including
|
|
49
|
+
the original version of the Work and any modifications or additions
|
|
50
|
+
to that Work or Derivative Works thereof, that is intentionally
|
|
51
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
|
52
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
|
53
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
|
54
|
+
means any form of electronic, verbal, or written communication sent
|
|
55
|
+
to the Licensor or its representatives, including but not limited to
|
|
56
|
+
communication on electronic mailing lists, source code control systems,
|
|
57
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
|
58
|
+
Licensor for the purpose of discussing and improving the Work, but
|
|
59
|
+
excluding communication that is conspicuously marked or otherwise
|
|
60
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
|
61
|
+
|
|
62
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
63
|
+
on behalf of whom a Contribution has been received by Licensor and
|
|
64
|
+
subsequently incorporated within the Work.
|
|
65
|
+
|
|
66
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
67
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
68
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
69
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
70
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
71
|
+
Work and such Derivative Works in Source or Object form.
|
|
72
|
+
|
|
73
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
74
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
75
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
76
|
+
(except as stated in this section) patent license to make, have made,
|
|
77
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
78
|
+
where such license applies only to those patent claims licensable
|
|
79
|
+
by such Contributor that are necessarily infringed by their
|
|
80
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
81
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
82
|
+
institute patent litigation against any entity (including a
|
|
83
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
84
|
+
or a Contribution incorporated within the Work constitutes direct
|
|
85
|
+
or contributory patent infringement, then any patent licenses
|
|
86
|
+
granted to You under this License for that Work shall terminate
|
|
87
|
+
as of the date such litigation is filed.
|
|
88
|
+
|
|
89
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
90
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
91
|
+
modifications, and in Source or Object form, provided that You
|
|
92
|
+
meet the following conditions:
|
|
93
|
+
|
|
94
|
+
(a) You must give any other recipients of the Work or
|
|
95
|
+
Derivative Works a copy of this License; and
|
|
96
|
+
|
|
97
|
+
(b) You must cause any modified files to carry prominent notices
|
|
98
|
+
stating that You changed the files; and
|
|
99
|
+
|
|
100
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
101
|
+
that You distribute, all copyright, patent, trademark, and
|
|
102
|
+
attribution notices from the Source form of the Work,
|
|
103
|
+
excluding those notices that do not pertain to any part of
|
|
104
|
+
the Derivative Works; and
|
|
105
|
+
|
|
106
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
107
|
+
distribution, then any Derivative Works that You distribute must
|
|
108
|
+
include a readable copy of the attribution notices contained
|
|
109
|
+
within such NOTICE file, excluding those notices that do not
|
|
110
|
+
pertain to any part of the Derivative Works, in at least one
|
|
111
|
+
of the following places: within a NOTICE text file distributed
|
|
112
|
+
as part of the Derivative Works; within the Source form or
|
|
113
|
+
documentation, if provided along with the Derivative Works; or,
|
|
114
|
+
within a display generated by the Derivative Works, if and
|
|
115
|
+
wherever such third-party notices normally appear. The contents
|
|
116
|
+
of the NOTICE file are for informational purposes only and
|
|
117
|
+
do not modify the License. You may add Your own attribution
|
|
118
|
+
notices within Derivative Works that You distribute, alongside
|
|
119
|
+
or as an addendum to the NOTICE text from the Work, provided
|
|
120
|
+
that such additional attribution notices cannot be construed
|
|
121
|
+
as modifying the License.
|
|
122
|
+
|
|
123
|
+
You may add Your own copyright statement to Your modifications and
|
|
124
|
+
may provide additional or different license terms and conditions
|
|
125
|
+
for use, reproduction, or distribution of Your modifications, or
|
|
126
|
+
for any such Derivative Works as a whole, provided Your use,
|
|
127
|
+
reproduction, and distribution of the Work otherwise complies with
|
|
128
|
+
the conditions stated in this License.
|
|
129
|
+
|
|
130
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
131
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
132
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
133
|
+
this License, without any additional terms or conditions.
|
|
134
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
135
|
+
the terms of any separate license agreement you may have executed
|
|
136
|
+
with Licensor regarding such Contributions.
|
|
137
|
+
|
|
138
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
139
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
140
|
+
except as required for reasonable and customary use in describing the
|
|
141
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
|
142
|
+
|
|
143
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
144
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
145
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
146
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
147
|
+
implied, including, without limitation, any warranties or conditions
|
|
148
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
149
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
150
|
+
appropriateness of using or redistributing the Work and assume any
|
|
151
|
+
risks associated with Your exercise of permissions under this License.
|
|
152
|
+
|
|
153
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
154
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
155
|
+
unless required by applicable law (such as deliberate and grossly
|
|
156
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
157
|
+
liable to You for damages, including any direct, indirect, special,
|
|
158
|
+
incidental, or consequential damages of any character arising as a
|
|
159
|
+
result of this License or out of the use or inability to use the
|
|
160
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
161
|
+
work stoppage, computer failure or malfunction, or any and all
|
|
162
|
+
other commercial damages or losses), even if such Contributor
|
|
163
|
+
has been advised of the possibility of such damages.
|
|
164
|
+
|
|
165
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
166
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
|
167
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
168
|
+
or other liability obligations and/or rights consistent with this
|
|
169
|
+
License. However, in accepting such obligations, You may act only
|
|
170
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
|
171
|
+
of any other Contributor, and only if You agree to indemnify,
|
|
172
|
+
defend, and hold each Contributor harmless for any liability
|
|
173
|
+
incurred by, or claims asserted against, such Contributor by reason
|
|
174
|
+
of your accepting any such warranty or additional liability.
|
|
175
|
+
|
|
176
|
+
END OF TERMS AND CONDITIONS
|
|
177
|
+
|
|
178
|
+
APPENDIX: How to apply the Apache License to your work.
|
|
179
|
+
|
|
180
|
+
To apply the Apache License to your work, attach the following
|
|
181
|
+
boilerplate notice, with the fields enclosed by brackets "[]"
|
|
182
|
+
replaced with your own identifying information. (Don't include
|
|
183
|
+
the brackets!) The text should be enclosed in the appropriate
|
|
184
|
+
comment syntax for the file format. We also recommend that a
|
|
185
|
+
file or class name and description of purpose be included on the
|
|
186
|
+
same "printed page" as the copyright notice for easier
|
|
187
|
+
identification within third-party archives.
|
|
188
|
+
|
|
189
|
+
Copyright [yyyy] [name of copyright owner]
|
|
190
|
+
|
|
191
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
192
|
+
you may not use this file except in compliance with the License.
|
|
193
|
+
You may obtain a copy of the License at
|
|
194
|
+
|
|
195
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
196
|
+
|
|
197
|
+
Unless required by applicable law or agreed to in writing, software
|
|
198
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
199
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
200
|
+
See the License for the specific language governing permissions and
|
|
201
|
+
limitations under the License.
|
package/README.md
CHANGED
|
@@ -1,3 +1,191 @@
|
|
|
1
|
-
#
|
|
1
|
+
# eslint-rule-forge
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
Helpers for writing [typescript-eslint](https://typescript-eslint.io/) rules
|
|
4
|
+
that are hard to fool: they see through type wrappers and parentheses, and
|
|
5
|
+
resolve what a file imports however it is written.
|
|
6
|
+
|
|
7
|
+
A rule that matches `Number(x)` but not `(Number as NumberConstructor)(x)` has
|
|
8
|
+
a hole a wrapper slips through, and one that asks the type of `x as string`
|
|
9
|
+
trusts a claim the checker never verified. These are the helpers the
|
|
10
|
+
`eslint-plugin-ts-*` packages and `eslint-config-typed` share for that, in one
|
|
11
|
+
place, so that a fix to one of them reaches every rule.
|
|
12
|
+
|
|
13
|
+
## Installation
|
|
14
|
+
|
|
15
|
+
```sh
|
|
16
|
+
npm install eslint-rule-forge
|
|
17
|
+
```
|
|
18
|
+
|
|
19
|
+
`@typescript-eslint/utils` comes with it; `eslint` and `typescript` are peer
|
|
20
|
+
dependencies.
|
|
21
|
+
|
|
22
|
+
## Type wrappers
|
|
23
|
+
|
|
24
|
+
`x as T`, `x!`, `x satisfies T` and `<T>x` change a type and never a value.
|
|
25
|
+
|
|
26
|
+
| Helper | What it gives |
|
|
27
|
+
| :------------------ | :------------------------------------------------------------------------------------ |
|
|
28
|
+
| `isTypeWrapper` | whether a node is one of the four |
|
|
29
|
+
| `skipTypeWrappers` | the expression inside every wrapper around a node: judge syntax on this |
|
|
30
|
+
| `typeWrapperLayers` | the node and every expression inside its wrappers, outermost first: ask types of each |
|
|
31
|
+
| `TypeWrapper` | the four node types |
|
|
32
|
+
|
|
33
|
+
All of them take a readonly (`DeepReadonly<TSESTree.Node>`) or a mutable node
|
|
34
|
+
and give back the same kind.
|
|
35
|
+
|
|
36
|
+
Judge syntax on what `skipTypeWrappers` returns, and write the fix from the
|
|
37
|
+
original node, so that the wrapper is kept:
|
|
38
|
+
|
|
39
|
+
```ts
|
|
40
|
+
import { AST_NODE_TYPES, type TSESLint } from '@typescript-eslint/utils';
|
|
41
|
+
import { getArgumentText, skipTypeWrappers } from 'eslint-rule-forge';
|
|
42
|
+
|
|
43
|
+
// Reports `Number(x)`, also written `(Number as NumberConstructor)(x)` or
|
|
44
|
+
// `Number!(x)`, and rewrites it to `toNumber(x)`.
|
|
45
|
+
export const noNumberCall: TSESLint.RuleModule<'noNumberCall'> = {
|
|
46
|
+
meta: {
|
|
47
|
+
type: 'suggestion',
|
|
48
|
+
messages: { noNumberCall: 'Use `toNumber` instead of `Number`.' },
|
|
49
|
+
fixable: 'code',
|
|
50
|
+
schema: [],
|
|
51
|
+
},
|
|
52
|
+
defaultOptions: [],
|
|
53
|
+
create: (context) => ({
|
|
54
|
+
CallExpression: (node) => {
|
|
55
|
+
// Judge the syntax on the expression inside the wrappers ...
|
|
56
|
+
const callee = skipTypeWrappers(node.callee);
|
|
57
|
+
|
|
58
|
+
const [argument] = node.arguments;
|
|
59
|
+
|
|
60
|
+
if (
|
|
61
|
+
argument === undefined ||
|
|
62
|
+
callee.type !== AST_NODE_TYPES.Identifier ||
|
|
63
|
+
callee.name !== 'Number'
|
|
64
|
+
) {
|
|
65
|
+
return;
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
context.report({
|
|
69
|
+
node,
|
|
70
|
+
messageId: 'noNumberCall',
|
|
71
|
+
// ... and write the fix from the original nodes, keeping a sequence
|
|
72
|
+
// argument `(a, b)` one argument.
|
|
73
|
+
fix: (fixer) =>
|
|
74
|
+
fixer.replaceText(
|
|
75
|
+
node,
|
|
76
|
+
`toNumber(${getArgumentText(context.sourceCode, argument)})`,
|
|
77
|
+
),
|
|
78
|
+
});
|
|
79
|
+
},
|
|
80
|
+
}),
|
|
81
|
+
} as const;
|
|
82
|
+
```
|
|
83
|
+
|
|
84
|
+
A conclusion drawn from a type — that a guard always holds, that a value is an
|
|
85
|
+
array — has to hold at every layer `typeWrapperLayers` returns. A widening
|
|
86
|
+
assertion (`x as string | null`) then makes the rule more cautious, and a
|
|
87
|
+
narrowing one (`x!`, `v as string` on an `unknown`), which the checker cannot
|
|
88
|
+
verify, changes nothing.
|
|
89
|
+
|
|
90
|
+
## Parentheses
|
|
91
|
+
|
|
92
|
+
The range and the text of a node leave out the parentheses written around it.
|
|
93
|
+
|
|
94
|
+
| Helper | What it gives |
|
|
95
|
+
| :------------------- | :------------------------------------------------------------------ |
|
|
96
|
+
| `parenthesesDepth` | how many pairs of parentheses surround a node |
|
|
97
|
+
| `getRangeWithParens` | the node's range including them |
|
|
98
|
+
| `getArgumentText` | the node's text for an argument list, `(a, b)` kept as one argument |
|
|
99
|
+
|
|
100
|
+
## Imports and bindings
|
|
101
|
+
|
|
102
|
+
| Helper | What it gives |
|
|
103
|
+
| :---------------------- | :--------------------------------------------------------------------------------------------------- |
|
|
104
|
+
| `getImportDeclarations` | every `import … from '<module>'` in the file, not only the first |
|
|
105
|
+
| `getNamedImports` | the export names those declarations import |
|
|
106
|
+
| `getImportedLocalName` | the local name an export is imported as (`'y'` for `import { x as y }`) |
|
|
107
|
+
| `buildCalleeResolver` | which export a callee calls, through aliases, namespace imports and type wrappers |
|
|
108
|
+
| `buildNamedImportFix` | a fix inserting `import { … } from '<module>';`, for an import sorter to place |
|
|
109
|
+
| `hasForeignBinding` | whether a name, at a node, is bound to something other than the module's import (a fix must give up) |
|
|
110
|
+
| `isLocallyBound` | whether a name, at a node, is declared in the file rather than a global or a standard-library type |
|
|
111
|
+
|
|
112
|
+
```ts
|
|
113
|
+
import type { TSESLint } from '@typescript-eslint/utils';
|
|
114
|
+
import {
|
|
115
|
+
buildCalleeResolver,
|
|
116
|
+
buildNamedImportFix,
|
|
117
|
+
getImportDeclarations,
|
|
118
|
+
getImportedLocalName,
|
|
119
|
+
hasForeignBinding,
|
|
120
|
+
} from 'eslint-rule-forge';
|
|
121
|
+
|
|
122
|
+
const MODULE = 'my-lib';
|
|
123
|
+
|
|
124
|
+
// Rewrites a call to `isNull` from `my-lib`, however it is imported, to
|
|
125
|
+
// `isNullish` from the same module, adding the import when it is missing.
|
|
126
|
+
export const preferIsNullish: TSESLint.RuleModule<'preferIsNullish'> = {
|
|
127
|
+
meta: {
|
|
128
|
+
type: 'suggestion',
|
|
129
|
+
messages: { preferIsNullish: 'Use `isNullish`.' },
|
|
130
|
+
fixable: 'code',
|
|
131
|
+
schema: [],
|
|
132
|
+
},
|
|
133
|
+
defaultOptions: [],
|
|
134
|
+
create: (context) => {
|
|
135
|
+
const program = context.sourceCode.ast;
|
|
136
|
+
|
|
137
|
+
const declarations = getImportDeclarations(program, MODULE);
|
|
138
|
+
|
|
139
|
+
// `isNull(x)`, `n(x)` after `import { isNull as n }` and `lib.isNull(x)`
|
|
140
|
+
// after `import * as lib` all call `isNull`.
|
|
141
|
+
const resolve = buildCalleeResolver(declarations);
|
|
142
|
+
|
|
143
|
+
return {
|
|
144
|
+
CallExpression: (node) => {
|
|
145
|
+
const resolved = resolve(node.callee);
|
|
146
|
+
|
|
147
|
+
if (resolved?.canonicalName !== 'isNull') {
|
|
148
|
+
return;
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
const importedAs = getImportedLocalName(
|
|
152
|
+
declarations,
|
|
153
|
+
'isNullish',
|
|
154
|
+
);
|
|
155
|
+
|
|
156
|
+
const name = resolved.isNamespace
|
|
157
|
+
? 'isNullish'
|
|
158
|
+
: (importedAs ?? 'isNullish');
|
|
159
|
+
|
|
160
|
+
// A local `isNullish` that is not `my-lib`'s would capture the call.
|
|
161
|
+
const canFix =
|
|
162
|
+
resolved.isNamespace ||
|
|
163
|
+
!hasForeignBinding(context.sourceCode, node, name, MODULE);
|
|
164
|
+
|
|
165
|
+
context.report({
|
|
166
|
+
node,
|
|
167
|
+
messageId: 'preferIsNullish',
|
|
168
|
+
fix: canFix
|
|
169
|
+
? (fixer) => [
|
|
170
|
+
...(importedAs !== undefined ||
|
|
171
|
+
resolved.isNamespace
|
|
172
|
+
? []
|
|
173
|
+
: buildNamedImportFix(
|
|
174
|
+
fixer,
|
|
175
|
+
program,
|
|
176
|
+
MODULE,
|
|
177
|
+
[name],
|
|
178
|
+
)),
|
|
179
|
+
fixer.replaceText(resolved.propertyNode, name),
|
|
180
|
+
]
|
|
181
|
+
: null,
|
|
182
|
+
});
|
|
183
|
+
},
|
|
184
|
+
};
|
|
185
|
+
},
|
|
186
|
+
} as const;
|
|
187
|
+
```
|
|
188
|
+
|
|
189
|
+
## License
|
|
190
|
+
|
|
191
|
+
[Apache-2.0](https://github.com/noshiro-pf/mono/blob/main/libs/eslint-rule-forge/LICENSE)
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import { type TSESLint, type TSESTree } from '@typescript-eslint/utils';
|
|
2
|
+
/**
|
|
3
|
+
* Whether `name`, read at `node`, resolves to something this file writes down
|
|
4
|
+
* — a declaration, a parameter, an import — rather than to a global or a
|
|
5
|
+
* standard-library type of the same name.
|
|
6
|
+
*
|
|
7
|
+
* The scope manager makes an implicit variable for every reference it cannot
|
|
8
|
+
* resolve, so `findVariable` finds `Exclude` in a file that never declares it;
|
|
9
|
+
* only a variable with a definition was written in the file.
|
|
10
|
+
*/
|
|
11
|
+
export declare const isLocallyBound: (sourceCode: TSESLint.SourceCode, node: TSESTree.Node, name: string) => boolean;
|
|
12
|
+
/**
|
|
13
|
+
* Whether `name`, read at `node`, resolves to a binding that is not an import
|
|
14
|
+
* from `moduleName` — a declaration, a parameter, or an import from another
|
|
15
|
+
* module. A fix that makes code at `node` refer to `moduleName`'s export
|
|
16
|
+
* `name` has to give up then, because the name would refer to that binding
|
|
17
|
+
* instead.
|
|
18
|
+
*
|
|
19
|
+
* The innermost binding decides: an import from `moduleName` at the top of the
|
|
20
|
+
* file does not help a name a function shadows around `node`.
|
|
21
|
+
*/
|
|
22
|
+
export declare const hasForeignBinding: (sourceCode: TSESLint.SourceCode, node: TSESTree.Node, name: string, moduleName: string) => boolean;
|
|
23
|
+
//# sourceMappingURL=bindings.d.mts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"bindings.d.mts","sourceRoot":"","sources":["../src/bindings.mts"],"names":[],"mappings":"AAAA,OAAO,EAGL,KAAK,QAAQ,EACb,KAAK,QAAQ,EACd,MAAM,0BAA0B,CAAC;AAIlC;;;;;;;;GAQG;AACH,eAAO,MAAM,cAAc,eACb,QAAQ,CAAC,UAAU,QACzB,QAAQ,CAAC,IAAI,QACb,MAAM,KACX,OAIF,CAAC;AAEF;;;;;;;;;GASG;AACH,eAAO,MAAM,iBAAiB,eAChB,QAAQ,CAAC,UAAU,QACzB,QAAQ,CAAC,IAAI,QACb,MAAM,cACA,MAAM,KACjB,OAIF,CAAC"}
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
import { AST_NODE_TYPES, ASTUtils, } from '@typescript-eslint/utils';
|
|
2
|
+
|
|
3
|
+
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
export const isLocallyBound = (sourceCode, node, name) => {
|
|
13
|
+
const variable = ASTUtils.findVariable(sourceCode.getScope(node), name);
|
|
14
|
+
return variable !== null && variable.defs.length > 0;
|
|
15
|
+
};
|
|
16
|
+
|
|
17
|
+
|
|
18
|
+
|
|
19
|
+
|
|
20
|
+
|
|
21
|
+
|
|
22
|
+
|
|
23
|
+
|
|
24
|
+
|
|
25
|
+
|
|
26
|
+
export const hasForeignBinding = (sourceCode, node, name, moduleName) => {
|
|
27
|
+
const variable = ASTUtils.findVariable(sourceCode.getScope(node), name);
|
|
28
|
+
return variable?.defs.some((def) => !isImportFrom(def, moduleName)) ?? false;
|
|
29
|
+
};
|
|
30
|
+
|
|
31
|
+
|
|
32
|
+
|
|
33
|
+
|
|
34
|
+
const isImportFrom = (def, moduleName) => (def.node.type === AST_NODE_TYPES.ImportSpecifier ||
|
|
35
|
+
def.node.type === AST_NODE_TYPES.ImportDefaultSpecifier ||
|
|
36
|
+
def.node.type === AST_NODE_TYPES.ImportNamespaceSpecifier) &&
|
|
37
|
+
def.node.parent.type === AST_NODE_TYPES.ImportDeclaration &&
|
|
38
|
+
def.node.parent.source.value === moduleName;
|
|
39
|
+
//# sourceMappingURL=bindings.mjs.map
|
|
@@ -0,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B;IACzD,GAAG,CAAC,IAAI,CAAC,MAAM,CAAC,MAAM,CAAC,KAAK,KAAK,UAAU,CAAC"}
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
import { type TSESLint, type TSESTree } from '@typescript-eslint/utils';
|
|
2
|
+
import type { DeepReadonly } from 'ts-type-forge';
|
|
3
|
+
/**
|
|
4
|
+
* Every `import … from '<moduleName>'` declaration in the file, not only the
|
|
5
|
+
* first: `import type { A } from 'm'` and `import { b } from 'm'` are two
|
|
6
|
+
* declarations, and a name imported by either is in scope.
|
|
7
|
+
*/
|
|
8
|
+
export declare const getImportDeclarations: (program: TSESTree.Program, moduleName: string) => readonly TSESTree.ImportDeclaration[];
|
|
9
|
+
/**
|
|
10
|
+
* The exported names `declarations` import by name, whatever they are bound to
|
|
11
|
+
* locally: `import { a, b as c }` gives `a` and `b`.
|
|
12
|
+
*/
|
|
13
|
+
export declare const getNamedImports: (declarations: DeepReadonly<TSESTree.ImportDeclaration[]>) => readonly string[];
|
|
14
|
+
/**
|
|
15
|
+
* The local name `declarations` bind the export `importedName` to, or
|
|
16
|
+
* `undefined` when they do not import it: `'y'` for `import { x as y }`. A fix
|
|
17
|
+
* writes this rather than the exported name, which may not be in scope.
|
|
18
|
+
*/
|
|
19
|
+
export declare const getImportedLocalName: (declarations: DeepReadonly<TSESTree.ImportDeclaration[]>, importedName: string) => string | undefined;
|
|
20
|
+
/** What {@link buildCalleeResolver}'s resolver says a callee calls. */
|
|
21
|
+
export type ResolvedCallee = Readonly<{
|
|
22
|
+
/** The exported name, whatever the callee calls it locally. */
|
|
23
|
+
canonicalName: string;
|
|
24
|
+
/**
|
|
25
|
+
* The identifier to rewrite to change what is called: the callee itself for
|
|
26
|
+
* a named import, the property for a namespace import.
|
|
27
|
+
*/
|
|
28
|
+
propertyNode: TSESTree.Identifier;
|
|
29
|
+
/** Whether the call goes through a namespace import (`m.f(...)`). */
|
|
30
|
+
isNamespace: boolean;
|
|
31
|
+
}>;
|
|
32
|
+
/**
|
|
33
|
+
* Builds a function that says which export of the module `declarations`
|
|
34
|
+
* import a callee calls, or `undefined` when it calls none of them. It knows
|
|
35
|
+
* named imports, aliased ones (`import { f as g }` → `g(...)` calls `f`) and
|
|
36
|
+
* namespace imports (`import * as m` → `m.f(...)`), and reads both the callee
|
|
37
|
+
* and a namespace object through their type wrappers:
|
|
38
|
+
* `(f as (u: unknown) => boolean)(x)` and `(m as typeof m).f(x)` still call
|
|
39
|
+
* `f`.
|
|
40
|
+
*/
|
|
41
|
+
export declare const buildCalleeResolver: (declarations: DeepReadonly<TSESTree.ImportDeclaration[]>) => ((callee: TSESTree.Expression) => ResolvedCallee | undefined);
|
|
42
|
+
/**
|
|
43
|
+
* A fix that inserts `import { <specifiers> } from '<moduleName>';` at the top
|
|
44
|
+
* of the file, or no fix when `specifiers` is empty. Each specifier is written
|
|
45
|
+
* as given, so `type T` imports a type.
|
|
46
|
+
*
|
|
47
|
+
* The placement is deliberately naive: an import sorter
|
|
48
|
+
* (prettier-plugin-organize-imports or any equivalent) merges and orders it
|
|
49
|
+
* afterwards. Put every name one report needs into a single call: ESLint
|
|
50
|
+
* merges the fixes of one report into one edit, and two fixes inserting at the
|
|
51
|
+
* start of the file conflict, so that one of them is dropped.
|
|
52
|
+
*/
|
|
53
|
+
export declare const buildNamedImportFix: (fixer: TSESLint.RuleFixer, program: TSESTree.Program, moduleName: string, specifiers: readonly string[]) => readonly TSESLint.RuleFix[];
|
|
54
|
+
//# sourceMappingURL=imports.d.mts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"imports.d.mts","sourceRoot":"","sources":["../src/imports.mts"],"names":[],"mappings":"AAAA,OAAO,EAEL,KAAK,QAAQ,EACb,KAAK,QAAQ,EACd,MAAM,0BAA0B,CAAC;AAClC,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,eAAe,CAAC;AAKlD;;;;GAIG;AACH,eAAO,MAAM,qBAAqB,YACvB,QAAQ,CAAC,OAAO,cACb,MAAM,KACjB,SAAS,QAAQ,CAAC,iBAAiB,EAKnC,CAAC;AAEJ;;;GAGG;AACH,eAAO,MAAM,eAAe,iBACZ,YAAY,CAAC,QAAQ,CAAC,iBAAiB,EAAE,CAAC,KACvD,SAAS,MAAM,EAOf,CAAC;AAEJ;;;;GAIG;AACH,eAAO,MAAM,oBAAoB,iBACjB,YAAY,CAAC,QAAQ,CAAC,iBAAiB,EAAE,CAAC,gBAC1C,MAAM,KACnB,MAAM,GAAG,SAOK,CAAC;AAElB,uEAAuE;AACvE,MAAM,MAAM,cAAc,GAAG,QAAQ,CAAC;IACpC,+DAA+D;IAC/D,aAAa,EAAE,MAAM,CAAC;IAEtB;;;OAGG;IACH,YAAY,EAAE,QAAQ,CAAC,UAAU,CAAC;IAElC,qEAAqE;IACrE,WAAW,EAAE,OAAO,CAAC;CACtB,CAAC,CAAC;AAEH;;;;;;;;GAQG;AACH,eAAO,MAAM,mBAAmB,iBAChB,YAAY,CAAC,QAAQ,CAAC,iBAAiB,EAAE,CAAC,KACvD,CAAC,CAAC,MAAM,EAAE,QAAQ,CAAC,UAAU,KAAK,cAAc,GAAG,SAAS,CAuD9D,CAAC;AAEF;;;;;;;;;;GAUG;AACH,eAAO,MAAM,mBAAmB,UACvB,QAAQ,CAAC,SAAS,WAChB,QAAQ,CAAC,OAAO,cACb,MAAM,cACN,SAAS,MAAM,EAAE,KAC5B,SAAS,QAAQ,CAAC,OAAO,EAQX,CAAC"}
|
package/dist/imports.mjs
ADDED
|
@@ -0,0 +1,90 @@
|
|
|
1
|
+
import { AST_NODE_TYPES, } from '@typescript-eslint/utils';
|
|
2
|
+
import { isTypeWrapper, skipTypeWrappers } from './type-wrappers.mjs';
|
|
3
|
+
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
export const getImportDeclarations = (program, moduleName) => program.body.filter((node) => node.type === AST_NODE_TYPES.ImportDeclaration &&
|
|
10
|
+
node.source.value === moduleName);
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
export const getNamedImports = (declarations) => declarations.flatMap((declaration) => declaration.specifiers.flatMap((specifier) => specifier.type === AST_NODE_TYPES.ImportSpecifier
|
|
16
|
+
? [importedNameOf(specifier)]
|
|
17
|
+
: []));
|
|
18
|
+
|
|
19
|
+
|
|
20
|
+
|
|
21
|
+
|
|
22
|
+
|
|
23
|
+
export const getImportedLocalName = (declarations, importedName) => declarations
|
|
24
|
+
.flatMap((declaration) => declaration.specifiers)
|
|
25
|
+
.find((specifier) => specifier.type === AST_NODE_TYPES.ImportSpecifier &&
|
|
26
|
+
importedNameOf(specifier) === importedName)?.local.name;
|
|
27
|
+
|
|
28
|
+
|
|
29
|
+
|
|
30
|
+
|
|
31
|
+
|
|
32
|
+
|
|
33
|
+
|
|
34
|
+
|
|
35
|
+
|
|
36
|
+
export const buildCalleeResolver = (declarations) => {
|
|
37
|
+
const specifiers = declarations.flatMap((declaration) => declaration.specifiers);
|
|
38
|
+
const namespaceNames = new Set(specifiers.flatMap((specifier) => specifier.type === AST_NODE_TYPES.ImportNamespaceSpecifier
|
|
39
|
+
? [specifier.local.name]
|
|
40
|
+
: []));
|
|
41
|
+
const localToCanonical = new Map(specifiers.flatMap((specifier) => specifier.type === AST_NODE_TYPES.ImportSpecifier
|
|
42
|
+
? [[specifier.local.name, importedNameOf(specifier)]]
|
|
43
|
+
: []));
|
|
44
|
+
const resolve = (callee) => {
|
|
45
|
+
if (isTypeWrapper(callee)) {
|
|
46
|
+
return resolve(callee.expression);
|
|
47
|
+
}
|
|
48
|
+
if (callee.type === AST_NODE_TYPES.Identifier) {
|
|
49
|
+
const canonicalName = localToCanonical.get(callee.name);
|
|
50
|
+
return canonicalName === undefined
|
|
51
|
+
? undefined
|
|
52
|
+
: { canonicalName, propertyNode: callee, isNamespace: false };
|
|
53
|
+
}
|
|
54
|
+
if (callee.type !== AST_NODE_TYPES.MemberExpression ||
|
|
55
|
+
callee.computed ||
|
|
56
|
+
callee.property.type !== AST_NODE_TYPES.Identifier) {
|
|
57
|
+
return undefined;
|
|
58
|
+
}
|
|
59
|
+
const object = skipTypeWrappers(callee.object);
|
|
60
|
+
return object.type === AST_NODE_TYPES.Identifier &&
|
|
61
|
+
namespaceNames.has(object.name)
|
|
62
|
+
? {
|
|
63
|
+
canonicalName: callee.property.name,
|
|
64
|
+
propertyNode: callee.property,
|
|
65
|
+
isNamespace: true,
|
|
66
|
+
}
|
|
67
|
+
: undefined;
|
|
68
|
+
};
|
|
69
|
+
return resolve;
|
|
70
|
+
};
|
|
71
|
+
|
|
72
|
+
|
|
73
|
+
|
|
74
|
+
|
|
75
|
+
|
|
76
|
+
|
|
77
|
+
|
|
78
|
+
|
|
79
|
+
|
|
80
|
+
|
|
81
|
+
|
|
82
|
+
export const buildNamedImportFix = (fixer, program, moduleName, specifiers) => specifiers.length === 0
|
|
83
|
+
? []
|
|
84
|
+
: [
|
|
85
|
+
fixer.insertTextBefore(program, `import { ${specifiers.join(', ')} } from '${moduleName}';\n`),
|
|
86
|
+
];
|
|
87
|
+
const importedNameOf = (specifier) => specifier.imported.type === AST_NODE_TYPES.Identifier
|
|
88
|
+
? specifier.imported.name
|
|
89
|
+
: specifier.imported.value;
|
|
90
|
+
//# sourceMappingURL=imports.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"imports.mjs","sourceRoot":"","sources":["../src/imports.mts"],"names":[],"mappings":"AAAA,OAAO,EACL,cAAc,GAGf,MAAM,0BAA0B,CAAC;AAElC,OAAO,EAAE,aAAa,EAAE,gBAAgB,EAAE,MAAM,qBAAqB,CAAC;AAEtE,uEAAuE;AAEvE;;;;GAIG;AACH,MAAM,CAAC,MAAM,qBAAqB,GAAG,CACnC,OAAyB,EACzB,UAAkB,EACqB,EAAE,CACzC,OAAO,CAAC,IAAI,CAAC,MAAM,CACjB,CAAC,IAAI,EAAsC,EAAE,CAC3C,IAAI,CAAC,IAAI,KAAK,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AClB;YACH,CAAC,CAAC,SAAS,CAAC;IAChB,CAAC,CAAC;IAEF,OAAO,OAAO,CAAC;AACjB,CAAC,CAAC;AAEF;;;;;;;;;;GAUG;AACH,MAAM,CAAC,MAAM,mBAAmB,GAAG,CACjC,KAAyB,EACzB,OAAyB,EACzB,UAAkB,EAClB,UAA6B,EACA,EAAE,CAC/B,UAAU,CAAC,MAAM,KAAK,CAAC;IACrB,CAAC,CAAE,EAAY;IACf,CAAC,CAAE;QACC,KAAK,CAAC,gBAAgB,CACpB,OAAO,EACP,YAAY,UAAU,CAAC,IAAI,CAAC,IAAI,CAAC,YAAY,UAAU,MAAM,CAC9D;KACQ,CAAC;AAElB,MAAM,cAAc,GAAG,CACrB,SAAiD,EACzC,EAAE,CACV,SAAS,CAAC,QAAQ,CAAC,IAAI,KAAK,cAAc,CAAC,UAAU;IACnD,CAAC,CAAC,SAAS,CAAC,QAAQ,CAAC,IAAI;IACzB,CAAC,CAAC,SAAS,CAAC,QAAQ,CAAC,KAAK,CAAC"}
|
package/dist/index.d.mts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.mts","sourceRoot":"","sources":["../src/index.mts"],"names":[],"mappings":"AAAA,cAAc,gBAAgB,CAAC;AAC/B,cAAc,eAAe,CAAC;AAC9B,cAAc,mBAAmB,CAAC;AAClC,cAAc,qBAAqB,CAAC"}
|
package/dist/index.mjs
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.mjs","sourceRoot":"","sources":["../src/index.mts"],"names":[],"mappings":"AAAA,cAAc,gBAAgB,CAAC;AAC/B,cAAc,eAAe,CAAC;AAC9B,cAAc,mBAAmB,CAAC;AAClC,cAAc,qBAAqB,CAAC"}
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import { type TSESLint, type TSESTree } from '@typescript-eslint/utils';
|
|
2
|
+
import type { DeepReadonly } from 'ts-type-forge';
|
|
3
|
+
/**
|
|
4
|
+
* How many pairs of parentheses the source puts around `node`: `0` for `a`, `2`
|
|
5
|
+
* for `((a))`. The parentheses of a call's argument list are not counted.
|
|
6
|
+
*/
|
|
7
|
+
export declare const parenthesesDepth: (sourceCode: TSESLint.SourceCode, node: DeepReadonly<TSESTree.Node>) => number;
|
|
8
|
+
/**
|
|
9
|
+
* The range of `node` together with the parentheses the source puts around it,
|
|
10
|
+
* which `node.range` leaves out: all of `((Array.isArray))` for the member
|
|
11
|
+
* expression inside.
|
|
12
|
+
*/
|
|
13
|
+
export declare const getRangeWithParens: (sourceCode: TSESLint.SourceCode, node: DeepReadonly<TSESTree.Node>) => TSESTree.Range;
|
|
14
|
+
/**
|
|
15
|
+
* The source text of `node` to put in a call's argument list. A node's text
|
|
16
|
+
* never includes the parentheses the source wrote around it, so a sequence
|
|
17
|
+
* expression gets them back here, whether or not the source had them:
|
|
18
|
+
* `(setup(), x)` would otherwise become two arguments.
|
|
19
|
+
*/
|
|
20
|
+
export declare const getArgumentText: (sourceCode: TSESLint.SourceCode, node: DeepReadonly<TSESTree.Node>) => string;
|
|
21
|
+
//# sourceMappingURL=parentheses.d.mts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"parentheses.d.mts","sourceRoot":"","sources":["../src/parentheses.mts"],"names":[],"mappings":"AAAA,OAAO,EAGL,KAAK,QAAQ,EACb,KAAK,QAAQ,EACd,MAAM,0BAA0B,CAAC;AAClC,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,eAAe,CAAC;AAIlD;;;GAGG;AACH,eAAO,MAAM,gBAAgB,eACf,QAAQ,CAAC,UAAU,QACzB,YAAY,CAAC,QAAQ,CAAC,IAAI,CAAC,KAChC,MASF,CAAC;AAEF;;;;GAIG;AACH,eAAO,MAAM,kBAAkB,eACjB,QAAQ,CAAC,UAAU,QACzB,YAAY,CAAC,QAAQ,CAAC,IAAI,CAAC,KAChC,QAAQ,CAAC,KAcX,CAAC;AAEF;;;;;GAKG;AACH,eAAO,MAAM,eAAe,eACd,QAAQ,CAAC,UAAU,QACzB,YAAY,CAAC,QAAQ,CAAC,IAAI,CAAC,KAChC,MAIF,CAAC"}
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
import { AST_NODE_TYPES, ASTUtils, } from '@typescript-eslint/utils';
|
|
2
|
+
|
|
3
|
+
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
|
|
7
|
+
export const parenthesesDepth = (sourceCode, node) => {
|
|
8
|
+
const mutableNode = asNode(node);
|
|
9
|
+
const depthFrom = (known) => ASTUtils.isParenthesized(known + 1, mutableNode, sourceCode)
|
|
10
|
+
? depthFrom(known + 1)
|
|
11
|
+
: known;
|
|
12
|
+
return depthFrom(0);
|
|
13
|
+
};
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
|
|
17
|
+
|
|
18
|
+
|
|
19
|
+
export const getRangeWithParens = (sourceCode, node) => {
|
|
20
|
+
const depth = parenthesesDepth(sourceCode, node);
|
|
21
|
+
if (depth === 0) {
|
|
22
|
+
return [node.range[0], node.range[1]];
|
|
23
|
+
}
|
|
24
|
+
const opening = sourceCode.getTokenBefore(asNode(node), { skip: depth - 1 });
|
|
25
|
+
const closing = sourceCode.getTokenAfter(asNode(node), { skip: depth - 1 });
|
|
26
|
+
return opening === null || closing === null
|
|
27
|
+
? [node.range[0], node.range[1]]
|
|
28
|
+
: [opening.range[0], closing.range[1]];
|
|
29
|
+
};
|
|
30
|
+
|
|
31
|
+
|
|
32
|
+
|
|
33
|
+
|
|
34
|
+
|
|
35
|
+
|
|
36
|
+
export const getArgumentText = (sourceCode, node) => {
|
|
37
|
+
const text = sourceCode.text.slice(node.range[0], node.range[1]);
|
|
38
|
+
return node.type === AST_NODE_TYPES.SequenceExpression ? `(${text})` : text;
|
|
39
|
+
};
|
|
40
|
+
|
|
41
|
+
|
|
42
|
+
|
|
43
|
+
|
|
44
|
+
const asNode = (node) =>
|
|
45
|
+
|
|
46
|
+
node;
|
|
47
|
+
//# sourceMappingURL=parentheses.mjs.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OAAO,KAAK,IAAI;QACzC,CAAC,CAAC,CAAC,IAAI,CAAC,KAAK,CAAC,CAAC,CAAC,EAAE,IAAI,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC;QAChC,CAAC,CAAC,CAAC,OAAO,CAAC,KAAK,CAAC,CAAC,CAAC,EAAE,OAAO,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC,CAAC;AAC3C,CAAC,CAAC;AAEF;;;;;GAKG;AACH,MAAM,CAAC,MAAM,eAAe,GAAG,CAC7B,UAA+B,EAC/B,IAAiC,EACzB,EAAE;IACV,MAAM,IAAI,GAAG,UAAU,CAAC,IAAI,CAAC,KAAK,CAAC,IAAI,CAAC,KAAK,CAAC,CAAC,CAAC,EAAE,IAAI,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC,CAAC;IAEjE,OAAO,IAAI,CAAC,IAAI,KAAK,cAAc,CAAC,kBAAkB,CAAC,CAAC,CAAC,IAAI,IAAI,GAAG,CAAC,CAAC,CAAC,IAAI,CAAC;AAC9E,CAAC,CAAC;AAEF;;;GAGG;AACH,MAAM,MAAM,GAAG,CAAC,IAAiC,EAAiB,EAAE;AAClE,oEAAoE;AACpE,IAAqB,CAAC"}
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
import { type TSESTree } from '@typescript-eslint/utils';
|
|
2
|
+
import type { DeepReadonly, RelaxedExtract } from 'ts-type-forge';
|
|
3
|
+
/**
|
|
4
|
+
* The four expressions that change a value's type and nothing else: `x as T`,
|
|
5
|
+
* `x!`, `x satisfies T` and `<T>x`.
|
|
6
|
+
*/
|
|
7
|
+
export type TypeWrapper = TSESTree.TSAsExpression | TSESTree.TSNonNullExpression | TSESTree.TSSatisfiesExpression | TSESTree.TSTypeAssertion;
|
|
8
|
+
/**
|
|
9
|
+
* The expression a type wrapper among `N` wraps, as mutable or as readonly as
|
|
10
|
+
* `N` itself: `TSESTree.Expression` for a mutable node,
|
|
11
|
+
* `DeepReadonly<TSESTree.Expression>` for a readonly one, and `never` when no
|
|
12
|
+
* member of `N` is a type wrapper.
|
|
13
|
+
*/
|
|
14
|
+
export type WrappedExpression<N> = RelaxedExtract<N, DeepReadonly<TypeWrapper> | TypeWrapper>['expression'];
|
|
15
|
+
/**
|
|
16
|
+
* Whether `node` is a type wrapper (see {@link TypeWrapper}). Takes a readonly
|
|
17
|
+
* or a mutable node and narrows it without changing which.
|
|
18
|
+
*/
|
|
19
|
+
export declare const isTypeWrapper: (node: DeepReadonly<TSESTree.Node>) => node is DeepReadonly<TypeWrapper> | TypeWrapper;
|
|
20
|
+
/**
|
|
21
|
+
* `node` with every `as`, `satisfies`, `!` and `<T>` around it taken off: the
|
|
22
|
+
* expression whose value it is. A wrapper changes no value, so a rule judges
|
|
23
|
+
* syntax on what this returns — `xs.length > (0 as number)` compares with the
|
|
24
|
+
* literal `0`, and `(Number satisfies NumberConstructor)('1')` calls `Number` —
|
|
25
|
+
* and writes its fix from the original node, so that the wrapper is kept. A
|
|
26
|
+
* type is another matter: see {@link typeWrapperLayers}.
|
|
27
|
+
*/
|
|
28
|
+
export declare const skipTypeWrappers: <N extends DeepReadonly<TSESTree.Node>>(node: N) => N | WrappedExpression<N>;
|
|
29
|
+
/**
|
|
30
|
+
* `node` and every expression inside the type wrappers around it, outermost
|
|
31
|
+
* first: `x as A as B` gives `x as A as B`, `x as A` and `x`, the last of which
|
|
32
|
+
* is `skipTypeWrappers(node)`.
|
|
33
|
+
*
|
|
34
|
+
* A conclusion a rule draws from a type — that a guard always holds, that a
|
|
35
|
+
* value is an array, that an operation may throw — has to hold for the type of
|
|
36
|
+
* every layer. An assertion therefore only ever makes a rule more cautious:
|
|
37
|
+
* one that widens (`x as string | null`) is the author saying the value may be
|
|
38
|
+
* `null`, and is respected; one that narrows (`x!`, `v as string` on an
|
|
39
|
+
* `unknown`) is a claim the checker cannot verify, and the value's own type
|
|
40
|
+
* still decides. The fix, which keeps the wrapper, has to type-check at every
|
|
41
|
+
* layer too.
|
|
42
|
+
*/
|
|
43
|
+
export declare const typeWrapperLayers: <N extends DeepReadonly<TSESTree.Node>>(node: N) => readonly (N | WrappedExpression<N>)[];
|
|
44
|
+
//# sourceMappingURL=type-wrappers.d.mts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"type-wrappers.d.mts","sourceRoot":"","sources":["../src/type-wrappers.mts"],"names":[],"mappings":"AAAA,OAAO,EAAkB,KAAK,QAAQ,EAAE,MAAM,0BAA0B,CAAC;AACzE,OAAO,KAAK,EAAE,YAAY,EAAE,cAAc,EAAE,MAAM,eAAe,CAAC;AAElE;;;GAGG;AACH,MAAM,MAAM,WAAW,GACnB,QAAQ,CAAC,cAAc,GACvB,QAAQ,CAAC,mBAAmB,GAC5B,QAAQ,CAAC,qBAAqB,GAC9B,QAAQ,CAAC,eAAe,CAAC;AAE7B;;;;;GAKG;AACH,MAAM,MAAM,iBAAiB,CAAC,CAAC,IAAI,cAAc,CAC/C,CAAC,EACD,YAAY,CAAC,WAAW,CAAC,GAAG,WAAW,CACxC,CAAC,YAAY,CAAC,CAAC;AAEhB;;;GAGG;AACH,eAAO,MAAM,aAAa,SAClB,YAAY,CAAC,QAAQ,CAAC,IAAI,CAAC,KAChC,IAAI,IAAI,YAAY,CAAC,WAAW,CAAC,GAAG,WACT,CAAC;AAE/B;;;;;;;GAOG;AACH,eAAO,MAAM,gBAAgB,GAAI,CAAC,SAAS,YAAY,CAAC,QAAQ,CAAC,IAAI,CAAC,QAC9D,CAAC,KACN,CAAC,GAAG,iBAAiB,CAAC,CAAC,CACsC,CAAC;AAEjE;;;;;;;;;;;;;GAaG;AACH,eAAO,MAAM,iBAAiB,GAAI,CAAC,SAAS,YAAY,CAAC,QAAQ,CAAC,IAAI,CAAC,QAC/D,CAAC,KACN,SAAS,CAAC,CAAC,GAAG,iBAAiB,CAAC,CAAC,CAAC,CAAC,EAGf,CAAC"}
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
import { AST_NODE_TYPES } from '@typescript-eslint/utils';
|
|
2
|
+
|
|
3
|
+
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
export const isTypeWrapper = (node) => TYPE_WRAPPERS.has(node.type);
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
export const skipTypeWrappers = (node) => isTypeWrapper(node) ? skipTypeWrappers(node.expression) : node;
|
|
16
|
+
|
|
17
|
+
|
|
18
|
+
|
|
19
|
+
|
|
20
|
+
|
|
21
|
+
|
|
22
|
+
|
|
23
|
+
|
|
24
|
+
|
|
25
|
+
|
|
26
|
+
|
|
27
|
+
|
|
28
|
+
|
|
29
|
+
|
|
30
|
+
export const typeWrapperLayers = (node) => isTypeWrapper(node)
|
|
31
|
+
? [node, ...typeWrapperLayers(node.expression)]
|
|
32
|
+
: [node];
|
|
33
|
+
const TYPE_WRAPPERS = new Set([
|
|
34
|
+
AST_NODE_TYPES.TSAsExpression,
|
|
35
|
+
AST_NODE_TYPES.TSNonNullExpression,
|
|
36
|
+
AST_NODE_TYPES.TSSatisfiesExpression,
|
|
37
|
+
AST_NODE_TYPES.TSTypeAssertion,
|
|
38
|
+
]);
|
|
39
|
+
//# sourceMappingURL=type-wrappers.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"type-wrappers.mjs","sourceRoot":"","sources":["../src/type-wrappers.mts"],"names":[],"mappings":"AAAA,OAAO,EAAE,cAAc,EAAiB,MAAM,0BAA0B,CAAC;AAwBzE;;;GAGG;AACH,MAAM,CAAC,MAAM,aAAa,GAAG,CAC3B,IAAiC,EACgB,EAAE,CACnD,aAAa,CAAC,GAAG,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC;AAE/B;;;;;;;GAOG;AACH,MAAM,CAAC,MAAM,gBAAgB,GAAG,CAC9B,IAAO,EACmB,EAAE,CAC5B,aAAa,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC,gBAAgB,CAAC,IAAI,CAAC,UAAU,CAAC,CAAC,CAAC,CAAC,IAAI,CAAC;AAEjE;;;;;;;;;;;;;GAaG;AACH,MAAM,CAAC,MAAM,iBAAiB,GAAG,CAC/B,IAAO,EACgC,EAAE,CACzC,aAAa,CAAC,IAAI,CAAC;IACjB,CAAC,CAAE,CAAC,IAAI,EAAE,GAAG,iBAAiB,CAAC,IAAI,CAAC,UAAU,CAAC,CAAW;IAC1D,CAAC,CAAE,CAAC,IAAI,CAAW,CAAC;AAExB,MAAM,aAAa,GAAgC,IAAI,GAAG,CAAC;IACzD,cAAc,CAAC,cAAc;IAC7B,cAAc,CAAC,mBAAmB;IAClC,cAAc,CAAC,qBAAqB;IACpC,cAAc,CAAC,eAAe;CAC/B,CAAC,CAAC"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,88 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "eslint-rule-forge",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"
|
|
5
|
-
"description": "
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"private": false,
|
|
5
|
+
"description": "Helpers for writing typescript-eslint rules that see through type wrappers and parentheses, and resolve what a file imports.",
|
|
6
|
+
"keywords": [
|
|
7
|
+
"eslint",
|
|
8
|
+
"typescript-eslint",
|
|
9
|
+
"eslint-rule",
|
|
10
|
+
"ast",
|
|
11
|
+
"typescript"
|
|
12
|
+
],
|
|
13
|
+
"repository": {
|
|
14
|
+
"type": "git",
|
|
15
|
+
"url": "git+https://github.com/noshiro-pf/mono.git",
|
|
16
|
+
"directory": "libs/eslint-rule-forge"
|
|
17
|
+
},
|
|
18
|
+
"license": "Apache-2.0",
|
|
19
|
+
"author": "noshiro-pf <noshiro.pf@gmail.com>",
|
|
20
|
+
"sideEffects": false,
|
|
21
|
+
"type": "module",
|
|
22
|
+
"exports": {
|
|
23
|
+
".": {
|
|
24
|
+
"types": "./dist/index.d.mts",
|
|
25
|
+
"import": "./dist/index.mjs"
|
|
26
|
+
}
|
|
27
|
+
},
|
|
28
|
+
"module": "./dist/index.mjs",
|
|
29
|
+
"types": "./dist/index.d.mts",
|
|
30
|
+
"files": [
|
|
31
|
+
"src",
|
|
32
|
+
"dist",
|
|
33
|
+
"README.md",
|
|
34
|
+
"LICENSE"
|
|
35
|
+
],
|
|
36
|
+
"dependencies": {
|
|
37
|
+
"@typescript-eslint/utils": "^8.71.1",
|
|
38
|
+
"ts-type-forge": "~9.2.2"
|
|
39
|
+
},
|
|
40
|
+
"devDependencies": {
|
|
41
|
+
"@typescript-eslint/rule-tester": "8.71.1",
|
|
42
|
+
"dedent": "1.7.2",
|
|
43
|
+
"eslint": "10.12.0",
|
|
44
|
+
"eslint-config-typed": "5.17.4",
|
|
45
|
+
"eslint-plugin-ts-fortress": "1.3.3",
|
|
46
|
+
"eslint-plugin-ts-type-forge": "0.8.8",
|
|
47
|
+
"prettier": "3.9.9",
|
|
48
|
+
"prettier-plugin-organize-imports": "4.3.0",
|
|
49
|
+
"prettier-plugin-packagejson": "3.0.2",
|
|
50
|
+
"ts-data-forge": "14.7.1",
|
|
51
|
+
"ts-repo-utils": "10.7.1"
|
|
52
|
+
},
|
|
53
|
+
"peerDependencies": {
|
|
54
|
+
"eslint": ">=9.0.0",
|
|
55
|
+
"typescript": ">=5.0.0 <7.0.0"
|
|
56
|
+
},
|
|
57
|
+
"engines": {
|
|
58
|
+
"node": ">=22.22.2",
|
|
59
|
+
"pnpm": ">=9.0.0"
|
|
60
|
+
},
|
|
61
|
+
"publishConfig": {
|
|
62
|
+
"access": "public",
|
|
63
|
+
"provenance": true
|
|
64
|
+
},
|
|
65
|
+
"scripts": {
|
|
66
|
+
"build": "tsx --tsconfig ../../tools/configs/tsconfig.tsx.json ./scripts/cmd/build.mts",
|
|
67
|
+
"check:ext": "tsx --tsconfig ../../tools/configs/tsconfig.tsx.json ./scripts/cmd/check-ext.mts",
|
|
68
|
+
"check:lint": "oxlint . && eslint .",
|
|
69
|
+
"check:test": "pnpm run z:vitest run",
|
|
70
|
+
"check:test:cov": "pnpm run z:vitest run --coverage",
|
|
71
|
+
"check:types": "node ../../node_modules/typescript-native/bin/tsc --noEmit",
|
|
72
|
+
"doc": "pnpm run gen:readme",
|
|
73
|
+
"fix:fmt": "tsx --tsconfig ../../tools/configs/tsconfig.tsx.json ../../libs/ts-repo-utils/src/cmd/format-uncommitted.mts --cwd .",
|
|
74
|
+
"fix:fmt:diff": "tsx --tsconfig ../../tools/configs/tsconfig.tsx.json ../../libs/ts-repo-utils/src/cmd/format-diff-from.mts origin/main --cwd .",
|
|
75
|
+
"fix:fmt:full": "pnpm run z:prettier .",
|
|
76
|
+
"fix:lint": "oxlint . --fix && eslint . --fix",
|
|
77
|
+
"fmt": "pnpm run fix:fmt",
|
|
78
|
+
"gen": "run-s gen:index gen:readme",
|
|
79
|
+
"gen:index": "run-s gen:index:src fix:fmt",
|
|
80
|
+
"gen:index:src": "tsx --tsconfig ../../tools/configs/tsconfig.tsx.json ../../libs/ts-repo-utils/src/cmd/gen-index-ts.mts ./src --index-ext .mts --export-ext .mjs --target-ext .mts",
|
|
81
|
+
"gen:readme": "tsx --tsconfig ../../tools/configs/tsconfig.tsx.json ./scripts/cmd/embed-examples.mts",
|
|
82
|
+
"test": "pnpm run check:test",
|
|
83
|
+
"watch:test": "pnpm run z:vitest",
|
|
84
|
+
"watch:types": "node ../../node_modules/typescript-native/bin/tsc --noEmit --watch -p ./tsconfig.json",
|
|
85
|
+
"z:prettier": "prettier --config ../../.prettierrc --ignore-path ../../.prettierignore --ignore-path ../../.gitignore --ignore-unknown --no-error-on-unmatched-pattern --write",
|
|
86
|
+
"z:vitest": "vitest --config ./configs/vitest.config.mts"
|
|
87
|
+
}
|
|
6
88
|
}
|
package/src/bindings.mts
ADDED
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
import {
|
|
2
|
+
AST_NODE_TYPES,
|
|
3
|
+
ASTUtils,
|
|
4
|
+
type TSESLint,
|
|
5
|
+
type TSESTree,
|
|
6
|
+
} from '@typescript-eslint/utils';
|
|
7
|
+
|
|
8
|
+
/* eslint-disable @typescript-eslint/prefer-readonly-parameter-types */
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* Whether `name`, read at `node`, resolves to something this file writes down
|
|
12
|
+
* — a declaration, a parameter, an import — rather than to a global or a
|
|
13
|
+
* standard-library type of the same name.
|
|
14
|
+
*
|
|
15
|
+
* The scope manager makes an implicit variable for every reference it cannot
|
|
16
|
+
* resolve, so `findVariable` finds `Exclude` in a file that never declares it;
|
|
17
|
+
* only a variable with a definition was written in the file.
|
|
18
|
+
*/
|
|
19
|
+
export const isLocallyBound = (
|
|
20
|
+
sourceCode: TSESLint.SourceCode,
|
|
21
|
+
node: TSESTree.Node,
|
|
22
|
+
name: string,
|
|
23
|
+
): boolean => {
|
|
24
|
+
const variable = ASTUtils.findVariable(sourceCode.getScope(node), name);
|
|
25
|
+
|
|
26
|
+
return variable !== null && variable.defs.length > 0;
|
|
27
|
+
};
|
|
28
|
+
|
|
29
|
+
/**
|
|
30
|
+
* Whether `name`, read at `node`, resolves to a binding that is not an import
|
|
31
|
+
* from `moduleName` — a declaration, a parameter, or an import from another
|
|
32
|
+
* module. A fix that makes code at `node` refer to `moduleName`'s export
|
|
33
|
+
* `name` has to give up then, because the name would refer to that binding
|
|
34
|
+
* instead.
|
|
35
|
+
*
|
|
36
|
+
* The innermost binding decides: an import from `moduleName` at the top of the
|
|
37
|
+
* file does not help a name a function shadows around `node`.
|
|
38
|
+
*/
|
|
39
|
+
export const hasForeignBinding = (
|
|
40
|
+
sourceCode: TSESLint.SourceCode,
|
|
41
|
+
node: TSESTree.Node,
|
|
42
|
+
name: string,
|
|
43
|
+
moduleName: string,
|
|
44
|
+
): boolean => {
|
|
45
|
+
const variable = ASTUtils.findVariable(sourceCode.getScope(node), name);
|
|
46
|
+
|
|
47
|
+
return variable?.defs.some((def) => !isImportFrom(def, moduleName)) ?? false;
|
|
48
|
+
};
|
|
49
|
+
|
|
50
|
+
/**
|
|
51
|
+
* Whether `def` is a binding an `import … from '<moduleName>'` declaration
|
|
52
|
+
* introduces.
|
|
53
|
+
*/
|
|
54
|
+
const isImportFrom = (
|
|
55
|
+
def: TSESLint.Scope.Definition,
|
|
56
|
+
moduleName: string,
|
|
57
|
+
): boolean =>
|
|
58
|
+
(def.node.type === AST_NODE_TYPES.ImportSpecifier ||
|
|
59
|
+
def.node.type === AST_NODE_TYPES.ImportDefaultSpecifier ||
|
|
60
|
+
def.node.type === AST_NODE_TYPES.ImportNamespaceSpecifier) &&
|
|
61
|
+
def.node.parent.type === AST_NODE_TYPES.ImportDeclaration &&
|
|
62
|
+
def.node.parent.source.value === moduleName;
|
package/src/imports.mts
ADDED
|
@@ -0,0 +1,172 @@
|
|
|
1
|
+
import {
|
|
2
|
+
AST_NODE_TYPES,
|
|
3
|
+
type TSESLint,
|
|
4
|
+
type TSESTree,
|
|
5
|
+
} from '@typescript-eslint/utils';
|
|
6
|
+
import type { DeepReadonly } from 'ts-type-forge';
|
|
7
|
+
import { isTypeWrapper, skipTypeWrappers } from './type-wrappers.mjs';
|
|
8
|
+
|
|
9
|
+
/* eslint-disable @typescript-eslint/prefer-readonly-parameter-types */
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* Every `import … from '<moduleName>'` declaration in the file, not only the
|
|
13
|
+
* first: `import type { A } from 'm'` and `import { b } from 'm'` are two
|
|
14
|
+
* declarations, and a name imported by either is in scope.
|
|
15
|
+
*/
|
|
16
|
+
export const getImportDeclarations = (
|
|
17
|
+
program: TSESTree.Program,
|
|
18
|
+
moduleName: string,
|
|
19
|
+
): readonly TSESTree.ImportDeclaration[] =>
|
|
20
|
+
program.body.filter(
|
|
21
|
+
(node): node is TSESTree.ImportDeclaration =>
|
|
22
|
+
node.type === AST_NODE_TYPES.ImportDeclaration &&
|
|
23
|
+
node.source.value === moduleName,
|
|
24
|
+
);
|
|
25
|
+
|
|
26
|
+
/**
|
|
27
|
+
* The exported names `declarations` import by name, whatever they are bound to
|
|
28
|
+
* locally: `import { a, b as c }` gives `a` and `b`.
|
|
29
|
+
*/
|
|
30
|
+
export const getNamedImports = (
|
|
31
|
+
declarations: DeepReadonly<TSESTree.ImportDeclaration[]>,
|
|
32
|
+
): readonly string[] =>
|
|
33
|
+
declarations.flatMap((declaration) =>
|
|
34
|
+
declaration.specifiers.flatMap((specifier) =>
|
|
35
|
+
specifier.type === AST_NODE_TYPES.ImportSpecifier
|
|
36
|
+
? [importedNameOf(specifier)]
|
|
37
|
+
: [],
|
|
38
|
+
),
|
|
39
|
+
);
|
|
40
|
+
|
|
41
|
+
/**
|
|
42
|
+
* The local name `declarations` bind the export `importedName` to, or
|
|
43
|
+
* `undefined` when they do not import it: `'y'` for `import { x as y }`. A fix
|
|
44
|
+
* writes this rather than the exported name, which may not be in scope.
|
|
45
|
+
*/
|
|
46
|
+
export const getImportedLocalName = (
|
|
47
|
+
declarations: DeepReadonly<TSESTree.ImportDeclaration[]>,
|
|
48
|
+
importedName: string,
|
|
49
|
+
): string | undefined =>
|
|
50
|
+
declarations
|
|
51
|
+
.flatMap((declaration) => declaration.specifiers)
|
|
52
|
+
.find(
|
|
53
|
+
(specifier) =>
|
|
54
|
+
specifier.type === AST_NODE_TYPES.ImportSpecifier &&
|
|
55
|
+
importedNameOf(specifier) === importedName,
|
|
56
|
+
)?.local.name;
|
|
57
|
+
|
|
58
|
+
/** What {@link buildCalleeResolver}'s resolver says a callee calls. */
|
|
59
|
+
export type ResolvedCallee = Readonly<{
|
|
60
|
+
/** The exported name, whatever the callee calls it locally. */
|
|
61
|
+
canonicalName: string;
|
|
62
|
+
|
|
63
|
+
/**
|
|
64
|
+
* The identifier to rewrite to change what is called: the callee itself for
|
|
65
|
+
* a named import, the property for a namespace import.
|
|
66
|
+
*/
|
|
67
|
+
propertyNode: TSESTree.Identifier;
|
|
68
|
+
|
|
69
|
+
/** Whether the call goes through a namespace import (`m.f(...)`). */
|
|
70
|
+
isNamespace: boolean;
|
|
71
|
+
}>;
|
|
72
|
+
|
|
73
|
+
/**
|
|
74
|
+
* Builds a function that says which export of the module `declarations`
|
|
75
|
+
* import a callee calls, or `undefined` when it calls none of them. It knows
|
|
76
|
+
* named imports, aliased ones (`import { f as g }` → `g(...)` calls `f`) and
|
|
77
|
+
* namespace imports (`import * as m` → `m.f(...)`), and reads both the callee
|
|
78
|
+
* and a namespace object through their type wrappers:
|
|
79
|
+
* `(f as (u: unknown) => boolean)(x)` and `(m as typeof m).f(x)` still call
|
|
80
|
+
* `f`.
|
|
81
|
+
*/
|
|
82
|
+
export const buildCalleeResolver = (
|
|
83
|
+
declarations: DeepReadonly<TSESTree.ImportDeclaration[]>,
|
|
84
|
+
): ((callee: TSESTree.Expression) => ResolvedCallee | undefined) => {
|
|
85
|
+
const specifiers = declarations.flatMap(
|
|
86
|
+
(declaration) => declaration.specifiers,
|
|
87
|
+
);
|
|
88
|
+
|
|
89
|
+
const namespaceNames: ReadonlySet<string> = new Set(
|
|
90
|
+
specifiers.flatMap((specifier) =>
|
|
91
|
+
specifier.type === AST_NODE_TYPES.ImportNamespaceSpecifier
|
|
92
|
+
? [specifier.local.name]
|
|
93
|
+
: [],
|
|
94
|
+
),
|
|
95
|
+
);
|
|
96
|
+
|
|
97
|
+
const localToCanonical: ReadonlyMap<string, string> = new Map(
|
|
98
|
+
specifiers.flatMap((specifier) =>
|
|
99
|
+
specifier.type === AST_NODE_TYPES.ImportSpecifier
|
|
100
|
+
? [[specifier.local.name, importedNameOf(specifier)] as const]
|
|
101
|
+
: [],
|
|
102
|
+
),
|
|
103
|
+
);
|
|
104
|
+
|
|
105
|
+
const resolve = (callee: TSESTree.Expression): ResolvedCallee | undefined => {
|
|
106
|
+
if (isTypeWrapper(callee)) {
|
|
107
|
+
return resolve(callee.expression);
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
if (callee.type === AST_NODE_TYPES.Identifier) {
|
|
111
|
+
const canonicalName = localToCanonical.get(callee.name);
|
|
112
|
+
|
|
113
|
+
return canonicalName === undefined
|
|
114
|
+
? undefined
|
|
115
|
+
: { canonicalName, propertyNode: callee, isNamespace: false };
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
if (
|
|
119
|
+
callee.type !== AST_NODE_TYPES.MemberExpression ||
|
|
120
|
+
callee.computed ||
|
|
121
|
+
callee.property.type !== AST_NODE_TYPES.Identifier
|
|
122
|
+
) {
|
|
123
|
+
return undefined;
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
const object = skipTypeWrappers(callee.object);
|
|
127
|
+
|
|
128
|
+
return object.type === AST_NODE_TYPES.Identifier &&
|
|
129
|
+
namespaceNames.has(object.name)
|
|
130
|
+
? {
|
|
131
|
+
canonicalName: callee.property.name,
|
|
132
|
+
propertyNode: callee.property,
|
|
133
|
+
isNamespace: true,
|
|
134
|
+
}
|
|
135
|
+
: undefined;
|
|
136
|
+
};
|
|
137
|
+
|
|
138
|
+
return resolve;
|
|
139
|
+
};
|
|
140
|
+
|
|
141
|
+
/**
|
|
142
|
+
* A fix that inserts `import { <specifiers> } from '<moduleName>';` at the top
|
|
143
|
+
* of the file, or no fix when `specifiers` is empty. Each specifier is written
|
|
144
|
+
* as given, so `type T` imports a type.
|
|
145
|
+
*
|
|
146
|
+
* The placement is deliberately naive: an import sorter
|
|
147
|
+
* (prettier-plugin-organize-imports or any equivalent) merges and orders it
|
|
148
|
+
* afterwards. Put every name one report needs into a single call: ESLint
|
|
149
|
+
* merges the fixes of one report into one edit, and two fixes inserting at the
|
|
150
|
+
* start of the file conflict, so that one of them is dropped.
|
|
151
|
+
*/
|
|
152
|
+
export const buildNamedImportFix = (
|
|
153
|
+
fixer: TSESLint.RuleFixer,
|
|
154
|
+
program: TSESTree.Program,
|
|
155
|
+
moduleName: string,
|
|
156
|
+
specifiers: readonly string[],
|
|
157
|
+
): readonly TSESLint.RuleFix[] =>
|
|
158
|
+
specifiers.length === 0
|
|
159
|
+
? ([] as const)
|
|
160
|
+
: ([
|
|
161
|
+
fixer.insertTextBefore(
|
|
162
|
+
program,
|
|
163
|
+
`import { ${specifiers.join(', ')} } from '${moduleName}';\n`,
|
|
164
|
+
),
|
|
165
|
+
] as const);
|
|
166
|
+
|
|
167
|
+
const importedNameOf = (
|
|
168
|
+
specifier: DeepReadonly<TSESTree.ImportSpecifier>,
|
|
169
|
+
): string =>
|
|
170
|
+
specifier.imported.type === AST_NODE_TYPES.Identifier
|
|
171
|
+
? specifier.imported.name
|
|
172
|
+
: specifier.imported.value;
|
package/src/index.mts
ADDED
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
import {
|
|
2
|
+
AST_NODE_TYPES,
|
|
3
|
+
ASTUtils,
|
|
4
|
+
type TSESLint,
|
|
5
|
+
type TSESTree,
|
|
6
|
+
} from '@typescript-eslint/utils';
|
|
7
|
+
import type { DeepReadonly } from 'ts-type-forge';
|
|
8
|
+
|
|
9
|
+
/* eslint-disable @typescript-eslint/prefer-readonly-parameter-types */
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* How many pairs of parentheses the source puts around `node`: `0` for `a`, `2`
|
|
13
|
+
* for `((a))`. The parentheses of a call's argument list are not counted.
|
|
14
|
+
*/
|
|
15
|
+
export const parenthesesDepth = (
|
|
16
|
+
sourceCode: TSESLint.SourceCode,
|
|
17
|
+
node: DeepReadonly<TSESTree.Node>,
|
|
18
|
+
): number => {
|
|
19
|
+
const mutableNode = asNode(node);
|
|
20
|
+
|
|
21
|
+
const depthFrom = (known: number): number =>
|
|
22
|
+
ASTUtils.isParenthesized(known + 1, mutableNode, sourceCode)
|
|
23
|
+
? depthFrom(known + 1)
|
|
24
|
+
: known;
|
|
25
|
+
|
|
26
|
+
return depthFrom(0);
|
|
27
|
+
};
|
|
28
|
+
|
|
29
|
+
/**
|
|
30
|
+
* The range of `node` together with the parentheses the source puts around it,
|
|
31
|
+
* which `node.range` leaves out: all of `((Array.isArray))` for the member
|
|
32
|
+
* expression inside.
|
|
33
|
+
*/
|
|
34
|
+
export const getRangeWithParens = (
|
|
35
|
+
sourceCode: TSESLint.SourceCode,
|
|
36
|
+
node: DeepReadonly<TSESTree.Node>,
|
|
37
|
+
): TSESTree.Range => {
|
|
38
|
+
const depth = parenthesesDepth(sourceCode, node);
|
|
39
|
+
|
|
40
|
+
if (depth === 0) {
|
|
41
|
+
return [node.range[0], node.range[1]];
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
const opening = sourceCode.getTokenBefore(asNode(node), { skip: depth - 1 });
|
|
45
|
+
|
|
46
|
+
const closing = sourceCode.getTokenAfter(asNode(node), { skip: depth - 1 });
|
|
47
|
+
|
|
48
|
+
return opening === null || closing === null
|
|
49
|
+
? [node.range[0], node.range[1]]
|
|
50
|
+
: [opening.range[0], closing.range[1]];
|
|
51
|
+
};
|
|
52
|
+
|
|
53
|
+
/**
|
|
54
|
+
* The source text of `node` to put in a call's argument list. A node's text
|
|
55
|
+
* never includes the parentheses the source wrote around it, so a sequence
|
|
56
|
+
* expression gets them back here, whether or not the source had them:
|
|
57
|
+
* `(setup(), x)` would otherwise become two arguments.
|
|
58
|
+
*/
|
|
59
|
+
export const getArgumentText = (
|
|
60
|
+
sourceCode: TSESLint.SourceCode,
|
|
61
|
+
node: DeepReadonly<TSESTree.Node>,
|
|
62
|
+
): string => {
|
|
63
|
+
const text = sourceCode.text.slice(node.range[0], node.range[1]);
|
|
64
|
+
|
|
65
|
+
return node.type === AST_NODE_TYPES.SequenceExpression ? `(${text})` : text;
|
|
66
|
+
};
|
|
67
|
+
|
|
68
|
+
/**
|
|
69
|
+
* The parser's node, which the token store takes as mutable, for one that this
|
|
70
|
+
* package only reads.
|
|
71
|
+
*/
|
|
72
|
+
const asNode = (node: DeepReadonly<TSESTree.Node>): TSESTree.Node =>
|
|
73
|
+
// eslint-disable-next-line total-functions/no-unsafe-type-assertion
|
|
74
|
+
node as TSESTree.Node;
|
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
import { AST_NODE_TYPES, type TSESTree } from '@typescript-eslint/utils';
|
|
2
|
+
import type { DeepReadonly, RelaxedExtract } from 'ts-type-forge';
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* The four expressions that change a value's type and nothing else: `x as T`,
|
|
6
|
+
* `x!`, `x satisfies T` and `<T>x`.
|
|
7
|
+
*/
|
|
8
|
+
export type TypeWrapper =
|
|
9
|
+
| TSESTree.TSAsExpression
|
|
10
|
+
| TSESTree.TSNonNullExpression
|
|
11
|
+
| TSESTree.TSSatisfiesExpression
|
|
12
|
+
| TSESTree.TSTypeAssertion;
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* The expression a type wrapper among `N` wraps, as mutable or as readonly as
|
|
16
|
+
* `N` itself: `TSESTree.Expression` for a mutable node,
|
|
17
|
+
* `DeepReadonly<TSESTree.Expression>` for a readonly one, and `never` when no
|
|
18
|
+
* member of `N` is a type wrapper.
|
|
19
|
+
*/
|
|
20
|
+
export type WrappedExpression<N> = RelaxedExtract<
|
|
21
|
+
N,
|
|
22
|
+
DeepReadonly<TypeWrapper> | TypeWrapper
|
|
23
|
+
>['expression'];
|
|
24
|
+
|
|
25
|
+
/**
|
|
26
|
+
* Whether `node` is a type wrapper (see {@link TypeWrapper}). Takes a readonly
|
|
27
|
+
* or a mutable node and narrows it without changing which.
|
|
28
|
+
*/
|
|
29
|
+
export const isTypeWrapper = (
|
|
30
|
+
node: DeepReadonly<TSESTree.Node>,
|
|
31
|
+
): node is DeepReadonly<TypeWrapper> | TypeWrapper =>
|
|
32
|
+
TYPE_WRAPPERS.has(node.type);
|
|
33
|
+
|
|
34
|
+
/**
|
|
35
|
+
* `node` with every `as`, `satisfies`, `!` and `<T>` around it taken off: the
|
|
36
|
+
* expression whose value it is. A wrapper changes no value, so a rule judges
|
|
37
|
+
* syntax on what this returns — `xs.length > (0 as number)` compares with the
|
|
38
|
+
* literal `0`, and `(Number satisfies NumberConstructor)('1')` calls `Number` —
|
|
39
|
+
* and writes its fix from the original node, so that the wrapper is kept. A
|
|
40
|
+
* type is another matter: see {@link typeWrapperLayers}.
|
|
41
|
+
*/
|
|
42
|
+
export const skipTypeWrappers = <N extends DeepReadonly<TSESTree.Node>>(
|
|
43
|
+
node: N,
|
|
44
|
+
): N | WrappedExpression<N> =>
|
|
45
|
+
isTypeWrapper(node) ? skipTypeWrappers(node.expression) : node;
|
|
46
|
+
|
|
47
|
+
/**
|
|
48
|
+
* `node` and every expression inside the type wrappers around it, outermost
|
|
49
|
+
* first: `x as A as B` gives `x as A as B`, `x as A` and `x`, the last of which
|
|
50
|
+
* is `skipTypeWrappers(node)`.
|
|
51
|
+
*
|
|
52
|
+
* A conclusion a rule draws from a type — that a guard always holds, that a
|
|
53
|
+
* value is an array, that an operation may throw — has to hold for the type of
|
|
54
|
+
* every layer. An assertion therefore only ever makes a rule more cautious:
|
|
55
|
+
* one that widens (`x as string | null`) is the author saying the value may be
|
|
56
|
+
* `null`, and is respected; one that narrows (`x!`, `v as string` on an
|
|
57
|
+
* `unknown`) is a claim the checker cannot verify, and the value's own type
|
|
58
|
+
* still decides. The fix, which keeps the wrapper, has to type-check at every
|
|
59
|
+
* layer too.
|
|
60
|
+
*/
|
|
61
|
+
export const typeWrapperLayers = <N extends DeepReadonly<TSESTree.Node>>(
|
|
62
|
+
node: N,
|
|
63
|
+
): readonly (N | WrappedExpression<N>)[] =>
|
|
64
|
+
isTypeWrapper(node)
|
|
65
|
+
? ([node, ...typeWrapperLayers(node.expression)] as const)
|
|
66
|
+
: ([node] as const);
|
|
67
|
+
|
|
68
|
+
const TYPE_WRAPPERS: ReadonlySet<AST_NODE_TYPES> = new Set([
|
|
69
|
+
AST_NODE_TYPES.TSAsExpression,
|
|
70
|
+
AST_NODE_TYPES.TSNonNullExpression,
|
|
71
|
+
AST_NODE_TYPES.TSSatisfiesExpression,
|
|
72
|
+
AST_NODE_TYPES.TSTypeAssertion,
|
|
73
|
+
]);
|