Skip to content

Commit

Permalink
fix: allow additional schema types in require-meta-schema (#277)
Browse files Browse the repository at this point in the history
  • Loading branch information
bmish committed Aug 4, 2022
1 parent e2f3deb commit 5bf0648
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 1 deletion.
5 changes: 4 additions & 1 deletion lib/rules/require-meta-schema.js
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,10 @@ module.exports = {
hasEmptySchema = true;
}

if (!['ArrayExpression', 'ObjectExpression'].includes(value.type)) {
if (
value.type === 'Literal' ||
(value.type === 'Identifier' && value.name === 'undefined')
) {
context.report({ node: value, messageId: 'wrongType' });
}
},
Expand Down
59 changes: 59 additions & 0 deletions tests/lib/rules/require-meta-schema.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,20 +63,57 @@ ruleTester.run('require-meta-schema', rule, {
`,
parserOptions: { sourceType: 'module' },
},
// Variable schema with array value.
`
const schema = [];
module.exports = {
meta: { schema },
create(context) {}
};
`,
// Variable schema with object value.
`
const foo = {};
module.exports = {
meta: { schema: foo },
create(context) {}
};
`,
// Variable schema with no static value.
`
module.exports = {
meta: { schema },
create(context) {}
};
`,
// Variable schema pointing to unknown variable chain.
`
module.exports = {
meta: { schema: baseRule.meta.schema },
create(context) {}
};
`,
// Schema with function call as value.
`
module.exports = {
meta: { schema: getSchema() },
create(context) {}
};
`,
// Schema with ternary (conditional) expression.
`
module.exports = {
meta: { schema: foo ? [] : {} },
create(context) {}
};
`,
// Schema with logical expression.
`
module.exports = {
meta: { schema: foo || {} },
create(context) {}
};
`,
`
let schema;
schema = foo ? [] : {};
Expand Down Expand Up @@ -296,6 +333,28 @@ schema: [] },
output: null,
errors: [{ messageId: 'wrongType', type: 'Identifier', suggestions: [] }],
},
{
// Schema with number literal value.
code: `
module.exports = {
meta: { schema: 123 },
create(context) {}
};
`,
output: null,
errors: [{ messageId: 'wrongType', type: 'Literal', suggestions: [] }],
},
{
// Schema with string literal value.
code: `
module.exports = {
meta: { schema: 'hello world' },
create(context) {}
};
`,
output: null,
errors: [{ messageId: 'wrongType', type: 'Literal', suggestions: [] }],
},
{
code: `
const schema = null;
Expand Down

0 comments on commit 5bf0648

Please sign in to comment.