Skip to content

Commit 5bf0648

Browse files
authored
fix: allow additional schema types in require-meta-schema (#277)
1 parent e2f3deb commit 5bf0648

File tree

2 files changed

+63
-1
lines changed

2 files changed

+63
-1
lines changed

Diff for: lib/rules/require-meta-schema.js

+4-1
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,10 @@ module.exports = {
104104
hasEmptySchema = true;
105105
}
106106

107-
if (!['ArrayExpression', 'ObjectExpression'].includes(value.type)) {
107+
if (
108+
value.type === 'Literal' ||
109+
(value.type === 'Identifier' && value.name === 'undefined')
110+
) {
108111
context.report({ node: value, messageId: 'wrongType' });
109112
}
110113
},

Diff for: tests/lib/rules/require-meta-schema.js

+59
Original file line numberDiff line numberDiff line change
@@ -63,20 +63,57 @@ ruleTester.run('require-meta-schema', rule, {
6363
`,
6464
parserOptions: { sourceType: 'module' },
6565
},
66+
// Variable schema with array value.
6667
`
6768
const schema = [];
6869
module.exports = {
6970
meta: { schema },
7071
create(context) {}
7172
};
7273
`,
74+
// Variable schema with object value.
7375
`
7476
const foo = {};
7577
module.exports = {
7678
meta: { schema: foo },
7779
create(context) {}
7880
};
7981
`,
82+
// Variable schema with no static value.
83+
`
84+
module.exports = {
85+
meta: { schema },
86+
create(context) {}
87+
};
88+
`,
89+
// Variable schema pointing to unknown variable chain.
90+
`
91+
module.exports = {
92+
meta: { schema: baseRule.meta.schema },
93+
create(context) {}
94+
};
95+
`,
96+
// Schema with function call as value.
97+
`
98+
module.exports = {
99+
meta: { schema: getSchema() },
100+
create(context) {}
101+
};
102+
`,
103+
// Schema with ternary (conditional) expression.
104+
`
105+
module.exports = {
106+
meta: { schema: foo ? [] : {} },
107+
create(context) {}
108+
};
109+
`,
110+
// Schema with logical expression.
111+
`
112+
module.exports = {
113+
meta: { schema: foo || {} },
114+
create(context) {}
115+
};
116+
`,
80117
`
81118
let schema;
82119
schema = foo ? [] : {};
@@ -296,6 +333,28 @@ schema: [] },
296333
output: null,
297334
errors: [{ messageId: 'wrongType', type: 'Identifier', suggestions: [] }],
298335
},
336+
{
337+
// Schema with number literal value.
338+
code: `
339+
module.exports = {
340+
meta: { schema: 123 },
341+
create(context) {}
342+
};
343+
`,
344+
output: null,
345+
errors: [{ messageId: 'wrongType', type: 'Literal', suggestions: [] }],
346+
},
347+
{
348+
// Schema with string literal value.
349+
code: `
350+
module.exports = {
351+
meta: { schema: 'hello world' },
352+
create(context) {}
353+
};
354+
`,
355+
output: null,
356+
errors: [{ messageId: 'wrongType', type: 'Literal', suggestions: [] }],
357+
},
299358
{
300359
code: `
301360
const schema = null;

0 commit comments

Comments
 (0)