@@ -35,6 +35,33 @@ module.exports = {
35
35
let contextIdentifiers ;
36
36
let ruleReportsSuggestions ;
37
37
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
+
38
65
return {
39
66
Program ( ast ) {
40
67
contextIdentifiers = utils . getContextIdentifiers ( scopeManager , ast ) ;
@@ -52,28 +79,22 @@ module.exports = {
52
79
const suggestProp = utils
53
80
. evaluateObjectProperties ( node . arguments [ 0 ] , scopeManager )
54
81
. 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 ;
74
84
}
75
85
}
76
86
} ,
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
+ } ,
77
98
'Program:exit' ( ) {
78
99
const metaNode = ruleInfo && ruleInfo . meta ;
79
100
const hasSuggestionsProperty = utils
0 commit comments