-
-
Notifications
You must be signed in to change notification settings - Fork 681
/
Copy pathno-unsupported-features.js
373 lines (339 loc) · 10.8 KB
/
no-unsupported-features.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
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
/**
* @author Yosuke Ota
* See LICENSE file in root directory for full license.
*/
'use strict'
const semver = require('semver')
const utils = require('../utils')
/**
* @typedef {object} SyntaxRule
* @property {string} supported
* @property { (context: RuleContext) => TemplateListener } [createTemplateBodyVisitor]
* @property { (context: RuleContext) => RuleListener } [createScriptVisitor]
*/
const FEATURES = {
// Vue.js 2.5.0+
'slot-scope-attribute': require('./syntaxes/slot-scope-attribute'),
// Vue.js 2.6.0+
'dynamic-directive-arguments': require('./syntaxes/dynamic-directive-arguments'),
'v-slot': require('./syntaxes/v-slot'),
// >=2.6.0-beta.1 <=2.6.0-beta.3
'v-bind-prop-modifier-shorthand': require('./syntaxes/v-bind-prop-modifier-shorthand'),
// Vue.js 3.0.0+
'v-model-argument': require('./syntaxes/v-model-argument'),
'v-model-custom-modifiers': require('./syntaxes/v-model-custom-modifiers'),
'v-is': require('./syntaxes/v-is'),
'script-setup': require('./syntaxes/script-setup'),
'style-css-vars-injection': require('./syntaxes/style-css-vars-injection'),
// Vue.js 3.1.0+
'is-attribute-with-vue-prefix': require('./syntaxes/is-attribute-with-vue-prefix')
}
const SYNTAX_NAMES = /** @type {(keyof FEATURES)[]} */ (Object.keys(FEATURES))
const cache = new Map()
/**
* Get the `semver.Range` object of a given range text.
* @param {string} x The text expression for a semver range.
* @returns {semver.Range} The range object of a given range text.
* It's null if the `x` is not a valid range text.
*/
function getSemverRange(x) {
const s = String(x)
let ret = cache.get(s) || null
if (!ret) {
try {
ret = new semver.Range(s)
} catch (_error) {
// Ignore parsing error.
}
cache.set(s, ret)
}
return ret
}
module.exports = {
meta: {
type: 'suggestion',
docs: {
description:
'disallow unsupported Vue.js syntax on the specified version',
categories: undefined,
url: 'https://eslint.vuejs.org/rules/no-unsupported-features.html'
},
fixable: 'code',
schema: [
{
type: 'object',
properties: {
version: {
type: 'string'
},
ignores: {
type: 'array',
items: {
enum: SYNTAX_NAMES
},
uniqueItems: true
}
},
additionalProperties: false
}
],
messages: {
// Vue.js 2.5.0+
forbiddenSlotScopeAttribute:
'`slot-scope` are not supported except Vue.js ">=2.5.0 <3.0.0".',
// Vue.js 2.6.0+
forbiddenDynamicDirectiveArguments:
'Dynamic arguments are not supported until Vue.js "2.6.0".',
forbiddenVSlot: '`v-slot` are not supported until Vue.js "2.6.0".',
// >=2.6.0-beta.1 <=2.6.0-beta.3
forbiddenVBindPropModifierShorthand:
'`.prop` shorthand are not supported except Vue.js ">=2.6.0-beta.1 <=2.6.0-beta.3".',
// Vue.js 3.0.0+
forbiddenVModelArgument:
'Argument on `v-model` is not supported until Vue.js "3.0.0".',
forbiddenVModelCustomModifiers:
'Custom modifiers on `v-model` are not supported until Vue.js "3.0.0".',
forbiddenVIs: '`v-is` are not supported until Vue.js "3.0.0".',
forbiddenScriptSetup:
'`<script setup>` are not supported until Vue.js "3.0.0".',
forbiddenStyleCssVarsInjection:
'SFC CSS variable injection is not supported until Vue.js "3.0.3".',
// Vue.js 3.1.0+
forbiddenIsAttributeWithVuePrefix:
'`is="vue:"` are not supported until Vue.js "3.1.0".'
}
},
/** @param {RuleContext} context */
create(context) {
const { version, ignores } = Object.assign(
{
version: null,
ignores: []
},
context.options[0] || {}
)
if (!version) {
// version is not set.
return {}
}
const versionRange = getSemverRange(version)
/**
* Check whether a given case object is full-supported on the configured node version.
* @param {SyntaxRule} aCase The case object to check.
* @returns {boolean} `true` if it's supporting.
*/
function isNotSupportingVersion(aCase) {
return !semverSubset(versionRange, getSemverRange(aCase.supported))
}
/** @type {TemplateListener} */
let templateBodyVisitor = {}
/** @type {RuleListener} */
let scriptVisitor = {}
for (const syntaxName of SYNTAX_NAMES) {
/** @type {SyntaxRule} */
const syntax = FEATURES[syntaxName]
if (ignores.includes(syntaxName) || !isNotSupportingVersion(syntax)) {
continue
}
if (syntax.createTemplateBodyVisitor) {
const visitor = syntax.createTemplateBodyVisitor(context)
templateBodyVisitor = utils.compositingVisitors(
templateBodyVisitor,
visitor
)
}
if (syntax.createScriptVisitor) {
const visitor = syntax.createScriptVisitor(context)
scriptVisitor = utils.compositingVisitors(scriptVisitor, visitor)
}
}
return utils.defineTemplateBodyVisitor(
context,
templateBodyVisitor,
scriptVisitor
)
}
}
// TODO replace semver.subset() in the major version.
/**
* semver.subset()
*
* We need to use a copy of the semver source code until a major version upgrade.
*
* @see https://github.com/npm/node-semver/blob/e79ac3a450e8bb504e78b8159e3efc70895699b8/ranges/subset.js#L43
* @license ISC at Isaac Z. Schlueter and Contributors
* https://github.com/npm/node-semver/blob/master/LICENSE
*
* @param {semver.Range} sub
* @param {semver.Range} dom
*/
function semverSubset(sub, dom) {
if (sub === dom) return true
sub = new semver.Range(sub)
dom = new semver.Range(dom)
let sawNonNull = false
// eslint-disable-next-line no-labels
OUTER: for (const simpleSub of sub.set) {
for (const simpleDom of dom.set) {
const isSub = simpleSubset(simpleSub, simpleDom)
sawNonNull = sawNonNull || isSub !== null
// eslint-disable-next-line no-labels
if (isSub) continue OUTER
}
if (sawNonNull) return false
}
return true
}
/**
* @license ISC at Isaac Z. Schlueter and Contributors
* https://github.com/npm/node-semver/blob/master/LICENSE
* @param {readonly semver.Comparator[]} sub
* @param {readonly semver.Comparator[]} dom
*/
function simpleSubset(sub, dom) {
if (sub === dom) return true
/**
* @param {semver.Comparator} c
*/
function isAny(c) {
return Object.keys(c.semver).length === 0
}
if (sub.length === 1 && isAny(sub[0])) {
if (dom.length === 1 && isAny(dom[0])) return true
else sub = [new semver.Comparator('>=0.0.0')]
}
if (dom.length === 1 && isAny(dom[0])) {
dom = [new semver.Comparator('>=0.0.0')]
}
const eqSet = new Set()
let gt, lt
for (const c of sub) {
if (c.operator === '>' || c.operator === '>=') gt = higherGT(gt, c)
else if (c.operator === '<' || c.operator === '<=') lt = lowerLT(lt, c)
else eqSet.add(c.semver)
}
if (eqSet.size > 1) return null
let gtltComp
if (gt && lt) {
gtltComp = semver.compare(gt.semver, lt.semver)
if (gtltComp > 0) return null
else if (gtltComp === 0 && (gt.operator !== '>=' || lt.operator !== '<='))
return null
}
// will iterate one or zero times
for (const eq of eqSet) {
if (gt && !semver.satisfies(eq, String(gt))) return null
if (lt && !semver.satisfies(eq, String(lt))) return null
for (const c of dom) {
if (!semver.satisfies(eq, String(c))) return false
}
return true
}
let higher, lower
let hasDomLT, hasDomGT
// if the subset has a prerelease, we need a comparator in the superset
// with the same tuple and a prerelease, or it's not a subset
let needDomLTPre = lt && lt.semver.prerelease.length ? lt.semver : false
let needDomGTPre = gt && gt.semver.prerelease.length ? gt.semver : false
// exception: <1.2.3-0 is the same as <1.2.3
if (
needDomLTPre &&
needDomLTPre.prerelease.length === 1 &&
lt &&
lt.operator === '<' &&
needDomLTPre.prerelease[0] === 0
) {
needDomLTPre = false
}
for (const c of dom) {
hasDomGT = hasDomGT || c.operator === '>' || c.operator === '>='
hasDomLT = hasDomLT || c.operator === '<' || c.operator === '<='
if (gt) {
if (needDomGTPre) {
if (
c.semver.prerelease &&
c.semver.prerelease.length &&
c.semver.major === needDomGTPre.major &&
c.semver.minor === needDomGTPre.minor &&
c.semver.patch === needDomGTPre.patch
) {
needDomGTPre = false
}
}
if (c.operator === '>' || c.operator === '>=') {
higher = higherGT(gt, c)
if (higher === c && higher !== gt) return false
} else if (
gt.operator === '>=' &&
!semver.satisfies(gt.semver, String(c))
)
return false
}
if (lt) {
if (needDomLTPre) {
if (
c.semver.prerelease &&
c.semver.prerelease.length &&
c.semver.major === needDomLTPre.major &&
c.semver.minor === needDomLTPre.minor &&
c.semver.patch === needDomLTPre.patch
) {
needDomLTPre = false
}
}
if (c.operator === '<' || c.operator === '<=') {
lower = lowerLT(lt, c)
if (lower === c && lower !== lt) return false
} else if (
lt.operator === '<=' &&
!semver.satisfies(lt.semver, String(c))
)
return false
}
if (!c.operator && (lt || gt) && gtltComp !== 0) return false
}
// if there was a < or >, and nothing in the dom, then must be false
// UNLESS it was limited by another range in the other direction.
// Eg, >1.0.0 <1.0.1 is still a subset of <2.0.0
if (gt && hasDomLT && !lt && gtltComp !== 0) return false
if (lt && hasDomGT && !gt && gtltComp !== 0) return false
// we needed a prerelease range in a specific tuple, but didn't get one
// then this isn't a subset. eg >=1.2.3-pre is not a subset of >=1.0.0,
// because it includes prereleases in the 1.2.3 tuple
if (needDomGTPre || needDomLTPre) return false
return true
}
/**
* @license ISC at Isaac Z. Schlueter and Contributors
* https://github.com/npm/node-semver/blob/master/LICENSE
* @param {semver.Comparator | void} a
* @param {semver.Comparator} b
*/
const higherGT = (a, b) => {
if (!a) return b
const comp = semver.compare(a.semver, b.semver)
return comp > 0
? a
: comp < 0
? b
: b.operator === '>' && a.operator === '>='
? b
: a
}
/**
* @license ISC at Isaac Z. Schlueter and Contributors
* https://github.com/npm/node-semver/blob/master/LICENSE
* @param {semver.Comparator | void} a
* @param {semver.Comparator} b
*/
const lowerLT = (a, b) => {
if (!a) return b
const comp = semver.compare(a.semver, b.semver)
return comp < 0
? a
: comp > 0
? b
: b.operator === '<' && a.operator === '<='
? b
: a
}