eslint-plugin-ts-fortress 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 +77 -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 +25 -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/constants.d.mts +4 -0
- package/dist/rules/constants.d.mts.map +1 -0
- package/dist/rules/constants.mjs +6 -0
- package/dist/rules/constants.mjs.map +1 -0
- package/dist/rules/import-utils.d.mts +40 -0
- package/dist/rules/import-utils.d.mts.map +1 -0
- package/dist/rules/import-utils.mjs +68 -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 +6 -0
- package/dist/rules/prefer-non-empty-array.d.mts.map +1 -0
- package/dist/rules/prefer-non-empty-array.mjs +100 -0
- package/dist/rules/prefer-non-empty-array.mjs.map +1 -0
- package/dist/rules/rules.d.mts +4 -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 +40 -0
- package/src/rules/constants.mts +5 -0
- package/src/rules/import-utils.mts +185 -0
- package/src/rules/index.mts +1 -0
- package/src/rules/prefer-non-empty-array.mts +190 -0
- package/src/rules/prefer-non-empty-array.test.mts +222 -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,77 @@
|
|
|
1
|
+
# eslint-plugin-ts-fortress
|
|
2
|
+
|
|
3
|
+
ESLint rules that steer schema definitions toward
|
|
4
|
+
[`ts-fortress`](https://www.npmjs.com/package/ts-fortress) idioms. Every rule is
|
|
5
|
+
auto-fixable.
|
|
6
|
+
|
|
7
|
+
## Installation
|
|
8
|
+
|
|
9
|
+
```sh
|
|
10
|
+
npm install --save-dev eslint-plugin-ts-fortress
|
|
11
|
+
```
|
|
12
|
+
|
|
13
|
+
Requires ESLint 9+ (flat config) and TypeScript. No rule is type-aware, so a
|
|
14
|
+
configured TypeScript project is not required.
|
|
15
|
+
|
|
16
|
+
## Usage (flat config)
|
|
17
|
+
|
|
18
|
+
```ts
|
|
19
|
+
// eslint.config.mts
|
|
20
|
+
import {
|
|
21
|
+
eslintPluginTsFortress,
|
|
22
|
+
type EslintTsFortressRules,
|
|
23
|
+
} from 'eslint-plugin-ts-fortress';
|
|
24
|
+
|
|
25
|
+
export default [
|
|
26
|
+
{
|
|
27
|
+
plugins: { 'ts-fortress': eslintPluginTsFortress },
|
|
28
|
+
rules: {
|
|
29
|
+
'ts-fortress/prefer-non-empty-array': 'error',
|
|
30
|
+
} satisfies Partial<EslintTsFortressRules>,
|
|
31
|
+
},
|
|
32
|
+
];
|
|
33
|
+
```
|
|
34
|
+
|
|
35
|
+
## Rules
|
|
36
|
+
|
|
37
|
+
| Rule | Description |
|
|
38
|
+
| ------------------------ | ------------------------------------------------------- |
|
|
39
|
+
| `prefer-non-empty-array` | Replace `minLengthArray(1, …)` with `nonEmptyArray(…)`. |
|
|
40
|
+
|
|
41
|
+
### `prefer-non-empty-array`
|
|
42
|
+
|
|
43
|
+
`t.minLengthArray(1, x)` and `t.nonEmptyArray(x)` build the **same type** —
|
|
44
|
+
`NonEmptyArray<A>` is defined as `MinLengthArray<1, A>` — so the rewrite is
|
|
45
|
+
type-preserving. `nonEmptyArray` states the intent directly and, when no
|
|
46
|
+
explicit `typeName` is given, reports the clearer `NonEmptyArray<…>` instead of
|
|
47
|
+
`MinLengthArray<1, …>` in validation errors.
|
|
48
|
+
|
|
49
|
+
```ts
|
|
50
|
+
import * as t from 'ts-fortress';
|
|
51
|
+
|
|
52
|
+
// ❌
|
|
53
|
+
const Tags = t.minLengthArray(1, t.string());
|
|
54
|
+
|
|
55
|
+
// ✅
|
|
56
|
+
const Tags = t.nonEmptyArray(t.string());
|
|
57
|
+
```
|
|
58
|
+
|
|
59
|
+
Both the namespace style (`import * as t from 'ts-fortress'`) and named imports
|
|
60
|
+
— including aliases — are recognized, and the autofix reuses whatever binding
|
|
61
|
+
the file already has, adding `import { nonEmptyArray } from 'ts-fortress';`
|
|
62
|
+
only when a named call needs it.
|
|
63
|
+
|
|
64
|
+
The rule deliberately leaves alone:
|
|
65
|
+
|
|
66
|
+
- other minimum lengths (`minLengthArray(2, …)` is `MinLengthArray<2, …>`);
|
|
67
|
+
- `minLengthTuple(1, …)`, which produces the structural
|
|
68
|
+
`readonly [A, ...A[]]` tuple rather than the branded `NonEmptyArray`;
|
|
69
|
+
- calls passing a `defaultValue` option, because `nonEmptyArray` types it as
|
|
70
|
+
`NonEmptyTuple<A>` while `minLengthArray` types it as the branded
|
|
71
|
+
`MinLengthArray<1, A>` — a blind rewrite could stop type-checking. A
|
|
72
|
+
`typeName`-only options object is safe and is rewritten;
|
|
73
|
+
- files that already bind the name `nonEmptyArray` to something else.
|
|
74
|
+
|
|
75
|
+
## License
|
|
76
|
+
|
|
77
|
+
[Apache-2.0](./LICENSE)
|
package/dist/index.d.mts
ADDED
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
export { eslintPluginTsFortress } from './plugin.mjs';
|
|
2
|
+
export type { EslintTsFortressRules, EslintTsFortressRulesOption, } from './rule-types.mjs';
|
|
3
|
+
export { tsFortressRules } 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,sBAAsB,EAAE,MAAM,cAAc,CAAC;AACtD,YAAY,EACV,qBAAqB,EACrB,2BAA2B,GAC5B,MAAM,kBAAkB,CAAC;AAC1B,OAAO,EAAE,eAAe,EAAE,MAAM,mBAAmB,CAAC;AACpD,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,sBAAsB,EAAE,YAK3B,CAAC"}
|
package/dist/plugin.mjs
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"plugin.mjs","sources":["../src/plugin.mts"],"sourcesContent":["import { tsFortressRules } from './rules/index.mjs';\nimport { type ESLintPlugin } from './types.mjs';\n\nexport const eslintPluginTsFortress: ESLintPlugin = {\n meta: {\n name: 'eslint-plugin-ts-fortress',\n },\n rules: tsFortressRules,\n} as const;\n"],"names":[],"mappings":";;AAGO,MAAM,sBAAA,GAAuC;AAAA,EAClD,IAAA,EAAM;AAAA,IACJ,IAAA,EAAM;AAAA,GACR;AAAA,EACA,KAAA,EAAO;AACT;;;;"}
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import { type Linter } from 'eslint';
|
|
2
|
+
import { type TypeEq } from 'ts-type-forge';
|
|
3
|
+
import { type tsFortressRules } from './rules/index.mjs';
|
|
4
|
+
/**
|
|
5
|
+
* @description Replace `minLengthArray(1, ...)` with `nonEmptyArray(...)` from ts-fortress.
|
|
6
|
+
*
|
|
7
|
+
* ```md
|
|
8
|
+
* | key | value |
|
|
9
|
+
* | :--------- | :--------- |
|
|
10
|
+
* | type | suggestion |
|
|
11
|
+
* | deprecated | false |
|
|
12
|
+
* | fixable | code |
|
|
13
|
+
* ```
|
|
14
|
+
*/
|
|
15
|
+
declare namespace PreferNonEmptyArray {
|
|
16
|
+
type RuleEntry = Linter.StringSeverity;
|
|
17
|
+
}
|
|
18
|
+
export type EslintTsFortressRules = Readonly<{
|
|
19
|
+
'ts-fortress/prefer-non-empty-array': PreferNonEmptyArray.RuleEntry;
|
|
20
|
+
}>;
|
|
21
|
+
export type EslintTsFortressRulesOption = Readonly<Record<never, never>>;
|
|
22
|
+
type AssertTrue<B extends true> = B;
|
|
23
|
+
export type RuleNamesAreInSync = AssertTrue<TypeEq<keyof EslintTsFortressRules, `ts-fortress/${keyof typeof tsFortressRules}`>>;
|
|
24
|
+
export {};
|
|
25
|
+
//# 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,MAAM,EAAE,MAAM,QAAQ,CAAC;AACrC,OAAO,EAAE,KAAK,MAAM,EAAE,MAAM,eAAe,CAAC;AAC5C,OAAO,EAAE,KAAK,eAAe,EAAE,MAAM,mBAAmB,CAAC;AAEzD;;;;;;;;;;GAUG;AACH,kBAAU,mBAAmB,CAAC;IAC5B,KAAY,SAAS,GAAG,MAAM,CAAC,cAAc,CAAC;CAC/C;AAED,MAAM,MAAM,qBAAqB,GAAG,QAAQ,CAAC;IAC3C,oCAAoC,EAAE,mBAAmB,CAAC,SAAS,CAAC;CACrE,CAAC,CAAC;AAEH,MAAM,MAAM,2BAA2B,GAAG,QAAQ,CAAC,MAAM,CAAC,KAAK,EAAE,KAAK,CAAC,CAAC,CAAC;AAIzE,KAAK,UAAU,CAAC,CAAC,SAAS,IAAI,IAAI,CAAC,CAAC;AAEpC,MAAM,MAAM,kBAAkB,GAAG,UAAU,CACzC,MAAM,CACJ,MAAM,qBAAqB,EAC3B,eAAe,MAAM,OAAO,eAAe,EAAE,CAC9C,CACF,CAAC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"rule-types.mjs","sources":[],"sourcesContent":[],"names":[],"mappings":""}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"constants.d.mts","sourceRoot":"","sources":["../../src/rules/constants.mts"],"names":[],"mappings":"AAAA,eAAO,MAAM,kBAAkB,gBAAgB,CAAC;AAEhD,eAAO,MAAM,mBAAmB,mBAAmB,CAAC;AAEpD,eAAO,MAAM,kBAAkB,kBAAkB,CAAC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"constants.mjs","sources":["../../src/rules/constants.mts"],"sourcesContent":["export const TS_FORTRESS_MODULE = 'ts-fortress';\n\nexport const MIN_LENGTH_ARRAY_FN = 'minLengthArray';\n\nexport const NON_EMPTY_ARRAY_FN = 'nonEmptyArray';\n"],"names":[],"mappings":"AAAO,MAAM,kBAAA,GAAqB;AAE3B,MAAM,mBAAA,GAAsB;AAE5B,MAAM,kBAAA,GAAqB;;;;"}
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
import { type TSESLint, type TSESTree } from '@typescript-eslint/utils';
|
|
2
|
+
export type ResolvedCallee = Readonly<{
|
|
3
|
+
/** The canonical (imported) ts-fortress name, regardless of local alias. */
|
|
4
|
+
canonicalName: string;
|
|
5
|
+
/** The identifier node to rewrite when replacing the callee name. */
|
|
6
|
+
propertyNode: TSESTree.Node;
|
|
7
|
+
/** Whether the call was made through a namespace import (`t.foo(...)`). */
|
|
8
|
+
isNamespace: boolean;
|
|
9
|
+
}>;
|
|
10
|
+
/** Every `import ... from 'ts-fortress'` declaration in the file. */
|
|
11
|
+
export declare const getTsFortressImports: (program: TSESTree.Program) => readonly TSESTree.ImportDeclaration[];
|
|
12
|
+
/**
|
|
13
|
+
* Returns the in-scope local name a given ts-fortress export is imported as
|
|
14
|
+
* (handling aliases such as `import { nonEmptyArray as nea }` → `'nea'`), or
|
|
15
|
+
* `undefined` when it is not imported. Used so autofixes reference the binding
|
|
16
|
+
* that actually exists rather than the canonical name.
|
|
17
|
+
*/
|
|
18
|
+
export declare const getImportedLocalName: (importDeclarations: readonly TSESTree.ImportDeclaration[], importedName: string) => string | undefined;
|
|
19
|
+
/**
|
|
20
|
+
* Builds a resolver that maps a call expression's callee to the canonical
|
|
21
|
+
* ts-fortress function it refers to. Handles named imports (including aliases)
|
|
22
|
+
* and namespace imports (`import * as t` → `t.foo(...)`) — the latter being the
|
|
23
|
+
* idiomatic way to consume ts-fortress. Returns `undefined` for any callee that
|
|
24
|
+
* does not reference the given ts-fortress imports.
|
|
25
|
+
*/
|
|
26
|
+
export declare const buildCalleeResolver: (importDeclarations: readonly TSESTree.ImportDeclaration[]) => ((callee: TSESTree.Expression) => ResolvedCallee | undefined);
|
|
27
|
+
/**
|
|
28
|
+
* Whether the file already binds `name` to something that is not the
|
|
29
|
+
* ts-fortress export of the same name — a local declaration or an import from
|
|
30
|
+
* another module. Autofixes that introduce `name` must bail out in that case,
|
|
31
|
+
* because the rewritten call would resolve to the wrong binding.
|
|
32
|
+
*/
|
|
33
|
+
export declare const hasConflictingDeclaration: (program: TSESTree.Program, name: string) => boolean;
|
|
34
|
+
/**
|
|
35
|
+
* Inserts `import { <name> } from 'ts-fortress';` at the top of the file.
|
|
36
|
+
* Placement is deliberately naive: prettier-plugin-organize-imports (or any
|
|
37
|
+
* equivalent) merges and sorts it afterwards.
|
|
38
|
+
*/
|
|
39
|
+
export declare const buildImportFix: (fixer: TSESLint.RuleFixer, program: TSESTree.Program, name: string) => readonly TSESLint.RuleFix[];
|
|
40
|
+
//# 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,MAAM,MAAM,cAAc,GAAG,QAAQ,CAAC;IACpC,4EAA4E;IAC5E,aAAa,EAAE,MAAM,CAAC;IACtB,qEAAqE;IACrE,YAAY,EAAE,QAAQ,CAAC,IAAI,CAAC;IAC5B,2EAA2E;IAC3E,WAAW,EAAE,OAAO,CAAC;CACtB,CAAC,CAAC;AAEH,qEAAqE;AACrE,eAAO,MAAM,oBAAoB,YACtB,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;;;;;;GAMG;AACH,eAAO,MAAM,mBAAmB,uBACV,SAAS,QAAQ,CAAC,iBAAiB,EAAE,KACxD,CAAC,CAAC,MAAM,EAAE,QAAQ,CAAC,UAAU,KAAK,cAAc,GAAG,SAAS,CAkD9D,CAAC;AAEF;;;;;GAKG;AACH,eAAO,MAAM,yBAAyB,YAC3B,QAAQ,CAAC,OAAO,QACnB,MAAM,KACX,OAgBC,CAAC;AAEL;;;;GAIG;AACH,eAAO,MAAM,cAAc,UAClB,QAAQ,CAAC,SAAS,WAChB,QAAQ,CAAC,OAAO,QACnB,MAAM,KACX,SAAS,QAAQ,CAAC,OAAO,EAMhB,CAAC"}
|
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
import { AST_NODE_TYPES } from '@typescript-eslint/utils';
|
|
2
|
+
import { TS_FORTRESS_MODULE } from './constants.mjs';
|
|
3
|
+
|
|
4
|
+
const getTsFortressImports = (program) => program.body.filter(
|
|
5
|
+
(node) => node.type === AST_NODE_TYPES.ImportDeclaration && node.source.value === TS_FORTRESS_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 buildCalleeResolver = (importDeclarations) => {
|
|
11
|
+
const specifiers = importDeclarations.flatMap(
|
|
12
|
+
(declaration) => declaration.specifiers
|
|
13
|
+
);
|
|
14
|
+
const namespaceNames = new Set(
|
|
15
|
+
specifiers.filter(
|
|
16
|
+
(specifier) => specifier.type === AST_NODE_TYPES.ImportNamespaceSpecifier
|
|
17
|
+
).map((specifier) => specifier.local.name)
|
|
18
|
+
);
|
|
19
|
+
const localToCanonical = new Map(
|
|
20
|
+
specifiers.flatMap((specifier) => {
|
|
21
|
+
if (specifier.type !== AST_NODE_TYPES.ImportSpecifier) return [];
|
|
22
|
+
const importedName = specifier.imported.type === AST_NODE_TYPES.Identifier ? specifier.imported.name : specifier.imported.value;
|
|
23
|
+
return typeof importedName === "string" ? [[specifier.local.name, importedName]] : [];
|
|
24
|
+
})
|
|
25
|
+
);
|
|
26
|
+
return (callee) => {
|
|
27
|
+
if (callee.type === AST_NODE_TYPES.Identifier) {
|
|
28
|
+
const canonicalName = localToCanonical.get(callee.name);
|
|
29
|
+
return canonicalName === void 0 ? void 0 : { canonicalName, propertyNode: callee, isNamespace: false };
|
|
30
|
+
}
|
|
31
|
+
return callee.type === AST_NODE_TYPES.MemberExpression && !callee.computed && callee.object.type === AST_NODE_TYPES.Identifier && namespaceNames.has(callee.object.name) && callee.property.type === AST_NODE_TYPES.Identifier ? {
|
|
32
|
+
canonicalName: callee.property.name,
|
|
33
|
+
propertyNode: callee.property,
|
|
34
|
+
isNamespace: true
|
|
35
|
+
} : void 0;
|
|
36
|
+
};
|
|
37
|
+
};
|
|
38
|
+
const hasConflictingDeclaration = (program, name) => program.body.some((node) => {
|
|
39
|
+
if (node.type === AST_NODE_TYPES.ImportDeclaration) {
|
|
40
|
+
return node.source.value !== TS_FORTRESS_MODULE && node.specifiers.some((specifier) => specifier.local.name === name);
|
|
41
|
+
}
|
|
42
|
+
return declaresName(
|
|
43
|
+
node.type === AST_NODE_TYPES.ExportNamedDeclaration || node.type === AST_NODE_TYPES.ExportDefaultDeclaration ? node.declaration : node,
|
|
44
|
+
name
|
|
45
|
+
);
|
|
46
|
+
});
|
|
47
|
+
const buildImportFix = (fixer, program, name) => [
|
|
48
|
+
fixer.insertTextBefore(
|
|
49
|
+
program,
|
|
50
|
+
`import { ${name} } from '${TS_FORTRESS_MODULE}';
|
|
51
|
+
`
|
|
52
|
+
)
|
|
53
|
+
];
|
|
54
|
+
const declaresName = (node, name) => {
|
|
55
|
+
if (node === null || node === void 0) return false;
|
|
56
|
+
if (node.type === AST_NODE_TYPES.TSTypeAliasDeclaration || node.type === AST_NODE_TYPES.TSInterfaceDeclaration || node.type === AST_NODE_TYPES.TSEnumDeclaration) {
|
|
57
|
+
return node.id.name === name;
|
|
58
|
+
}
|
|
59
|
+
if (node.type === AST_NODE_TYPES.ClassDeclaration || node.type === AST_NODE_TYPES.FunctionDeclaration) {
|
|
60
|
+
return node.id?.name === name;
|
|
61
|
+
}
|
|
62
|
+
return node.type === AST_NODE_TYPES.VariableDeclaration && node.declarations.some(
|
|
63
|
+
(declarator) => declarator.id.type === AST_NODE_TYPES.Identifier && declarator.id.name === name
|
|
64
|
+
);
|
|
65
|
+
};
|
|
66
|
+
|
|
67
|
+
export { buildCalleeResolver, buildImportFix, getImportedLocalName, getTsFortressImports, hasConflictingDeclaration };
|
|
68
|
+
//# 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_FORTRESS_MODULE } from './constants.mjs';\n\n/* eslint-disable @typescript-eslint/prefer-readonly-parameter-types */\n\nexport type ResolvedCallee = Readonly<{\n /** The canonical (imported) ts-fortress name, regardless of local alias. */\n canonicalName: string;\n /** The identifier node to rewrite when replacing the callee name. */\n propertyNode: TSESTree.Node;\n /** Whether the call was made through a namespace import (`t.foo(...)`). */\n isNamespace: boolean;\n}>;\n\n/** Every `import ... from 'ts-fortress'` declaration in the file. */\nexport const getTsFortressImports = (\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_FORTRESS_MODULE,\n );\n\n/**\n * Returns the in-scope local name a given ts-fortress export is imported as\n * (handling aliases such as `import { nonEmptyArray as nea }` → `'nea'`), or\n * `undefined` when it is not imported. Used so autofixes reference the binding\n * 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 * Builds a resolver that maps a call expression's callee to the canonical\n * ts-fortress function it refers to. Handles named imports (including aliases)\n * and namespace imports (`import * as t` → `t.foo(...)`) — the latter being the\n * idiomatic way to consume ts-fortress. Returns `undefined` for any callee that\n * does not reference the given ts-fortress imports.\n */\nexport const buildCalleeResolver = (\n importDeclarations: readonly TSESTree.ImportDeclaration[],\n): ((callee: TSESTree.Expression) => ResolvedCallee | undefined) => {\n const specifiers = importDeclarations.flatMap(\n (declaration) => declaration.specifiers,\n );\n\n const namespaceNames = new Set(\n specifiers\n .filter(\n (specifier) =>\n specifier.type === AST_NODE_TYPES.ImportNamespaceSpecifier,\n )\n .map((specifier) => specifier.local.name),\n );\n\n const localToCanonical = new Map<string, string>(\n specifiers.flatMap((specifier) => {\n if (specifier.type !== AST_NODE_TYPES.ImportSpecifier) return [];\n\n const importedName =\n specifier.imported.type === AST_NODE_TYPES.Identifier\n ? specifier.imported.name\n : specifier.imported.value;\n\n return typeof importedName === 'string'\n ? [[specifier.local.name, importedName] as const]\n : [];\n }),\n );\n\n return (callee) => {\n if (callee.type === AST_NODE_TYPES.Identifier) {\n const canonicalName = localToCanonical.get(callee.name);\n\n return canonicalName === undefined\n ? undefined\n : { canonicalName, propertyNode: callee, isNamespace: false };\n }\n\n return callee.type === AST_NODE_TYPES.MemberExpression &&\n !callee.computed &&\n callee.object.type === AST_NODE_TYPES.Identifier &&\n namespaceNames.has(callee.object.name) &&\n callee.property.type === AST_NODE_TYPES.Identifier\n ? {\n canonicalName: callee.property.name,\n propertyNode: callee.property,\n isNamespace: true,\n }\n : undefined;\n };\n};\n\n/**\n * Whether the file already binds `name` to something that is not the\n * ts-fortress export of the same name — a local declaration or an import from\n * another module. Autofixes that introduce `name` must bail out in that case,\n * because the rewritten call would resolve to the wrong binding.\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_FORTRESS_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 { <name> } from 'ts-fortress';` at the top of the file.\n * Placement is deliberately naive: prettier-plugin-organize-imports (or any\n * equivalent) merges and sorts it afterwards.\n */\nexport const buildImportFix = (\n fixer: TSESLint.RuleFixer,\n program: TSESTree.Program,\n name: string,\n): readonly TSESLint.RuleFix[] =>\n [\n fixer.insertTextBefore(\n program,\n `import { ${name} } from '${TS_FORTRESS_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 if (\n node.type === AST_NODE_TYPES.ClassDeclaration ||\n node.type === AST_NODE_TYPES.FunctionDeclaration\n ) {\n return node.id?.name === name;\n }\n\n return (\n node.type === AST_NODE_TYPES.VariableDeclaration &&\n node.declarations.some(\n (declarator) =>\n declarator.id.type === AST_NODE_TYPES.Identifier &&\n declarator.id.name === name,\n )\n );\n};\n\n/* eslint-enable @typescript-eslint/prefer-readonly-parameter-types */\n"],"names":[],"mappings":"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"}
|
|
@@ -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,6 @@
|
|
|
1
|
+
import { type TSESLint } from '@typescript-eslint/utils';
|
|
2
|
+
type Options = readonly [];
|
|
3
|
+
type MessageIds = 'useNonEmptyArray';
|
|
4
|
+
export declare const preferNonEmptyArray: TSESLint.RuleModule<MessageIds, Options>;
|
|
5
|
+
export {};
|
|
6
|
+
//# 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,EAEL,KAAK,QAAQ,EAEd,MAAM,0BAA0B,CAAC;AAUlC,KAAK,OAAO,GAAG,SAAS,EAAE,CAAC;AAE3B,KAAK,UAAU,GAAG,kBAAkB,CAAC;AAErC,eAAO,MAAM,mBAAmB,EAAE,QAAQ,CAAC,UAAU,CAAC,UAAU,EAAE,OAAO,CAkH/D,CAAC"}
|
|
@@ -0,0 +1,100 @@
|
|
|
1
|
+
import { AST_NODE_TYPES } from '@typescript-eslint/utils';
|
|
2
|
+
import { NON_EMPTY_ARRAY_FN, MIN_LENGTH_ARRAY_FN } from './constants.mjs';
|
|
3
|
+
import { getTsFortressImports, buildCalleeResolver, getImportedLocalName, hasConflictingDeclaration, buildImportFix } from './import-utils.mjs';
|
|
4
|
+
|
|
5
|
+
const preferNonEmptyArray = {
|
|
6
|
+
meta: {
|
|
7
|
+
type: "suggestion",
|
|
8
|
+
docs: {
|
|
9
|
+
description: "Replace `minLengthArray(1, ...)` with `nonEmptyArray(...)` from ts-fortress."
|
|
10
|
+
},
|
|
11
|
+
fixable: "code",
|
|
12
|
+
schema: [],
|
|
13
|
+
messages: {
|
|
14
|
+
useNonEmptyArray: "Use `{{replacement}}(...)` instead of `{{original}}(1, ...)`: both build the same `NonEmptyArray` type, and `{{replacement}}` reports a clearer `typeName` on validation failure."
|
|
15
|
+
}
|
|
16
|
+
},
|
|
17
|
+
create: (context) => {
|
|
18
|
+
const sourceCode = context.sourceCode;
|
|
19
|
+
const program = sourceCode.ast;
|
|
20
|
+
const tsFortressImports = getTsFortressImports(program);
|
|
21
|
+
const resolveCallee = buildCalleeResolver(tsFortressImports);
|
|
22
|
+
const importedLocalName = getImportedLocalName(
|
|
23
|
+
tsFortressImports,
|
|
24
|
+
NON_EMPTY_ARRAY_FN
|
|
25
|
+
);
|
|
26
|
+
const mut_callsToFix = [];
|
|
27
|
+
return {
|
|
28
|
+
CallExpression: (node) => {
|
|
29
|
+
const resolved = resolveCallee(node.callee);
|
|
30
|
+
if (resolved === void 0) return;
|
|
31
|
+
if (resolved.canonicalName !== MIN_LENGTH_ARRAY_FN || !isRewritableMinLengthOneCall(node)) {
|
|
32
|
+
return;
|
|
33
|
+
}
|
|
34
|
+
mut_callsToFix.push({
|
|
35
|
+
node,
|
|
36
|
+
propertyNode: resolved.propertyNode,
|
|
37
|
+
isNamespace: resolved.isNamespace
|
|
38
|
+
});
|
|
39
|
+
},
|
|
40
|
+
"Program:exit": () => {
|
|
41
|
+
if (mut_callsToFix.length === 0) return;
|
|
42
|
+
const needsNamedBinding = mut_callsToFix.some(
|
|
43
|
+
(call) => !call.isNamespace
|
|
44
|
+
);
|
|
45
|
+
if (needsNamedBinding && importedLocalName === void 0 && hasConflictingDeclaration(program, NON_EMPTY_ARRAY_FN)) {
|
|
46
|
+
return;
|
|
47
|
+
}
|
|
48
|
+
const localName = importedLocalName ?? NON_EMPTY_ARRAY_FN;
|
|
49
|
+
const importFixIndex = needsNamedBinding && importedLocalName === void 0 ? mut_callsToFix.findIndex((call) => !call.isNamespace) : -1;
|
|
50
|
+
for (const [
|
|
51
|
+
index,
|
|
52
|
+
{ node, propertyNode, isNamespace }
|
|
53
|
+
] of mut_callsToFix.entries()) {
|
|
54
|
+
context.report({
|
|
55
|
+
node,
|
|
56
|
+
messageId: "useNonEmptyArray",
|
|
57
|
+
data: {
|
|
58
|
+
original: MIN_LENGTH_ARRAY_FN,
|
|
59
|
+
replacement: isNamespace ? NON_EMPTY_ARRAY_FN : localName
|
|
60
|
+
},
|
|
61
|
+
fix: (fixer) => [
|
|
62
|
+
...index === importFixIndex ? buildImportFix(fixer, program, NON_EMPTY_ARRAY_FN) : [],
|
|
63
|
+
fixer.replaceText(
|
|
64
|
+
propertyNode,
|
|
65
|
+
isNamespace ? NON_EMPTY_ARRAY_FN : localName
|
|
66
|
+
),
|
|
67
|
+
// Drop the leading `1` argument, from its start up to the start
|
|
68
|
+
// of the element-type argument, so any comments and whitespace
|
|
69
|
+
// in between go with it.
|
|
70
|
+
...dropFirstArgumentFix(fixer, node)
|
|
71
|
+
]
|
|
72
|
+
});
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
};
|
|
76
|
+
},
|
|
77
|
+
defaultOptions: []
|
|
78
|
+
};
|
|
79
|
+
const isRewritableMinLengthOneCall = (node) => {
|
|
80
|
+
const args = node.arguments;
|
|
81
|
+
if (args.length < 2 || args.length > 3) return false;
|
|
82
|
+
if (args.some((arg) => arg.type === AST_NODE_TYPES.SpreadElement)) {
|
|
83
|
+
return false;
|
|
84
|
+
}
|
|
85
|
+
const [minLength, , options] = args;
|
|
86
|
+
if (minLength?.type !== AST_NODE_TYPES.Literal || minLength.value !== 1 || node.typeArguments !== void 0) {
|
|
87
|
+
return false;
|
|
88
|
+
}
|
|
89
|
+
if (options === void 0) return true;
|
|
90
|
+
return options.type === AST_NODE_TYPES.ObjectExpression && options.properties.every(
|
|
91
|
+
(property) => property.type === AST_NODE_TYPES.Property && !property.computed && property.key.type === AST_NODE_TYPES.Identifier && property.key.name !== "defaultValue"
|
|
92
|
+
);
|
|
93
|
+
};
|
|
94
|
+
const dropFirstArgumentFix = (fixer, node) => {
|
|
95
|
+
const [minLength, elementType] = node.arguments;
|
|
96
|
+
return minLength === void 0 || elementType === void 0 ? [] : [fixer.removeRange([minLength.range[0], elementType.range[0]])];
|
|
97
|
+
};
|
|
98
|
+
|
|
99
|
+
export { preferNonEmptyArray };
|
|
100
|
+
//# sourceMappingURL=prefer-non-empty-array.mjs.map
|