/
extensions.js
177 lines (147 loc) · 4.73 KB
/
extensions.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
import path from 'path'
import resolve from 'eslint-module-utils/resolve'
import { isBuiltIn, isExternalModule, isScoped } from '../core/importType'
import docsUrl from '../docsUrl'
const enumValues = { enum: [ 'always', 'ignorePackages', 'never' ] }
const patternProperties = {
type: 'object',
patternProperties: { '.*': enumValues },
}
const properties = {
type: 'object',
properties: {
'pattern': patternProperties,
'ignorePackages': { type: 'boolean' },
},
}
function buildProperties(context) {
const result = {
defaultConfig: 'never',
pattern: {},
ignorePackages: false,
}
context.options.forEach(obj => {
// If this is a string, set defaultConfig to its value
if (typeof obj === 'string') {
result.defaultConfig = obj
return
}
// If this is not the new structure, transfer all props to result.pattern
if (obj.pattern === undefined && obj.ignorePackages === undefined) {
Object.assign(result.pattern, obj)
return
}
// If pattern is provided, transfer all props
if (obj.pattern !== undefined) {
Object.assign(result.pattern, obj.pattern)
}
// If ignorePackages is provided, transfer it to result
if (obj.ignorePackages !== undefined) {
result.ignorePackages = obj.ignorePackages
}
})
if (result.defaultConfig === 'ignorePackages') {
result.defaultConfig = 'always'
result.ignorePackages = true
}
return result
}
module.exports = {
meta: {
type: 'suggestion',
docs: {
url: docsUrl('extensions'),
},
schema: {
anyOf: [
{
type: 'array',
items: [enumValues],
additionalItems: false,
},
{
type: 'array',
items: [
enumValues,
properties,
],
additionalItems: false,
},
{
type: 'array',
items: [properties],
additionalItems: false,
},
{
type: 'array',
items: [patternProperties],
additionalItems: false,
},
{
type: 'array',
items: [
enumValues,
patternProperties,
],
additionalItems: false,
},
],
},
},
create: function (context) {
const props = buildProperties(context)
function getModifier(extension) {
return props.pattern[extension] || props.defaultConfig
}
function isUseOfExtensionRequired(extension, isPackage) {
return getModifier(extension) === 'always' && (!props.ignorePackages || !isPackage)
}
function isUseOfExtensionForbidden(extension) {
return getModifier(extension) === 'never'
}
function isResolvableWithoutExtension(file) {
const extension = path.extname(file)
const fileWithoutExtension = file.slice(0, -extension.length)
const resolvedFileWithoutExtension = resolve(fileWithoutExtension, context)
return resolvedFileWithoutExtension === resolve(file, context)
}
function checkFileExtension(node) {
const { source } = node
// bail if the declaration doesn't have a source, e.g. "export { foo };"
if (!source) return
const importPathWithQueryString = source.value
// don't enforce anything on builtins
if (isBuiltIn(importPathWithQueryString, context.settings)) return
const importPath = importPathWithQueryString.replace(/\?(.*)$/, '')
const resolvedPath = resolve(importPath, context)
// get extension from resolved path, if possible.
// for unresolved, use source value.
const extension = path.extname(resolvedPath || importPath).substring(1)
// determine if this is a module
const isPackage = isExternalModule(importPath, context.settings)
|| isScoped(importPath)
if (!extension || !importPath.endsWith(`.${extension}`)) {
const extensionRequired = isUseOfExtensionRequired(extension, isPackage)
const extensionForbidden = isUseOfExtensionForbidden(extension)
if (extensionRequired && !extensionForbidden) {
context.report({
node: source,
message:
`Missing file extension ${extension ? `"${extension}" ` : ''}for "${importPathWithQueryString}"`,
})
}
} else if (extension) {
if (isUseOfExtensionForbidden(extension) && isResolvableWithoutExtension(importPath)) {
context.report({
node: source,
message: `Unexpected use of file extension "${extension}" for "${importPathWithQueryString}"`,
})
}
}
}
return {
ImportDeclaration: checkFileExtension,
ExportNamedDeclaration: checkFileExtension,
}
},
}