-
-
Notifications
You must be signed in to change notification settings - Fork 171
/
no-unpublished-import.js
274 lines (259 loc) · 8.14 KB
/
no-unpublished-import.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
/**
* @author Toru Nagashima
* See LICENSE file in root directory for full license.
*/
"use strict"
const path = require("path")
const { Linter, RuleTester } = require("eslint")
const rule = require("../../../lib/rules/no-unpublished-import")
const DynamicImportSupported = (() => {
const config = { parserOptions: { ecmaVersion: 2020 } }
const messages = new Linter().verify("import(s)", config)
return messages.length === 0
})()
if (!DynamicImportSupported) {
//eslint-disable-next-line no-console
console.warn(
"[%s] Skip tests for 'import()'",
path.basename(__filename, ".js")
)
}
/**
* Makes a file path to a fixture.
* @param {string} name - A name.
* @returns {string} A file path to a fixture.
*/
function fixture(name) {
return path.resolve(__dirname, "../../fixtures/no-unpublished", name)
}
const ruleTester = new RuleTester({
parserOptions: {
ecmaVersion: 2015,
sourceType: "module",
},
})
ruleTester.run("no-unpublished-import", rule, {
valid: [
{
filename: fixture("1/test.js"),
code: "import fs from 'fs';",
},
{
filename: fixture("1/test.js"),
code: "import aaa from 'aaa'; aaa();",
},
{
filename: fixture("1/test.js"),
code: "import c from 'aaa/a/b/c';",
},
{
filename: fixture("1/test.js"),
code: "import a from './a';",
},
{
filename: fixture("1/test.js"),
code: "import a from './a.js';",
},
{
filename: fixture("2/ignore1.js"),
code: "import test from './test';",
},
{
filename: fixture("2/ignore1.js"),
code: "import bbb from 'bbb';",
},
{
filename: fixture("2/ignore1.js"),
code: "import c from 'bbb/a/b/c';",
},
{
filename: fixture("2/ignore1.js"),
code: "import ignore2 from './ignore2';",
},
{
filename: fixture("3/test.js"),
code: "import a from './pub/a';",
},
{
filename: fixture("3/test.js"),
code: "import test2 from './test2';",
},
{
filename: fixture("3/test.js"),
code: "import aaa from 'aaa';",
},
{
filename: fixture("3/test.js"),
code: "import bbb from 'bbb';",
},
{
filename: fixture("3/pub/ignore1.js"),
code: "import bbb from 'bbb';",
},
{
filename: fixture("3/pub/test.js"),
code: "import p from '../package.json';",
},
{
filename: fixture("3/src/pub/test.js"),
code: "import bbb from 'bbb';",
},
{
filename: fixture("3/src/pub/test.js"),
code: "import bbb from 'bbb!foo?a=b&c=d';",
},
// Ignores it if the filename is unknown.
"import noExistPackage0 from 'no-exist-package-0';",
"import b from './b';",
// Should work fine if the filename is relative.
{
filename: "tests/fixtures/no-unpublished/2/test.js",
code: "import aaa from 'aaa';",
},
{
filename: "tests/fixtures/no-unpublished/2/test.js",
code: "import a from './a';",
},
{
filename: fixture("1/test.js"),
code: "import electron from 'electron';",
options: [{ allowModules: ["electron"] }],
},
// Auto-published files only apply to root package directory
{
filename: fixture("3/src/readme.js"),
code: "import bbb from 'bbb';",
env: { node: true },
},
// Negative patterns in files field.
{
filename: fixture("negative-in-files/lib/__test__/index.js"),
code: "import bbb from 'bbb';",
},
],
invalid: [
{
filename: fixture("2/test.js"),
code: "import ignore1 from './ignore1.js';",
errors: ['"./ignore1.js" is not published.'],
},
{
filename: fixture("2/test.js"),
code: "import ignore1 from './ignore1';",
errors: ['"./ignore1" is not published.'],
},
{
filename: fixture("3/pub/test.js"),
code: "import bbb from 'bbb';",
errors: ['"bbb" is not published.'],
},
{
filename: fixture("3/pub/test.js"),
code: "import ignore1 from './ignore1';",
errors: ['"./ignore1" is not published.'],
},
{
filename: fixture("3/pub/test.js"),
code: "import abc from './abc';",
errors: ['"./abc" is not published.'],
},
{
filename: fixture("3/pub/test.js"),
code: "import test from '../test';",
errors: ['"../test" is not published.'],
},
{
filename: fixture("3/pub/test.js"),
code: "import a from '../src/pub/a.js';",
errors: ['"../src/pub/a.js" is not published.'],
},
{
filename: fixture("1/test.js"),
code: "import a from '../a.js';",
errors: ['"../a.js" is not published.'],
},
// Should work fine if the filename is relative.
{
filename: "tests/fixtures/no-unpublished/2/test.js",
code: "import ignore1 from './ignore1';",
errors: ['"./ignore1" is not published.'],
},
// `convertPath` option.
{
filename: fixture("3/src/test.jsx"),
code: "import a from '../test';",
errors: ['"../test" is not published.'],
settings: {
node: {
convertPath: {
"src/**/*.jsx": ["src/(.+?)\\.jsx", "pub/$1.js"],
},
tryExtensions: [".js", ".jsx", ".json"],
},
},
},
{
filename: fixture("3/src/test.jsx"),
code: "import a from '../test';",
options: [
{
convertPath: {
"src/**/*.jsx": ["src/(.+?)\\.jsx", "pub/$1.js"],
},
tryExtensions: [".js", ".jsx", ".json"],
},
],
errors: ['"../test" is not published.'],
},
{
filename: fixture("3/src/test.jsx"),
code: "import a from '../test';",
errors: ['"../test" is not published.'],
settings: {
node: {
convertPath: [
{
include: ["src/**/*.jsx"],
replace: ["src/(.+?)\\.jsx", "pub/$1.js"],
},
],
tryExtensions: [".js", ".jsx", ".json"],
},
},
},
{
filename: fixture("3/src/test.jsx"),
code: "import a from '../test';",
options: [
{
convertPath: [
{
include: ["src/**/*.jsx"],
replace: ["src/(.+?)\\.jsx", "pub/$1.js"],
},
],
tryExtensions: [".js", ".jsx", ".json"],
},
],
errors: ['"../test" is not published.'],
},
// outside of the package.
{
filename: fixture("1/test.js"),
code: "import a from '../2/a.js';",
env: { node: true },
errors: ['"../2/a.js" is not published.'],
},
// import()
...(DynamicImportSupported
? [
{
filename: fixture("2/test.js"),
code: "function f() { import('./ignore1.js') }",
parserOptions: { ecmaVersion: 2020 },
errors: ['"./ignore1.js" is not published.'],
},
]
: []),
],
})