From 5bf0648f50213fa30e9b623a8db376f41a3af411 Mon Sep 17 00:00:00 2001 From: Bryan Mishkin <698306+bmish@users.noreply.github.com> Date: Wed, 3 Aug 2022 23:40:43 -0400 Subject: [PATCH] fix: allow additional schema types in require-meta-schema (#277) --- lib/rules/require-meta-schema.js | 5 ++- tests/lib/rules/require-meta-schema.js | 59 ++++++++++++++++++++++++++ 2 files changed, 63 insertions(+), 1 deletion(-) diff --git a/lib/rules/require-meta-schema.js b/lib/rules/require-meta-schema.js index 0789bc6a..2d0d3000 100644 --- a/lib/rules/require-meta-schema.js +++ b/lib/rules/require-meta-schema.js @@ -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' }); } }, diff --git a/tests/lib/rules/require-meta-schema.js b/tests/lib/rules/require-meta-schema.js index a0c2a4d0..8558c7d7 100644 --- a/tests/lib/rules/require-meta-schema.js +++ b/tests/lib/rules/require-meta-schema.js @@ -63,6 +63,7 @@ ruleTester.run('require-meta-schema', rule, { `, parserOptions: { sourceType: 'module' }, }, + // Variable schema with array value. ` const schema = []; module.exports = { @@ -70,6 +71,7 @@ ruleTester.run('require-meta-schema', rule, { create(context) {} }; `, + // Variable schema with object value. ` const foo = {}; module.exports = { @@ -77,6 +79,41 @@ ruleTester.run('require-meta-schema', rule, { 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 ? [] : {}; @@ -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;