Skip to content

Commit

Permalink
Fix: Fix false negatives and reporting location in require-meta-type (
Browse files Browse the repository at this point in the history
  • Loading branch information
bmish authored Jul 7, 2021
1 parent 4aa9aca commit 7c0d1d0
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 8 deletions.
16 changes: 13 additions & 3 deletions lib/rules/require-meta-type.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

'use strict';

const { getStaticValue } = require('eslint-utils');
const utils = require('../utils');
const VALID_TYPES = new Set(['problem', 'suggestion', 'layout']);

Expand Down Expand Up @@ -52,10 +53,19 @@ module.exports = {
metaNode.properties &&
metaNode.properties.find(p => p.type === 'Property' && utils.getKeyName(p) === 'type');

if (typeNode && typeNode.value.type === 'Literal' && !VALID_TYPES.has(typeNode.value.value)) {
context.report({ node: metaNode, messageId: 'unexpected' });
} else if (!typeNode) {
if (!typeNode) {
context.report({ node: metaNode, messageId: 'missing' });
return;
}

const staticValue = getStaticValue(typeNode.value, context.getScope());
if (!staticValue) {
// Ignore non-static values since we can't determine what they look like.
return;
}

if (!VALID_TYPES.has(staticValue.value)) {
context.report({ node: typeNode.value, messageId: 'unexpected' });
}
},
};
Expand Down
57 changes: 52 additions & 5 deletions tests/lib/rules/require-meta-type.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,25 @@ ruleTester.run('require-meta-type', rule, {
create(context) {}
};
`,
`
const type = 'problem';
module.exports = {
meta: { type },
create(context) {}
};
`,
`
module.exports = {
meta: { type: getType() },
create(context) {}
};
`,
`
module.exports = {
meta: { type: FOO },
create(context) {}
};
`,
`module.exports = {
create(context) {}
}`,
Expand All @@ -60,7 +79,7 @@ ruleTester.run('require-meta-type', rule, {
create(context) {}
};
`,
errors: [{ messageId: 'missing' }],
errors: [{ messageId: 'missing', type: 'ObjectExpression' }],
},
{
code: `
Expand All @@ -70,7 +89,7 @@ ruleTester.run('require-meta-type', rule, {
create,
};
`,
errors: [{ messageId: 'missing' }],
errors: [{ messageId: 'missing', type: 'ObjectExpression' }],
},
{
code: `
Expand All @@ -80,7 +99,7 @@ ruleTester.run('require-meta-type', rule, {
create,
};
`,
errors: [{ messageId: 'missing' }],
errors: [{ messageId: 'missing', type: 'ObjectExpression' }],
},
{
code: `
Expand All @@ -90,7 +109,7 @@ ruleTester.run('require-meta-type', rule, {
create,
};
`,
errors: [{ messageId: 'missing' }],
errors: [{ messageId: 'missing', type: 'ObjectExpression' }],
},
{
code: `
Expand All @@ -99,7 +118,35 @@ ruleTester.run('require-meta-type', rule, {
create(context) {}
};
`,
errors: [{ messageId: 'unexpected' }],
errors: [{ messageId: 'unexpected', type: 'Literal' }],
},
{
code: `
const type = 'invalid-type';
module.exports = {
meta: { type },
create(context) {}
};
`,
errors: [{ messageId: 'unexpected', type: 'Identifier' }],
},
{
code: `
module.exports = {
meta: { type: null },
create(context) {}
};
`,
errors: [{ messageId: 'unexpected', type: 'Literal' }],
},
{
code: `
module.exports = {
meta: { type: undefined },
create(context) {}
};
`,
errors: [{ messageId: 'unexpected', type: 'Identifier' }],
},
],
});

0 comments on commit 7c0d1d0

Please sign in to comment.