/
no-useless-path-segments.js
218 lines (208 loc) · 8.54 KB
/
no-useless-path-segments.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
import { test } from '../utils'
import { RuleTester } from 'eslint'
const ruleTester = new RuleTester()
const rule = require('rules/no-useless-path-segments')
function runResolverTests(resolver) {
ruleTester.run(`no-useless-path-segments (${resolver})`, rule, {
valid: [
// CommonJS modules with default options
test({ code: 'require("./../files/malformed.js")' }),
// ES modules with default options
test({ code: 'import "./malformed.js"' }),
test({ code: 'import "./test-module"' }),
test({ code: 'import "./bar/"' }),
test({ code: 'import "."' }),
test({ code: 'import ".."' }),
test({ code: 'import fs from "fs"' }),
// ES modules + noUselessIndex
test({ code: 'import "../index"' }), // noUselessIndex is false by default
test({ code: 'import "../my-custom-index"', options: [{ noUselessIndex: true }] }),
test({ code: 'import "./bar.js"', options: [{ noUselessIndex: true }] }), // ./bar/index.js exists
test({ code: 'import "./bar"', options: [{ noUselessIndex: true }] }),
test({ code: 'import "./bar/"', options: [{ noUselessIndex: true }] }), // ./bar.js exists
test({ code: 'import "./malformed.js"', options: [{ noUselessIndex: true }] }), // ./malformed directory does not exist
test({ code: 'import "./malformed"', options: [{ noUselessIndex: true }] }), // ./malformed directory does not exist
test({ code: 'import "./importType"', options: [{ noUselessIndex: true }] }), // ./importType.js does not exist
test({ code: 'import(".")'
, parser: 'babel-eslint' }),
test({ code: 'import("..")'
, parser: 'babel-eslint' }),
test({ code: 'import("fs").then(function(fs){})'
, parser: 'babel-eslint' }),
],
invalid: [
// CommonJS modules
test({
code: 'require("./../files/malformed.js")',
options: [{ commonjs: true }],
errors: [ 'Useless path segments for "./../files/malformed.js", should be "../files/malformed.js"'],
}),
test({
code: 'require("./../files/malformed")',
options: [{ commonjs: true }],
errors: [ 'Useless path segments for "./../files/malformed", should be "../files/malformed"'],
}),
test({
code: 'require("../files/malformed.js")',
options: [{ commonjs: true }],
errors: [ 'Useless path segments for "../files/malformed.js", should be "./malformed.js"'],
}),
test({
code: 'require("../files/malformed")',
options: [{ commonjs: true }],
errors: [ 'Useless path segments for "../files/malformed", should be "./malformed"'],
}),
test({
code: 'require("./test-module/")',
options: [{ commonjs: true }],
errors: [ 'Useless path segments for "./test-module/", should be "./test-module"'],
}),
test({
code: 'require("./")',
options: [{ commonjs: true }],
errors: [ 'Useless path segments for "./", should be "."'],
}),
test({
code: 'require("../")',
options: [{ commonjs: true }],
errors: [ 'Useless path segments for "../", should be ".."'],
}),
test({
code: 'require("./deep//a")',
options: [{ commonjs: true }],
errors: [ 'Useless path segments for "./deep//a", should be "./deep/a"'],
}),
// CommonJS modules + noUselessIndex
test({
code: 'require("./bar/index.js")',
options: [{ commonjs: true, noUselessIndex: true }],
errors: ['Useless path segments for "./bar/index.js", should be "./bar/"'], // ./bar.js exists
}),
test({
code: 'require("./bar/index")',
options: [{ commonjs: true, noUselessIndex: true }],
errors: ['Useless path segments for "./bar/index", should be "./bar/"'], // ./bar.js exists
}),
test({
code: 'require("./importPath/")',
options: [{ commonjs: true, noUselessIndex: true }],
errors: ['Useless path segments for "./importPath/", should be "./importPath"'], // ./importPath.js does not exist
}),
test({
code: 'require("./importPath/index.js")',
options: [{ commonjs: true, noUselessIndex: true }],
errors: ['Useless path segments for "./importPath/index.js", should be "./importPath"'], // ./importPath.js does not exist
}),
test({
code: 'require("./importType/index")',
options: [{ commonjs: true, noUselessIndex: true }],
errors: ['Useless path segments for "./importType/index", should be "./importType"'], // ./importPath.js does not exist
}),
test({
code: 'require("./index")',
options: [{ commonjs: true, noUselessIndex: true }],
errors: ['Useless path segments for "./index", should be "."'],
}),
test({
code: 'require("../index")',
options: [{ commonjs: true, noUselessIndex: true }],
errors: ['Useless path segments for "../index", should be ".."'],
}),
test({
code: 'require("../index.js")',
options: [{ commonjs: true, noUselessIndex: true }],
errors: ['Useless path segments for "../index.js", should be ".."'],
}),
// ES modules
test({
code: 'import "./../files/malformed.js"',
errors: [ 'Useless path segments for "./../files/malformed.js", should be "../files/malformed.js"'],
}),
test({
code: 'import "./../files/malformed"',
errors: [ 'Useless path segments for "./../files/malformed", should be "../files/malformed"'],
}),
test({
code: 'import "../files/malformed.js"',
errors: [ 'Useless path segments for "../files/malformed.js", should be "./malformed.js"'],
}),
test({
code: 'import "../files/malformed"',
errors: [ 'Useless path segments for "../files/malformed", should be "./malformed"'],
}),
test({
code: 'import "./test-module/"',
errors: [ 'Useless path segments for "./test-module/", should be "./test-module"'],
}),
test({
code: 'import "./"',
errors: [ 'Useless path segments for "./", should be "."'],
}),
test({
code: 'import "../"',
errors: [ 'Useless path segments for "../", should be ".."'],
}),
test({
code: 'import "./deep//a"',
errors: [ 'Useless path segments for "./deep//a", should be "./deep/a"'],
}),
// ES modules + noUselessIndex
test({
code: 'import "./bar/index.js"',
options: [{ noUselessIndex: true }],
errors: ['Useless path segments for "./bar/index.js", should be "./bar/"'], // ./bar.js exists
}),
test({
code: 'import "./bar/index"',
options: [{ noUselessIndex: true }],
errors: ['Useless path segments for "./bar/index", should be "./bar/"'], // ./bar.js exists
}),
test({
code: 'import "./importPath/"',
options: [{ noUselessIndex: true }],
errors: ['Useless path segments for "./importPath/", should be "./importPath"'], // ./importPath.js does not exist
}),
test({
code: 'import "./importPath/index.js"',
options: [{ noUselessIndex: true }],
errors: ['Useless path segments for "./importPath/index.js", should be "./importPath"'], // ./importPath.js does not exist
}),
test({
code: 'import "./importPath/index"',
options: [{ noUselessIndex: true }],
errors: ['Useless path segments for "./importPath/index", should be "./importPath"'], // ./importPath.js does not exist
}),
test({
code: 'import "./index"',
options: [{ noUselessIndex: true }],
errors: ['Useless path segments for "./index", should be "."'],
}),
test({
code: 'import "../index"',
options: [{ noUselessIndex: true }],
errors: ['Useless path segments for "../index", should be ".."'],
}),
test({
code: 'import "../index.js"',
options: [{ noUselessIndex: true }],
errors: ['Useless path segments for "../index.js", should be ".."'],
}),
test({
code: 'import("./")',
errors: [ 'Useless path segments for "./", should be "."'],
parser: 'babel-eslint',
}),
test({
code: 'import("../")',
errors: [ 'Useless path segments for "../", should be ".."'],
parser: 'babel-eslint',
}),
test({
code: 'import("./deep//a")',
errors: [ 'Useless path segments for "./deep//a", should be "./deep/a"'],
parser: 'babel-eslint',
}),
],
})
}
['node', 'webpack'].forEach(runResolverTests)