@@ -4,10 +4,6 @@ const quoteString = require('./utils/quote-string');
4
4
const replaceTemplateElement = require ( './utils/replace-template-element' ) ;
5
5
const escapeTemplateElementRaw = require ( './utils/escape-template-element-raw' ) ;
6
6
7
- const defaultPatterns = {
8
- '\'' : '’'
9
- } ;
10
-
11
7
const ignoredIdentifier = new Set ( [
12
8
'gql' ,
13
9
'html' ,
@@ -43,13 +39,10 @@ const isIgnoredTag = node => {
43
39
} ;
44
40
45
41
const defaultMessage = 'Prefer `{{suggest}}` over `{{match}}`.' ;
42
+ const SUGGESTION_MESSAGE_ID = 'replace' ;
46
43
47
44
function getReplacements ( patterns ) {
48
- return Object . entries ( {
49
- ...defaultPatterns ,
50
- ...patterns
51
- } )
52
- . filter ( ( [ , options ] ) => options !== false )
45
+ return Object . entries ( patterns )
53
46
. map ( ( [ match , options ] ) => {
54
47
if ( typeof options === 'string' ) {
55
48
options = {
@@ -84,14 +77,11 @@ const create = context => {
84
77
let string ;
85
78
if ( type === 'Literal' ) {
86
79
string = node . value ;
87
- if ( typeof string !== 'string' ) {
88
- return ;
89
- }
90
80
} else if ( ! isIgnoredTag ( node ) ) {
91
81
string = node . value . raw ;
92
82
}
93
83
94
- if ( ! string ) {
84
+ if ( ! string || typeof string !== 'string' ) {
95
85
return ;
96
86
}
97
87
@@ -101,33 +91,39 @@ const create = context => {
101
91
return ;
102
92
}
103
93
104
- const { fix, message = defaultMessage , match, suggest} = replacement ;
94
+ const { fix : autoFix , message = defaultMessage , match, suggest} = replacement ;
95
+ const messageData = {
96
+ match,
97
+ suggest
98
+ } ;
105
99
const problem = {
106
100
node,
107
101
message,
108
- data : {
109
- match,
110
- suggest
111
- }
102
+ data : messageData
112
103
} ;
113
104
114
- if ( ! fix ) {
115
- context . report ( problem ) ;
116
- return ;
117
- }
118
-
119
105
const fixed = string . replace ( replacement . regex , suggest ) ;
120
- if ( type === 'Literal' ) {
121
- problem . fix = fixer => fixer . replaceText (
106
+ const fix = type === 'Literal' ?
107
+ fixer => fixer . replaceText (
122
108
node ,
123
109
quoteString ( fixed , node . raw [ 0 ] )
124
- ) ;
125
- } else {
126
- problem . fix = fixer => replaceTemplateElement (
110
+ ) :
111
+ fixer => replaceTemplateElement (
127
112
fixer ,
128
113
node ,
129
114
escapeTemplateElementRaw ( fixed )
130
115
) ;
116
+
117
+ if ( autoFix ) {
118
+ problem . fix = fix ;
119
+ } else {
120
+ problem . suggest = [
121
+ {
122
+ messageId : SUGGESTION_MESSAGE_ID ,
123
+ data : messageData ,
124
+ fix
125
+ }
126
+ ] ;
131
127
}
132
128
133
129
context . report ( problem ) ;
@@ -143,11 +139,6 @@ const schema = [
143
139
type : 'object' ,
144
140
additionalProperties : {
145
141
anyOf : [
146
- {
147
- enum : [
148
- false
149
- ]
150
- } ,
151
142
{
152
143
type : 'string'
153
144
} ,
@@ -186,6 +177,9 @@ module.exports = {
186
177
url : getDocumentationUrl ( __filename )
187
178
} ,
188
179
fixable : 'code' ,
189
- schema
180
+ schema,
181
+ messages : {
182
+ [ SUGGESTION_MESSAGE_ID ] : 'Replace `{{match}}` with `{{suggest}}`.'
183
+ }
190
184
}
191
185
} ;
0 commit comments