@@ -35,6 +35,33 @@ module.exports = {
3535 let contextIdentifiers ;
3636 let ruleReportsSuggestions ;
3737
38+ /**
39+ * Check if a "suggest" object property from a rule violation report should be considered to contain suggestions.
40+ * @param {Node } node - the "suggest" object property to check
41+ * @returns {boolean } whether this property should be considered to contain suggestions
42+ */
43+ function doesPropertyContainSuggestions ( node ) {
44+ const staticValue = getStaticValue ( node . value , context . getScope ( ) ) ;
45+ if (
46+ ! staticValue ||
47+ ( Array . isArray ( staticValue . value ) && staticValue . value . length > 0 ) ||
48+ ( Array . isArray ( staticValue . value ) &&
49+ staticValue . value . length === 0 &&
50+ node . value . type === 'Identifier' ) // Array variable can have suggestions pushed to it.
51+ ) {
52+ // These are all considered reporting suggestions:
53+ // suggest: [{...}]
54+ // suggest: getSuggestions()
55+ // suggest: MY_SUGGESTIONS
56+ return true ;
57+ }
58+ return false ;
59+ }
60+
61+ if ( ! ruleInfo ) {
62+ return { } ;
63+ }
64+
3865 return {
3966 Program ( ast ) {
4067 contextIdentifiers = utils . getContextIdentifiers ( scopeManager , ast ) ;
@@ -52,28 +79,22 @@ module.exports = {
5279 const suggestProp = utils
5380 . evaluateObjectProperties ( node . arguments [ 0 ] , scopeManager )
5481 . find ( ( prop ) => utils . getKeyName ( prop ) === 'suggest' ) ;
55- if ( suggestProp ) {
56- const staticValue = getStaticValue (
57- suggestProp . value ,
58- context . getScope ( )
59- ) ;
60- if (
61- ! staticValue ||
62- ( Array . isArray ( staticValue . value ) &&
63- staticValue . value . length > 0 ) ||
64- ( Array . isArray ( staticValue . value ) &&
65- staticValue . value . length === 0 &&
66- suggestProp . value . type === 'Identifier' ) // Array variable can have suggestions pushed to it.
67- ) {
68- // These are all considered reporting suggestions:
69- // suggest: [{...}]
70- // suggest: getSuggestions()
71- // suggest: MY_SUGGESTIONS
72- ruleReportsSuggestions = true ;
73- }
82+ if ( suggestProp && doesPropertyContainSuggestions ( suggestProp ) ) {
83+ ruleReportsSuggestions = true ;
7484 }
7585 }
7686 } ,
87+ Property ( node ) {
88+ // In order to reduce false positives, we will also check for a `suggest` property anywhere in the file.
89+ // This is helpful especially in the event that helper functions are used for reporting violations.
90+ if (
91+ node . key . type === 'Identifier' &&
92+ node . key . name === 'suggest' &&
93+ doesPropertyContainSuggestions ( node )
94+ ) {
95+ ruleReportsSuggestions = true ;
96+ }
97+ } ,
7798 'Program:exit' ( ) {
7899 const metaNode = ruleInfo && ruleInfo . meta ;
79100 const hasSuggestionsProperty = utils
0 commit comments