Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow additional types of dynamic schemas in require-meta-schema rule #277

Merged
merged 1 commit into from
Aug 4, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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')
) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

just wondering we can explicitly say what we want to check - as more expressions could be added in future es.

e.g some new added expressions:

  • ChainExpression
  • LogicalAssignmentExpression

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah I'm worried about this too. I think I should update this to instead only check a few common invalid types like Literal.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Switched to only checking for literals / null / undefined to be safe.

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