-
-
Notifications
You must be signed in to change notification settings - Fork 681
/
Copy pathno-invalid-meta-default-options.js
156 lines (137 loc) · 3.58 KB
/
no-invalid-meta-default-options.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
/**
* @fileoverview Internal rule to enforce valid default options.
* @author Flo Edelmann
*/
'use strict'
const Ajv = require('ajv')
const metaSchema = require('ajv/lib/refs/json-schema-draft-04.json')
// from https://github.com/eslint/eslint/blob/main/lib/shared/ajv.js
const ajv = new Ajv({
meta: false,
useDefaults: true,
validateSchema: false,
missingRefs: 'ignore',
verbose: true,
schemaId: 'auto'
})
ajv.addMetaSchema(metaSchema)
ajv._opts.defaultMeta = metaSchema.id
// from https://github.com/eslint/eslint/blob/main/lib/config/flat-config-helpers.js
const noOptionsSchema = Object.freeze({
type: 'array',
minItems: 0,
maxItems: 0
})
function getRuleOptionsSchema(schema) {
if (schema === false || typeof schema !== 'object' || schema === null) {
return null
}
if (!Array.isArray(schema)) {
return schema
}
if (schema.length === 0) {
return { ...noOptionsSchema }
}
return {
type: 'array',
items: schema,
minItems: 0,
maxItems: schema.length
}
}
/**
* @param {RuleContext} context
* @param {ASTNode} node
* @returns {any}
*/
function getNodeValue(context, node) {
try {
// eslint-disable-next-line no-eval
return eval(context.getSourceCode().getText(node))
} catch (error) {
return undefined
}
}
/**
* Gets the property of the Object node passed in that has the name specified.
*
* @param {string} propertyName Name of the property to return.
* @param {ASTNode} node The ObjectExpression node.
* @returns {ASTNode} The Property node or null if not found.
*/
function getPropertyFromObject(propertyName, node) {
if (node && node.type === 'ObjectExpression') {
for (const property of node.properties) {
if (property.type === 'Property' && property.key.name === propertyName) {
return property
}
}
}
return null
}
module.exports = {
meta: {
type: 'problem',
docs: {
description: 'enforce correct use of `meta` property in core rules',
categories: ['Internal']
},
schema: [],
messages: {
defaultOptionsNotMatchingSchema:
'Default options do not match the schema.'
}
},
create(context) {
/** @type {ASTNode} */
let exportsNode
return {
AssignmentExpression(node) {
if (
node.left &&
node.right &&
node.left.type === 'MemberExpression' &&
node.left.object.name === 'module' &&
node.left.property.name === 'exports'
) {
exportsNode = node.right
}
},
'Program:exit'() {
const metaProperty = getPropertyFromObject('meta', exportsNode)
if (!metaProperty) {
return
}
const metaSchema = getPropertyFromObject('schema', metaProperty.value)
const metaDefaultOptions = getPropertyFromObject(
'defaultOptions',
metaProperty.value
)
if (
!metaSchema ||
!metaDefaultOptions ||
metaDefaultOptions.value.type !== 'ArrayExpression'
) {
return
}
const defaultOptions = getNodeValue(context, metaDefaultOptions.value)
const schema = getNodeValue(context, metaSchema.value)
if (!defaultOptions || !schema) {
return
}
let validate
try {
validate = ajv.compile(getRuleOptionsSchema(schema))
} catch (error) {
return
}
if (!validate(defaultOptions)) {
context.report({
node: metaDefaultOptions.value,
messageId: 'defaultOptionsNotMatchingSchema'
})
}
}
}
}
}