Skip to content

Fix: Avoid crash with non-static value of hasSuggestions in require-meta-has-suggestions rule #163

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
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
4 changes: 2 additions & 2 deletions lib/rules/require-meta-has-suggestions.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,11 +65,11 @@ module.exports = {
if (!hasSuggestionsProperty) {
// Rule reports suggestions but is missing the `meta.hasSuggestions` property altogether.
context.report({ node: metaNode ? metaNode : ruleInfo.create, messageId: 'shouldBeSuggestable' });
} else if (hasSuggestionsStaticValue.value !== true) {
} else if (hasSuggestionsStaticValue && hasSuggestionsStaticValue.value !== true) {
// Rule reports suggestions but does not have `meta.hasSuggestions` property enabled.
context.report({ node: hasSuggestionsProperty.value, messageId: 'shouldBeSuggestable' });
}
} else if (!ruleReportsSuggestions && hasSuggestionsProperty && hasSuggestionsStaticValue.value === true) {
} else if (!ruleReportsSuggestions && hasSuggestionsProperty && hasSuggestionsStaticValue && hasSuggestionsStaticValue.value === true) {
// Rule does not report suggestions but has the `meta.hasSuggestions` property enabled.
context.report({ node: hasSuggestionsProperty.value, messageId: 'shouldNotBeSuggestable' });
}
Expand Down
28 changes: 28 additions & 0 deletions tests/lib/rules/require-meta-has-suggestions.js
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,34 @@ ruleTester.run('require-meta-has-suggestions', rule, {
}
};
`,
// Provides suggestions, has hasSuggestions property with no static value available.
`
module.exports = {
meta: { hasSuggestions: getHasSuggestions() },
create(context) {
context.report({node, message, suggest: [{}]});
}
};
`,
// Provides suggestions, has hasSuggestions property in variable with no static value available
`
const hasSuggestions = getHasSuggestions();
module.exports = {
meta: { hasSuggestions },
create(context) {
context.report({node, message, suggest: [{}]});
}
};
`,
// Does not provide suggestions, has hasSuggestions property with no static value available
`
module.exports = {
meta: { hasSuggestions: getHasSuggestions() },
create(context) {
context.report({node, message});
}
};
`,
// Spread syntax.
{
code: `
Expand Down