Skip to content

Commit 901e77a

Browse files
committed
Add lint rule to caught custom DEFAULT_OPTIONS not overridable
1 parent 150ca6b commit 901e77a

1 file changed

Lines changed: 60 additions & 0 deletions

File tree

eslint.config.mjs

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,5 +28,65 @@ export default wdioEslint.config([
2828
'@typescript-eslint/no-require-imports': 'off',
2929
'@typescript-eslint/no-explicit-any': 'off'
3030
}
31+
},
32+
{
33+
files: ['src/**/*.ts'],
34+
plugins: {
35+
local: {
36+
rules: {
37+
'enforce-options-list': {
38+
create(context) {
39+
const fileName = context.filename || context.getFilename()
40+
const isConstantsFile = fileName.endsWith('src/constants.ts')
41+
const definedOptions = new Set()
42+
let listNode = null
43+
const listElements = new Set()
44+
45+
return {
46+
VariableDeclarator(node) {
47+
if (node.id.type === 'Identifier') {
48+
if (node.id.name.includes('DEFAULT_OPTIONS')) {
49+
if (isConstantsFile) {
50+
definedOptions.add(node.id.name)
51+
} else {
52+
context.report({
53+
node: node.id,
54+
message: `Option '${node.id.name}' must be included in 'src/constants.ts#defaultOptionsList', so it can be globally overridden.`
55+
})
56+
}
57+
}
58+
if (isConstantsFile && node.id.name === 'defaultOptionsList' && node.init && node.init.type === 'ArrayExpression') {
59+
listNode = node
60+
node.init.elements.forEach(el => {
61+
if (el && el.type === 'Identifier') {
62+
listElements.add(el.name)
63+
}
64+
})
65+
}
66+
}
67+
},
68+
'Program:exit'() {
69+
if (!listNode) {
70+
return
71+
}
72+
73+
definedOptions.forEach(opt => {
74+
if (!listElements.has(opt)) {
75+
context.report({
76+
node: listNode,
77+
message: `Option '${opt}' must be included in 'defaultOptionsList', so it can be globally overridden in 'src/constants.ts'.`
78+
})
79+
}
80+
})
81+
}
82+
}
83+
}
84+
}
85+
}
86+
}
87+
},
88+
rules: {
89+
'local/enforce-options-list': 'error'
90+
}
3191
}
3292
])

0 commit comments

Comments
 (0)