eslint-plugin-ts-type-forge 0.2.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 +96 -0
- package/dist/index.d.mts +5 -0
- package/dist/index.d.mts.map +1 -0
- package/dist/index.mjs +3 -0
- package/dist/index.mjs.map +1 -0
- package/dist/plugin.d.mts +3 -0
- package/dist/plugin.d.mts.map +1 -0
- package/dist/plugin.mjs +11 -0
- package/dist/plugin.mjs.map +1 -0
- package/dist/rule-types.d.mts +58 -0
- package/dist/rule-types.d.mts.map +1 -0
- package/dist/rule-types.mjs +2 -0
- package/dist/rule-types.mjs.map +1 -0
- package/dist/rules/ast-utils.d.mts +17 -0
- package/dist/rules/ast-utils.d.mts.map +1 -0
- package/dist/rules/ast-utils.mjs +26 -0
- package/dist/rules/ast-utils.mjs.map +1 -0
- package/dist/rules/constants.d.mts +3 -0
- package/dist/rules/constants.d.mts.map +1 -0
- package/dist/rules/constants.mjs +5 -0
- package/dist/rules/constants.mjs.map +1 -0
- package/dist/rules/import-utils.d.mts +26 -0
- package/dist/rules/import-utils.d.mts.map +1 -0
- package/dist/rules/import-utils.mjs +35 -0
- package/dist/rules/import-utils.mjs.map +1 -0
- package/dist/rules/index.d.mts +2 -0
- package/dist/rules/index.d.mts.map +1 -0
- package/dist/rules/index.mjs +2 -0
- package/dist/rules/index.mjs.map +1 -0
- package/dist/rules/prefer-non-empty-array.d.mts +11 -0
- package/dist/rules/prefer-non-empty-array.d.mts.map +1 -0
- package/dist/rules/prefer-non-empty-array.mjs +81 -0
- package/dist/rules/prefer-non-empty-array.mjs.map +1 -0
- package/dist/rules/rules.d.mts +6 -0
- package/dist/rules/rules.d.mts.map +1 -0
- package/dist/rules/rules.mjs +8 -0
- package/dist/rules/rules.mjs.map +1 -0
- package/dist/types.d.mts +4 -0
- package/dist/types.d.mts.map +1 -0
- package/dist/types.mjs +2 -0
- package/dist/types.mjs.map +1 -0
- package/package.json +77 -0
- package/src/index.mts +7 -0
- package/src/plugin.mts +9 -0
- package/src/rule-types.mts +89 -0
- package/src/rules/ast-utils.mts +76 -0
- package/src/rules/constants.mts +3 -0
- package/src/rules/import-utils.mts +105 -0
- package/src/rules/index.mts +1 -0
- package/src/rules/prefer-non-empty-array.mts +129 -0
- package/src/rules/prefer-non-empty-array.test.mts +192 -0
- package/src/rules/rules.mts +6 -0
- package/src/types.mts +4 -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
ADDED
|
@@ -0,0 +1,96 @@
|
|
|
1
|
+
# eslint-plugin-ts-type-forge
|
|
2
|
+
|
|
3
|
+
ESLint rules that steer TypeScript **type declarations** toward
|
|
4
|
+
[`ts-type-forge`](https://www.npmjs.com/package/ts-type-forge) idioms — e.g.
|
|
5
|
+
preferring `NonEmptyArray<V>` over the hand-rolled `readonly [V, ...V[]]`
|
|
6
|
+
tuple spelling. Every rule is auto-fixable.
|
|
7
|
+
|
|
8
|
+
## Installation
|
|
9
|
+
|
|
10
|
+
```sh
|
|
11
|
+
npm install --save-dev eslint-plugin-ts-type-forge
|
|
12
|
+
```
|
|
13
|
+
|
|
14
|
+
Requires ESLint 9+ (flat config) and TypeScript. No rule is type-aware, so a
|
|
15
|
+
configured TypeScript project is not required.
|
|
16
|
+
|
|
17
|
+
## Usage (flat config)
|
|
18
|
+
|
|
19
|
+
```ts
|
|
20
|
+
// eslint.config.mts
|
|
21
|
+
import {
|
|
22
|
+
eslintPluginTsTypeForge,
|
|
23
|
+
type EslintTsTypeForgeRules,
|
|
24
|
+
} from 'eslint-plugin-ts-type-forge';
|
|
25
|
+
|
|
26
|
+
export default [
|
|
27
|
+
{
|
|
28
|
+
plugins: { 'ts-type-forge': eslintPluginTsTypeForge },
|
|
29
|
+
rules: {
|
|
30
|
+
'ts-type-forge/prefer-non-empty-array': 'error',
|
|
31
|
+
} satisfies Partial<EslintTsTypeForgeRules>,
|
|
32
|
+
},
|
|
33
|
+
];
|
|
34
|
+
```
|
|
35
|
+
|
|
36
|
+
## Rules
|
|
37
|
+
|
|
38
|
+
| Rule | Description |
|
|
39
|
+
| ------------------------ | ------------------------------------------------------- |
|
|
40
|
+
| `prefer-non-empty-array` | Replace `readonly [V, ...V[]]` with `NonEmptyArray<V>`. |
|
|
41
|
+
|
|
42
|
+
### `prefer-non-empty-array`
|
|
43
|
+
|
|
44
|
+
`readonly [V, ...V[]]` is the hand-rolled spelling of "an array with at least
|
|
45
|
+
one element". `ts-type-forge` already exposes it as `NonEmptyArray<V>` (an
|
|
46
|
+
alias of `MinLengthArray<1, V>`), which reads better and stays consistent with
|
|
47
|
+
the rest of the length-constrained array family.
|
|
48
|
+
|
|
49
|
+
```ts
|
|
50
|
+
// ❌
|
|
51
|
+
type Names = readonly [string, ...string[]];
|
|
52
|
+
declare const head: (xs: readonly [number, ...(readonly number[])]) => number;
|
|
53
|
+
|
|
54
|
+
// ✅
|
|
55
|
+
type Names = NonEmptyArray<string>;
|
|
56
|
+
declare const head: (xs: NonEmptyArray<number>) => number;
|
|
57
|
+
```
|
|
58
|
+
|
|
59
|
+
The rule deliberately leaves alone:
|
|
60
|
+
|
|
61
|
+
- mutable tuples (`[V, ...V[]]`), which correspond to
|
|
62
|
+
`MutableNonEmptyArray<V>` rather than `NonEmptyArray<V>`;
|
|
63
|
+
- longer minimum lengths (`readonly [V, V, ...V[]]` — that is
|
|
64
|
+
`MinLengthArray<2, V>`);
|
|
65
|
+
- labelled members (`readonly [head: V, ...tail: V[]]`), because rewriting
|
|
66
|
+
them would silently drop the labels;
|
|
67
|
+
- files that already bind the name `NonEmptyArray` to something else (a local
|
|
68
|
+
alias, or an import from another module).
|
|
69
|
+
|
|
70
|
+
#### Options
|
|
71
|
+
|
|
72
|
+
| Option | Type | Default | Description |
|
|
73
|
+
| ------------- | --------------------- | ---------- | --------------------------------------------------------- |
|
|
74
|
+
| `importStyle` | `'global' \| 'named'` | `'global'` | How `NonEmptyArray` is expected to be brought into scope. |
|
|
75
|
+
|
|
76
|
+
- `'global'` — the ambient globals of `ts-type-forge/global` are in use
|
|
77
|
+
(`/// <reference types="ts-type-forge/global" />`), so the autofix only
|
|
78
|
+
rewrites the type and never touches imports.
|
|
79
|
+
- `'named'` — the autofix additionally inserts
|
|
80
|
+
`import { type NonEmptyArray } from 'ts-type-forge';` when the name is not
|
|
81
|
+
imported yet.
|
|
82
|
+
|
|
83
|
+
Either way, if the file already imports `NonEmptyArray` from `ts-type-forge`
|
|
84
|
+
(possibly under an alias), the autofix reuses that binding.
|
|
85
|
+
|
|
86
|
+
```ts
|
|
87
|
+
{
|
|
88
|
+
rules: {
|
|
89
|
+
'ts-type-forge/prefer-non-empty-array': ['error', { importStyle: 'named' }],
|
|
90
|
+
},
|
|
91
|
+
}
|
|
92
|
+
```
|
|
93
|
+
|
|
94
|
+
## License
|
|
95
|
+
|
|
96
|
+
[Apache-2.0](./LICENSE)
|
package/dist/index.d.mts
ADDED
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
export { eslintPluginTsTypeForge } from './plugin.mjs';
|
|
2
|
+
export type { EslintTsTypeForgeRules, EslintTsTypeForgeRulesOption, } from './rule-types.mjs';
|
|
3
|
+
export { tsTypeForgeRules } from './rules/index.mjs';
|
|
4
|
+
export { type ESLintPlugin } from './types.mjs';
|
|
5
|
+
//# sourceMappingURL=index.d.mts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.mts","sourceRoot":"","sources":["../src/index.mts"],"names":[],"mappings":"AAAA,OAAO,EAAE,uBAAuB,EAAE,MAAM,cAAc,CAAC;AACvD,YAAY,EACV,sBAAsB,EACtB,4BAA4B,GAC7B,MAAM,kBAAkB,CAAC;AAC1B,OAAO,EAAE,gBAAgB,EAAE,MAAM,mBAAmB,CAAC;AACrD,OAAO,EAAE,KAAK,YAAY,EAAE,MAAM,aAAa,CAAC"}
|
package/dist/index.mjs
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.mjs","sources":[],"sourcesContent":[],"names":[],"mappings":";"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"plugin.d.mts","sourceRoot":"","sources":["../src/plugin.mts"],"names":[],"mappings":"AACA,OAAO,EAAE,KAAK,YAAY,EAAE,MAAM,aAAa,CAAC;AAEhD,eAAO,MAAM,uBAAuB,EAAE,YAK5B,CAAC"}
|
package/dist/plugin.mjs
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"plugin.mjs","sources":["../src/plugin.mts"],"sourcesContent":["import { tsTypeForgeRules } from './rules/index.mjs';\nimport { type ESLintPlugin } from './types.mjs';\n\nexport const eslintPluginTsTypeForge: ESLintPlugin = {\n meta: {\n name: 'eslint-plugin-ts-type-forge',\n },\n rules: tsTypeForgeRules,\n} as const;\n"],"names":[],"mappings":";;AAGO,MAAM,uBAAA,GAAwC;AAAA,EACnD,IAAA,EAAM;AAAA,IACJ,IAAA,EAAM;AAAA,GACR;AAAA,EACA,KAAA,EAAO;AACT;;;;"}
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
import { type TSESLint } from '@typescript-eslint/utils';
|
|
2
|
+
import { type Linter } from 'eslint';
|
|
3
|
+
import { type TypeEq } from 'ts-type-forge';
|
|
4
|
+
import { type tsTypeForgeRules } from './rules/index.mjs';
|
|
5
|
+
type SpreadOptionsIfIsArray<T extends readonly [Linter.StringSeverity, unknown]> = T[1] extends readonly unknown[] ? readonly [Linter.StringSeverity, ...T[1]] : T;
|
|
6
|
+
/**
|
|
7
|
+
* Extracts the options tuple type of a rule from its TypeScript
|
|
8
|
+
* implementation, so that option types stay in sync with the rule source
|
|
9
|
+
* without converting `meta.schema` (JSON Schema) back into TypeScript types.
|
|
10
|
+
*/
|
|
11
|
+
type RuleOptionsOf<Name extends keyof typeof tsTypeForgeRules> = (typeof tsTypeForgeRules)[Name] extends TSESLint.RuleModule<infer _MessageIds extends string, infer Options extends readonly unknown[]> ? Options : never;
|
|
12
|
+
/**
|
|
13
|
+
* @description Replace the `readonly [V, ...V[]]` tuple spelling with `NonEmptyArray<V>` from ts-type-forge.
|
|
14
|
+
*
|
|
15
|
+
* ```md
|
|
16
|
+
* | key | value |
|
|
17
|
+
* | :--------- | :--------- |
|
|
18
|
+
* | type | suggestion |
|
|
19
|
+
* | deprecated | false |
|
|
20
|
+
* | fixable | code |
|
|
21
|
+
* ```
|
|
22
|
+
*/
|
|
23
|
+
declare namespace PreferNonEmptyArray {
|
|
24
|
+
/**
|
|
25
|
+
* ### schema
|
|
26
|
+
*
|
|
27
|
+
* ```json
|
|
28
|
+
* [
|
|
29
|
+
* {
|
|
30
|
+
* "type": "object",
|
|
31
|
+
* "properties": {
|
|
32
|
+
* "importStyle": {
|
|
33
|
+
* "type": "string",
|
|
34
|
+
* "enum": [
|
|
35
|
+
* "global",
|
|
36
|
+
* "named"
|
|
37
|
+
* ],
|
|
38
|
+
* "description": "How `NonEmptyArray` is brought into scope. 'global' (default) assumes the ambient globals of `ts-type-forge/global` and never touches imports; 'named' makes the autofix add `import { type NonEmptyArray } from 'ts-type-forge';` when the name is not imported yet."
|
|
39
|
+
* }
|
|
40
|
+
* },
|
|
41
|
+
* "additionalProperties": false
|
|
42
|
+
* }
|
|
43
|
+
* ]
|
|
44
|
+
* ```
|
|
45
|
+
*/
|
|
46
|
+
type Options = NonNullable<RuleOptionsOf<'prefer-non-empty-array'>[0]>;
|
|
47
|
+
type RuleEntry = 'off' | Linter.Severity | SpreadOptionsIfIsArray<readonly [Linter.StringSeverity, Options]>;
|
|
48
|
+
}
|
|
49
|
+
export type EslintTsTypeForgeRules = Readonly<{
|
|
50
|
+
'ts-type-forge/prefer-non-empty-array': PreferNonEmptyArray.RuleEntry;
|
|
51
|
+
}>;
|
|
52
|
+
export type EslintTsTypeForgeRulesOption = Readonly<{
|
|
53
|
+
'ts-type-forge/prefer-non-empty-array': PreferNonEmptyArray.Options;
|
|
54
|
+
}>;
|
|
55
|
+
type AssertTrue<B extends true> = B;
|
|
56
|
+
export type RuleNamesAreInSync = AssertTrue<TypeEq<keyof EslintTsTypeForgeRules, `ts-type-forge/${keyof typeof tsTypeForgeRules}`>>;
|
|
57
|
+
export {};
|
|
58
|
+
//# sourceMappingURL=rule-types.d.mts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"rule-types.d.mts","sourceRoot":"","sources":["../src/rule-types.mts"],"names":[],"mappings":"AAKA,OAAO,EAAE,KAAK,QAAQ,EAAE,MAAM,0BAA0B,CAAC;AACzD,OAAO,EAAE,KAAK,MAAM,EAAE,MAAM,QAAQ,CAAC;AACrC,OAAO,EAAE,KAAK,MAAM,EAAE,MAAM,eAAe,CAAC;AAC5C,OAAO,EAAE,KAAK,gBAAgB,EAAE,MAAM,mBAAmB,CAAC;AAE1D,KAAK,sBAAsB,CACzB,CAAC,SAAS,SAAS,CAAC,MAAM,CAAC,cAAc,EAAE,OAAO,CAAC,IACjD,CAAC,CAAC,CAAC,CAAC,SAAS,SAAS,OAAO,EAAE,GAC/B,SAAS,CAAC,MAAM,CAAC,cAAc,EAAE,GAAG,CAAC,CAAC,CAAC,CAAC,CAAC,GACzC,CAAC,CAAC;AAEN;;;;GAIG;AACH,KAAK,aAAa,CAAC,IAAI,SAAS,MAAM,OAAO,gBAAgB,IAC3D,CAAC,OAAO,gBAAgB,CAAC,CAAC,IAAI,CAAC,SAAS,QAAQ,CAAC,UAAU,CACzD,MAAM,WAAW,SAAS,MAAM,EAChC,MAAM,OAAO,SAAS,SAAS,OAAO,EAAE,CACzC,GACG,OAAO,GACP,KAAK,CAAC;AAEZ;;;;;;;;;;GAUG;AACH,kBAAU,mBAAmB,CAAC;IAC5B;;;;;;;;;;;;;;;;;;;;;OAqBG;IACH,KAAY,OAAO,GAAG,WAAW,CAAC,aAAa,CAAC,wBAAwB,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC;IAE9E,KAAY,SAAS,GACjB,KAAK,GACL,MAAM,CAAC,QAAQ,GACf,sBAAsB,CAAC,SAAS,CAAC,MAAM,CAAC,cAAc,EAAE,OAAO,CAAC,CAAC,CAAC;CACvE;AAED,MAAM,MAAM,sBAAsB,GAAG,QAAQ,CAAC;IAC5C,sCAAsC,EAAE,mBAAmB,CAAC,SAAS,CAAC;CACvE,CAAC,CAAC;AAEH,MAAM,MAAM,4BAA4B,GAAG,QAAQ,CAAC;IAClD,sCAAsC,EAAE,mBAAmB,CAAC,OAAO,CAAC;CACrE,CAAC,CAAC;AAIH,KAAK,UAAU,CAAC,CAAC,SAAS,IAAI,IAAI,CAAC,CAAC;AAEpC,MAAM,MAAM,kBAAkB,GAAG,UAAU,CACzC,MAAM,CACJ,MAAM,sBAAsB,EAC5B,iBAAiB,MAAM,OAAO,gBAAgB,EAAE,CACjD,CACF,CAAC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"rule-types.mjs","sources":[],"sourcesContent":[],"names":[],"mappings":""}
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import { type TSESLint, type TSESTree } from '@typescript-eslint/utils';
|
|
2
|
+
/**
|
|
3
|
+
* Matches the "non-empty array" tuple spelling — `readonly [V, ...V[]]` (and
|
|
4
|
+
* its equivalent `readonly [V, ...(readonly V[])]`) — and returns the source
|
|
5
|
+
* text of `V`, or `undefined` when `node` is not that shape.
|
|
6
|
+
*
|
|
7
|
+
* Deliberately narrow:
|
|
8
|
+
*
|
|
9
|
+
* - Only the `readonly` operator is matched. A mutable `[V, ...V[]]` is a
|
|
10
|
+
* different type (`MutableNonEmptyArray<V>`) and is left alone.
|
|
11
|
+
* - Exactly two elements, the second being a rest element whose element type is
|
|
12
|
+
* textually identical to the first.
|
|
13
|
+
* - Labelled members (`readonly [head: V, ...tail: V[]]`) are skipped, since
|
|
14
|
+
* rewriting them would silently drop the labels.
|
|
15
|
+
*/
|
|
16
|
+
export declare const getNonEmptyArrayElementText: (node: TSESTree.TSTypeOperator, sourceCode: TSESLint.SourceCode) => string | undefined;
|
|
17
|
+
//# sourceMappingURL=ast-utils.d.mts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"ast-utils.d.mts","sourceRoot":"","sources":["../../src/rules/ast-utils.mts"],"names":[],"mappings":"AAAA,OAAO,EAEL,KAAK,QAAQ,EACb,KAAK,QAAQ,EACd,MAAM,0BAA0B,CAAC;AAIlC;;;;;;;;;;;;;GAaG;AACH,eAAO,MAAM,2BAA2B,SAChC,QAAQ,CAAC,cAAc,cACjB,QAAQ,CAAC,UAAU,KAC9B,MAAM,GAAG,SAgCX,CAAC"}
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import { AST_NODE_TYPES } from '@typescript-eslint/utils';
|
|
2
|
+
|
|
3
|
+
const getNonEmptyArrayElementText = (node, sourceCode) => {
|
|
4
|
+
if (node.operator !== "readonly") return void 0;
|
|
5
|
+
const tuple = node.typeAnnotation;
|
|
6
|
+
if (tuple?.type !== AST_NODE_TYPES.TSTupleType) return void 0;
|
|
7
|
+
const [head, tail, ...rest] = tuple.elementTypes;
|
|
8
|
+
if (head === void 0 || tail === void 0 || rest.length > 0) {
|
|
9
|
+
return void 0;
|
|
10
|
+
}
|
|
11
|
+
if (head.type === AST_NODE_TYPES.TSRestType || head.type === AST_NODE_TYPES.TSOptionalType || head.type === AST_NODE_TYPES.TSNamedTupleMember || tail.type !== AST_NODE_TYPES.TSRestType) {
|
|
12
|
+
return void 0;
|
|
13
|
+
}
|
|
14
|
+
const restElementType = getArrayElementType(tail.typeAnnotation);
|
|
15
|
+
if (restElementType === void 0) return void 0;
|
|
16
|
+
const headText = sourceCode.getText(head);
|
|
17
|
+
return normalizeWhitespace(headText) === normalizeWhitespace(sourceCode.getText(restElementType)) ? headText : void 0;
|
|
18
|
+
};
|
|
19
|
+
const getArrayElementType = (node) => {
|
|
20
|
+
if (node.type === AST_NODE_TYPES.TSArrayType) return node.elementType;
|
|
21
|
+
return node.type === AST_NODE_TYPES.TSTypeOperator && node.operator === "readonly" && node.typeAnnotation?.type === AST_NODE_TYPES.TSArrayType ? node.typeAnnotation.elementType : void 0;
|
|
22
|
+
};
|
|
23
|
+
const normalizeWhitespace = (text) => text.replaceAll(/\s+/gu, " ").trim();
|
|
24
|
+
|
|
25
|
+
export { getNonEmptyArrayElementText };
|
|
26
|
+
//# sourceMappingURL=ast-utils.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"ast-utils.mjs","sources":["../../src/rules/ast-utils.mts"],"sourcesContent":["import {\n AST_NODE_TYPES,\n type TSESLint,\n type TSESTree,\n} from '@typescript-eslint/utils';\n\n/* eslint-disable @typescript-eslint/prefer-readonly-parameter-types */\n\n/**\n * Matches the \"non-empty array\" tuple spelling — `readonly [V, ...V[]]` (and\n * its equivalent `readonly [V, ...(readonly V[])]`) — and returns the source\n * text of `V`, or `undefined` when `node` is not that shape.\n *\n * Deliberately narrow:\n *\n * - Only the `readonly` operator is matched. A mutable `[V, ...V[]]` is a\n * different type (`MutableNonEmptyArray<V>`) and is left alone.\n * - Exactly two elements, the second being a rest element whose element type is\n * textually identical to the first.\n * - Labelled members (`readonly [head: V, ...tail: V[]]`) are skipped, since\n * rewriting them would silently drop the labels.\n */\nexport const getNonEmptyArrayElementText = (\n node: TSESTree.TSTypeOperator,\n sourceCode: TSESLint.SourceCode,\n): string | undefined => {\n if (node.operator !== 'readonly') return undefined;\n\n const tuple = node.typeAnnotation;\n\n if (tuple?.type !== AST_NODE_TYPES.TSTupleType) return undefined;\n\n const [head, tail, ...rest] = tuple.elementTypes;\n\n if (head === undefined || tail === undefined || rest.length > 0) {\n return undefined;\n }\n\n if (\n head.type === AST_NODE_TYPES.TSRestType ||\n head.type === AST_NODE_TYPES.TSOptionalType ||\n head.type === AST_NODE_TYPES.TSNamedTupleMember ||\n tail.type !== AST_NODE_TYPES.TSRestType\n ) {\n return undefined;\n }\n\n const restElementType = getArrayElementType(tail.typeAnnotation);\n\n if (restElementType === undefined) return undefined;\n\n const headText = sourceCode.getText(head);\n\n return normalizeWhitespace(headText) ===\n normalizeWhitespace(sourceCode.getText(restElementType))\n ? headText\n : undefined;\n};\n\n/** Unwraps `V[]` and `readonly V[]` to `V`. */\nconst getArrayElementType = (\n node: TSESTree.TypeNode,\n): TSESTree.TypeNode | undefined => {\n if (node.type === AST_NODE_TYPES.TSArrayType) return node.elementType;\n\n return node.type === AST_NODE_TYPES.TSTypeOperator &&\n node.operator === 'readonly' &&\n node.typeAnnotation?.type === AST_NODE_TYPES.TSArrayType\n ? node.typeAnnotation.elementType\n : undefined;\n};\n\nconst normalizeWhitespace = (text: string): string =>\n text.replaceAll(/\\s+/gu, ' ').trim();\n\n/* eslint-enable @typescript-eslint/prefer-readonly-parameter-types */\n"],"names":[],"mappings":";;AAsBO,MAAM,2BAAA,GAA8B,CACzC,IAAA,EACA,UAAA,KACuB;AACvB,EAAA,IAAI,IAAA,CAAK,QAAA,KAAa,UAAA,EAAY,OAAO,MAAA;AAEzC,EAAA,MAAM,QAAQ,IAAA,CAAK,cAAA;AAEnB,EAAA,IAAI,KAAA,EAAO,IAAA,KAAS,cAAA,CAAe,WAAA,EAAa,OAAO,MAAA;AAEvD,EAAA,MAAM,CAAC,IAAA,EAAM,IAAA,EAAM,GAAG,IAAI,IAAI,KAAA,CAAM,YAAA;AAEpC,EAAA,IAAI,SAAS,MAAA,IAAa,IAAA,KAAS,MAAA,IAAa,IAAA,CAAK,SAAS,CAAA,EAAG;AAC/D,IAAA,OAAO,MAAA;AAAA,EACT;AAEA,EAAA,IACE,IAAA,CAAK,IAAA,KAAS,cAAA,CAAe,UAAA,IAC7B,KAAK,IAAA,KAAS,cAAA,CAAe,cAAA,IAC7B,IAAA,CAAK,SAAS,cAAA,CAAe,kBAAA,IAC7B,IAAA,CAAK,IAAA,KAAS,eAAe,UAAA,EAC7B;AACA,IAAA,OAAO,MAAA;AAAA,EACT;AAEA,EAAA,MAAM,eAAA,GAAkB,mBAAA,CAAoB,IAAA,CAAK,cAAc,CAAA;AAE/D,EAAA,IAAI,eAAA,KAAoB,QAAW,OAAO,MAAA;AAE1C,EAAA,MAAM,QAAA,GAAW,UAAA,CAAW,OAAA,CAAQ,IAAI,CAAA;AAExC,EAAA,OAAO,mBAAA,CAAoB,QAAQ,CAAA,KACjC,mBAAA,CAAoB,WAAW,OAAA,CAAQ,eAAe,CAAC,CAAA,GACrD,QAAA,GACA,MAAA;AACN;AAGA,MAAM,mBAAA,GAAsB,CAC1B,IAAA,KACkC;AAClC,EAAA,IAAI,IAAA,CAAK,IAAA,KAAS,cAAA,CAAe,WAAA,SAAoB,IAAA,CAAK,WAAA;AAE1D,EAAA,OAAO,IAAA,CAAK,IAAA,KAAS,cAAA,CAAe,cAAA,IAClC,KAAK,QAAA,KAAa,UAAA,IAClB,IAAA,CAAK,cAAA,EAAgB,IAAA,KAAS,cAAA,CAAe,WAAA,GAC3C,IAAA,CAAK,eAAe,WAAA,GACpB,MAAA;AACN,CAAA;AAEA,MAAM,mBAAA,GAAsB,CAAC,IAAA,KAC3B,IAAA,CAAK,WAAW,OAAA,EAAS,GAAG,EAAE,IAAA,EAAK;;;;"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"constants.d.mts","sourceRoot":"","sources":["../../src/rules/constants.mts"],"names":[],"mappings":"AAAA,eAAO,MAAM,oBAAoB,kBAAkB,CAAC;AAEpD,eAAO,MAAM,yBAAyB,kBAAkB,CAAC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"constants.mjs","sources":["../../src/rules/constants.mts"],"sourcesContent":["export const TS_TYPE_FORGE_MODULE = 'ts-type-forge';\n\nexport const NON_EMPTY_ARRAY_TYPE_NAME = 'NonEmptyArray';\n"],"names":[],"mappings":"AAAO,MAAM,oBAAA,GAAuB;AAE7B,MAAM,yBAAA,GAA4B;;;;"}
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import { type TSESLint, type TSESTree } from '@typescript-eslint/utils';
|
|
2
|
+
/** Every `import ... from 'ts-type-forge'` declaration in the file. */
|
|
3
|
+
export declare const getTsTypeForgeImports: (program: TSESTree.Program) => readonly TSESTree.ImportDeclaration[];
|
|
4
|
+
/**
|
|
5
|
+
* Returns the in-scope local name a given ts-type-forge export is imported as
|
|
6
|
+
* (handling aliases such as `import type { NonEmptyArray as NEA }` → `'NEA'`),
|
|
7
|
+
* or `undefined` when it is not imported. Used so autofixes reference the
|
|
8
|
+
* binding that actually exists rather than the canonical name.
|
|
9
|
+
*/
|
|
10
|
+
export declare const getImportedLocalName: (importDeclarations: readonly TSESTree.ImportDeclaration[], importedName: string) => string | undefined;
|
|
11
|
+
/**
|
|
12
|
+
* Whether the file already binds `name` to something that is not the
|
|
13
|
+
* ts-type-forge export of the same name — a local type alias / interface /
|
|
14
|
+
* class / enum, or an import from another module. Autofixes that introduce
|
|
15
|
+
* `name` must bail out in that case, because the rewritten type would resolve
|
|
16
|
+
* to the wrong declaration. (ts-type-forge's own sources, which declare
|
|
17
|
+
* `NonEmptyArray` themselves, are the motivating example.)
|
|
18
|
+
*/
|
|
19
|
+
export declare const hasConflictingDeclaration: (program: TSESTree.Program, name: string) => boolean;
|
|
20
|
+
/**
|
|
21
|
+
* Inserts `import { type <name> } from 'ts-type-forge';` at the top of the
|
|
22
|
+
* file. Placement is deliberately naive: prettier-plugin-organize-imports (or
|
|
23
|
+
* any equivalent) merges and sorts it afterwards.
|
|
24
|
+
*/
|
|
25
|
+
export declare const buildTypeImportFix: (fixer: TSESLint.RuleFixer, program: TSESTree.Program, name: string) => readonly TSESLint.RuleFix[];
|
|
26
|
+
//# sourceMappingURL=import-utils.d.mts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"import-utils.d.mts","sourceRoot":"","sources":["../../src/rules/import-utils.mts"],"names":[],"mappings":"AAAA,OAAO,EAEL,KAAK,QAAQ,EACb,KAAK,QAAQ,EACd,MAAM,0BAA0B,CAAC;AAKlC,uEAAuE;AACvE,eAAO,MAAM,qBAAqB,YACvB,QAAQ,CAAC,OAAO,KACxB,SAAS,QAAQ,CAAC,iBAAiB,EAKnC,CAAC;AAEJ;;;;;GAKG;AACH,eAAO,MAAM,oBAAoB,uBACX,SAAS,QAAQ,CAAC,iBAAiB,EAAE,gBAC3C,MAAM,KACnB,MAAM,GAAG,SASK,CAAC;AAElB;;;;;;;GAOG;AACH,eAAO,MAAM,yBAAyB,YAC3B,QAAQ,CAAC,OAAO,QACnB,MAAM,KACX,OAgBC,CAAC;AAEL;;;;GAIG;AACH,eAAO,MAAM,kBAAkB,UACtB,QAAQ,CAAC,SAAS,WAChB,QAAQ,CAAC,OAAO,QACnB,MAAM,KACX,SAAS,QAAQ,CAAC,OAAO,EAMhB,CAAC"}
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
import { AST_NODE_TYPES } from '@typescript-eslint/utils';
|
|
2
|
+
import { TS_TYPE_FORGE_MODULE } from './constants.mjs';
|
|
3
|
+
|
|
4
|
+
const getTsTypeForgeImports = (program) => program.body.filter(
|
|
5
|
+
(node) => node.type === AST_NODE_TYPES.ImportDeclaration && node.source.value === TS_TYPE_FORGE_MODULE
|
|
6
|
+
);
|
|
7
|
+
const getImportedLocalName = (importDeclarations, importedName) => importDeclarations.flatMap((declaration) => declaration.specifiers).find(
|
|
8
|
+
(specifier) => specifier.type === AST_NODE_TYPES.ImportSpecifier && (specifier.imported.type === AST_NODE_TYPES.Identifier ? specifier.imported.name : specifier.imported.value) === importedName
|
|
9
|
+
)?.local.name;
|
|
10
|
+
const hasConflictingDeclaration = (program, name) => program.body.some((node) => {
|
|
11
|
+
if (node.type === AST_NODE_TYPES.ImportDeclaration) {
|
|
12
|
+
return node.source.value !== TS_TYPE_FORGE_MODULE && node.specifiers.some((specifier) => specifier.local.name === name);
|
|
13
|
+
}
|
|
14
|
+
return declaresName(
|
|
15
|
+
node.type === AST_NODE_TYPES.ExportNamedDeclaration || node.type === AST_NODE_TYPES.ExportDefaultDeclaration ? node.declaration : node,
|
|
16
|
+
name
|
|
17
|
+
);
|
|
18
|
+
});
|
|
19
|
+
const buildTypeImportFix = (fixer, program, name) => [
|
|
20
|
+
fixer.insertTextBefore(
|
|
21
|
+
program,
|
|
22
|
+
`import { type ${name} } from '${TS_TYPE_FORGE_MODULE}';
|
|
23
|
+
`
|
|
24
|
+
)
|
|
25
|
+
];
|
|
26
|
+
const declaresName = (node, name) => {
|
|
27
|
+
if (node === null || node === void 0) return false;
|
|
28
|
+
if (node.type === AST_NODE_TYPES.TSTypeAliasDeclaration || node.type === AST_NODE_TYPES.TSInterfaceDeclaration || node.type === AST_NODE_TYPES.TSEnumDeclaration) {
|
|
29
|
+
return node.id.name === name;
|
|
30
|
+
}
|
|
31
|
+
return node.type === AST_NODE_TYPES.ClassDeclaration && node.id?.name === name;
|
|
32
|
+
};
|
|
33
|
+
|
|
34
|
+
export { buildTypeImportFix, getImportedLocalName, getTsTypeForgeImports, hasConflictingDeclaration };
|
|
35
|
+
//# sourceMappingURL=import-utils.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"import-utils.mjs","sources":["../../src/rules/import-utils.mts"],"sourcesContent":["import {\n AST_NODE_TYPES,\n type TSESLint,\n type TSESTree,\n} from '@typescript-eslint/utils';\nimport { TS_TYPE_FORGE_MODULE } from './constants.mjs';\n\n/* eslint-disable @typescript-eslint/prefer-readonly-parameter-types */\n\n/** Every `import ... from 'ts-type-forge'` declaration in the file. */\nexport const getTsTypeForgeImports = (\n program: TSESTree.Program,\n): readonly TSESTree.ImportDeclaration[] =>\n program.body.filter(\n (node): node is TSESTree.ImportDeclaration =>\n node.type === AST_NODE_TYPES.ImportDeclaration &&\n node.source.value === TS_TYPE_FORGE_MODULE,\n );\n\n/**\n * Returns the in-scope local name a given ts-type-forge export is imported as\n * (handling aliases such as `import type { NonEmptyArray as NEA }` → `'NEA'`),\n * or `undefined` when it is not imported. Used so autofixes reference the\n * binding that actually exists rather than the canonical name.\n */\nexport const getImportedLocalName = (\n importDeclarations: readonly TSESTree.ImportDeclaration[],\n importedName: string,\n): string | undefined =>\n importDeclarations\n .flatMap((declaration) => declaration.specifiers)\n .find(\n (specifier) =>\n specifier.type === AST_NODE_TYPES.ImportSpecifier &&\n (specifier.imported.type === AST_NODE_TYPES.Identifier\n ? specifier.imported.name\n : specifier.imported.value) === importedName,\n )?.local.name;\n\n/**\n * Whether the file already binds `name` to something that is not the\n * ts-type-forge export of the same name — a local type alias / interface /\n * class / enum, or an import from another module. Autofixes that introduce\n * `name` must bail out in that case, because the rewritten type would resolve\n * to the wrong declaration. (ts-type-forge's own sources, which declare\n * `NonEmptyArray` themselves, are the motivating example.)\n */\nexport const hasConflictingDeclaration = (\n program: TSESTree.Program,\n name: string,\n): boolean =>\n program.body.some((node) => {\n if (node.type === AST_NODE_TYPES.ImportDeclaration) {\n return (\n node.source.value !== TS_TYPE_FORGE_MODULE &&\n node.specifiers.some((specifier) => specifier.local.name === name)\n );\n }\n\n return declaresName(\n node.type === AST_NODE_TYPES.ExportNamedDeclaration ||\n node.type === AST_NODE_TYPES.ExportDefaultDeclaration\n ? node.declaration\n : node,\n name,\n );\n });\n\n/**\n * Inserts `import { type <name> } from 'ts-type-forge';` at the top of the\n * file. Placement is deliberately naive: prettier-plugin-organize-imports (or\n * any equivalent) merges and sorts it afterwards.\n */\nexport const buildTypeImportFix = (\n fixer: TSESLint.RuleFixer,\n program: TSESTree.Program,\n name: string,\n): readonly TSESLint.RuleFix[] =>\n [\n fixer.insertTextBefore(\n program,\n `import { type ${name} } from '${TS_TYPE_FORGE_MODULE}';\\n`,\n ),\n ] as const;\n\nconst declaresName = (\n node: TSESTree.Node | null | undefined,\n name: string,\n): boolean => {\n if (node === null || node === undefined) return false;\n\n if (\n node.type === AST_NODE_TYPES.TSTypeAliasDeclaration ||\n node.type === AST_NODE_TYPES.TSInterfaceDeclaration ||\n node.type === AST_NODE_TYPES.TSEnumDeclaration\n ) {\n return node.id.name === name;\n }\n\n return (\n node.type === AST_NODE_TYPES.ClassDeclaration && node.id?.name === name\n );\n};\n\n/* eslint-enable @typescript-eslint/prefer-readonly-parameter-types */\n"],"names":[],"mappings":";;;AAUO,MAAM,qBAAA,GAAwB,CACnC,OAAA,KAEA,OAAA,CAAQ,IAAA,CAAK,MAAA;AAAA,EACX,CAAC,SACC,IAAA,CAAK,IAAA,KAAS,eAAe,iBAAA,IAC7B,IAAA,CAAK,OAAO,KAAA,KAAU;AAC1B;AAQK,MAAM,oBAAA,GAAuB,CAClC,kBAAA,EACA,YAAA,KAEA,kBAAA,CACG,QAAQ,CAAC,WAAA,KAAgB,WAAA,CAAY,UAAU,CAAA,CAC/C,IAAA;AAAA,EACC,CAAC,SAAA,KACC,SAAA,CAAU,IAAA,KAAS,cAAA,CAAe,oBACjC,SAAA,CAAU,QAAA,CAAS,IAAA,KAAS,cAAA,CAAe,aACxC,SAAA,CAAU,QAAA,CAAS,IAAA,GACnB,SAAA,CAAU,SAAS,KAAA,MAAW;AACtC,CAAA,EAAG,KAAA,CAAM;AAUN,MAAM,yBAAA,GAA4B,CACvC,OAAA,EACA,IAAA,KAEA,QAAQ,IAAA,CAAK,IAAA,CAAK,CAAC,IAAA,KAAS;AAC1B,EAAA,IAAI,IAAA,CAAK,IAAA,KAAS,cAAA,CAAe,iBAAA,EAAmB;AAClD,IAAA,OACE,IAAA,CAAK,MAAA,CAAO,KAAA,KAAU,oBAAA,IACtB,IAAA,CAAK,UAAA,CAAW,IAAA,CAAK,CAAC,SAAA,KAAc,SAAA,CAAU,KAAA,CAAM,IAAA,KAAS,IAAI,CAAA;AAAA,EAErE;AAEA,EAAA,OAAO,YAAA;AAAA,IACL,IAAA,CAAK,SAAS,cAAA,CAAe,sBAAA,IAC3B,KAAK,IAAA,KAAS,cAAA,CAAe,wBAAA,GAC3B,IAAA,CAAK,WAAA,GACL,IAAA;AAAA,IACJ;AAAA,GACF;AACF,CAAC;AAOI,MAAM,kBAAA,GAAqB,CAChC,KAAA,EACA,OAAA,EACA,IAAA,KAEA;AAAA,EACE,KAAA,CAAM,gBAAA;AAAA,IACJ,OAAA;AAAA,IACA,CAAA,cAAA,EAAiB,IAAI,CAAA,SAAA,EAAY,oBAAoB,CAAA;AAAA;AAAA;AAEzD;AAEF,MAAM,YAAA,GAAe,CACnB,IAAA,EACA,IAAA,KACY;AACZ,EAAA,IAAI,IAAA,KAAS,IAAA,IAAQ,IAAA,KAAS,MAAA,EAAW,OAAO,KAAA;AAEhD,EAAA,IACE,IAAA,CAAK,IAAA,KAAS,cAAA,CAAe,sBAAA,IAC7B,IAAA,CAAK,IAAA,KAAS,cAAA,CAAe,sBAAA,IAC7B,IAAA,CAAK,IAAA,KAAS,cAAA,CAAe,iBAAA,EAC7B;AACA,IAAA,OAAO,IAAA,CAAK,GAAG,IAAA,KAAS,IAAA;AAAA,EAC1B;AAEA,EAAA,OACE,KAAK,IAAA,KAAS,cAAA,CAAe,gBAAA,IAAoB,IAAA,CAAK,IAAI,IAAA,KAAS,IAAA;AAEvE,CAAA;;;;"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.mts","sourceRoot":"","sources":["../../src/rules/index.mts"],"names":[],"mappings":"AAAA,cAAc,aAAa,CAAC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.mjs","sources":[],"sourcesContent":[],"names":[],"mappings":""}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import { type TSESLint } from '@typescript-eslint/utils';
|
|
2
|
+
type ImportStyle = 'global' | 'named';
|
|
3
|
+
type Options = readonly [
|
|
4
|
+
Readonly<{
|
|
5
|
+
importStyle?: ImportStyle;
|
|
6
|
+
}>?
|
|
7
|
+
];
|
|
8
|
+
type MessageIds = 'useNonEmptyArray';
|
|
9
|
+
export declare const preferNonEmptyArray: TSESLint.RuleModule<MessageIds, Options>;
|
|
10
|
+
export {};
|
|
11
|
+
//# sourceMappingURL=prefer-non-empty-array.d.mts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"prefer-non-empty-array.d.mts","sourceRoot":"","sources":["../../src/rules/prefer-non-empty-array.mts"],"names":[],"mappings":"AAAA,OAAO,EAAE,KAAK,QAAQ,EAAiB,MAAM,0BAA0B,CAAC;AAaxE,KAAK,WAAW,GAAG,QAAQ,GAAG,OAAO,CAAC;AAEtC,KAAK,OAAO,GAAG,SAAS;IACtB,QAAQ,CAAC;QACP,WAAW,CAAC,EAAE,WAAW,CAAC;KAC3B,CAAC,CAAC;CACJ,CAAC;AAEF,KAAK,UAAU,GAAG,kBAAkB,CAAC;AAIrC,eAAO,MAAM,mBAAmB,EAAE,QAAQ,CAAC,UAAU,CAAC,UAAU,EAAE,OAAO,CAuG/D,CAAC"}
|