Skip to content

Fix: Fix false negatives and reporting location in require-meta-type rule #155

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

Merged
merged 1 commit into from
Jul 7, 2021
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
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' }],
Copy link
Member Author

Choose a reason for hiding this comment

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

This test case was valid before (false negative).

},
{
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' }],
Copy link
Member Author

Choose a reason for hiding this comment

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

This test case was valid before (false negative).

},
],
});