-
-
Notifications
You must be signed in to change notification settings - Fork 681
/
Copy pathno-unsupported-features.js
143 lines (132 loc) · 3.88 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
/**
* @author Yosuke Ota
* See LICENSE file in root directory for full license.
*/
'use strict'
const { Range } = require('semver')
const utils = require('../utils')
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')
}
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 {Range|null} 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 Range(s)
} catch (_error) {
// Ignore parsing error.
}
cache.set(s, ret)
}
return ret
}
/**
* Merge two visitors.
* @param {Visitor} x The visitor which is assigned.
* @param {Visitor} y The visitor which is assigning.
* @returns {Visitor} `x`.
*/
function merge (x, y) {
for (const key of Object.keys(y)) {
if (typeof x[key] === 'function') {
if (x[key]._handlers == null) {
const fs = [x[key], y[key]]
x[key] = node => fs.forEach(h => h(node))
x[key]._handlers = fs
} else {
x[key]._handlers.push(y[key])
}
} else {
x[key] = y[key]
}
}
return x
}
module.exports = {
meta: {
type: 'suggestion',
docs: {
description: 'disallow unsupported Vue.js syntax on the specified version',
category: 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: Object.keys(FEATURES)
},
uniqueItems: true
}
},
additionalProperties: false
}
],
messages: {
// Vue.js 2.5.0+
forbiddenSlotScopeAttribute: '`slot-scope` are not supported until Vue.js "2.5.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".'
}
},
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 {{supported:string}} aCase The case object to check.
* @returns {boolean} `true` if it's supporting.
*/
function isNotSupportingVersion (aCase) {
if (typeof aCase.supported === 'function') {
return !aCase.supported(versionRange)
}
return versionRange.intersects(getSemverRange(`<${aCase.supported}`))
}
const templateBodyVisitor = Object.keys(FEATURES)
.filter(syntaxName => !ignores.includes(syntaxName))
.filter(syntaxName => isNotSupportingVersion(FEATURES[syntaxName]))
.reduce((result, syntaxName) => {
const visitor = FEATURES[syntaxName].createTemplateBodyVisitor(context)
if (visitor) {
merge(result, visitor)
}
return result
}, {})
return utils.defineTemplateBodyVisitor(context, templateBodyVisitor)
}
}