21
21
22
22
"use strict" ;
23
23
24
- var j = require ( "jscodeshift" ) ;
24
+ const j = require ( "jscodeshift" ) ;
25
25
26
26
//------------------------------------------------------------------------------
27
27
// Helpers
28
28
//------------------------------------------------------------------------------
29
29
30
30
/**
31
31
* Returns `true` if the rule is already in the new format
32
- *
33
- * @param {Object } rootNode - where to look for the rule definition
34
- * @returns {Boolean } `true` if rule is already in the new format
32
+ * @param {Object } rootNode where to look for the rule definition
33
+ * @returns {boolean } `true` if rule is already in the new format
35
34
*/
36
35
function isAlreadyInNewFormat ( rootNode ) {
36
+
37
37
// If there's already a module.exports.meta property, we assume the rule
38
38
// is already in the new format.
39
39
return rootNode
40
40
. find ( j . Property )
41
- . filter ( function ( node ) {
42
- return (
43
- node . value . key . name === "meta" &&
41
+ . filter ( node => (
42
+ node . value . key . name === "meta" &&
44
43
node . parent . value . type === "ObjectExpression" &&
45
44
node . parent . parent . value . type === "AssignmentExpression" &&
46
45
node . parent . parent . value . left . type === "MemberExpression" &&
47
46
node . parent . parent . value . left . object . name === "module" &&
48
47
node . parent . parent . value . left . property . name === "exports"
49
- ) ;
50
- } )
48
+ ) )
51
49
. size ( ) > 0 ;
52
50
}
53
51
54
52
/**
55
53
* Checks if the node passed is a function expression or an arrow function expression
56
- *
57
- * @param {Object } node - node to check
58
- * @returns {Boolean } - `true` if node is a function or arrow function expression
54
+ * @param {Object } node node to check
55
+ * @returns {boolean } - `true` if node is a function or arrow function expression
59
56
*/
60
57
function isFunctionOrArrowFunctionExpression ( node ) {
61
58
return node . type === "FunctionExpression" || node . type === "ArrowFunctionExpression" ;
62
59
}
63
60
64
61
/**
65
62
* Checks if the node passed can be an "old format" rule definition
66
- *
67
- * @param {Object } node - node to check
68
- * @returns {Boolean } - `true` if node looks like an "old format" rule definition
63
+ * @param {Object } node node to check
64
+ * @returns {boolean } - `true` if node looks like an "old format" rule definition
69
65
*/
70
66
function isOldFormatRuleDefinition ( node ) {
71
67
return isFunctionOrArrowFunctionExpression ( node ) || node . type === "CallExpression" ;
@@ -75,55 +71,49 @@ function isOldFormatRuleDefinition(node) {
75
71
* Returns the node in `rootNode` that is the rule definition in the old format,
76
72
* which will be in the format:
77
73
* module.exports = function(context) { ... };
78
- *
79
- * @param {Object } rootNode - where to look for the rule definition node
74
+ * @param {Object } rootNode where to look for the rule definition node
80
75
* @returns {Object } node - rule definition expression node
81
76
*/
82
77
function getOldFormatRuleDefinition ( rootNode ) {
83
78
return rootNode
84
79
. find ( j . AssignmentExpression )
85
- . filter ( function ( node ) {
86
- return (
87
- node . value . left . type === "MemberExpression" &&
80
+ . filter ( node => (
81
+ node . value . left . type === "MemberExpression" &&
88
82
node . value . left . property . name === "exports" &&
89
83
node . value . left . object . name === "module" &&
90
84
isOldFormatRuleDefinition ( node . value . right )
91
- ) ;
92
- } ) ;
85
+ ) ) ;
93
86
}
94
87
95
88
/**
96
89
* Returns the node in `rootNode` that is the schema definition in the old format,
97
90
* which will be in the format:
98
91
* module.exports.schema = [ ... ];
99
- *
100
- * @param {Object } rootNode - where to look for the rule definition node
92
+ * @param {Object } rootNode where to look for the rule definition node
101
93
* @returns {Object } node - rule definition expression node
102
94
*/
103
95
function getOldFormatSchemaDefinition ( rootNode ) {
104
96
return rootNode
105
97
. find ( j . AssignmentExpression )
106
- . filter ( function ( node ) {
107
- return (
108
- node . value . left . type === "MemberExpression" &&
98
+ . filter ( node => (
99
+ node . value . left . type === "MemberExpression" &&
109
100
node . value . left . property . name === "schema" &&
110
101
node . value . left . object . type === "MemberExpression" &&
111
102
node . value . left . object . object . name === "module" &&
112
103
node . value . left . object . property . name === "exports"
113
- ) ;
114
- } ) ;
104
+ ) ) ;
115
105
}
116
106
117
107
/**
118
- * Creates the object expression node that will be the `meta` property of this rule
119
- *
120
- * @param {Object } schemaNode - node that was the schema definition in the old rule format
121
- * @param {Object } schemaNodeComments - comments that were above the old schema node
122
- * @param {Boolean } isRuleFixable - `true` if the rule is fixable
123
- * @returns {Object } ObjectExpression node
124
- */
108
+ * Creates the object expression node that will be the `meta` property of this rule
109
+ * @param {Object } schemaNode node that was the schema definition in the old rule format
110
+ * @param {Object } schemaNodeComments comments that were above the old schema node
111
+ * @param {boolean } isRuleFixable `true` if the rule is fixable
112
+ * @returns {Object } ObjectExpression node
113
+ */
125
114
function createMetaObjectExpression ( schemaNode , schemaNodeComments , isRuleFixable ) {
126
- var properties = [
115
+ const properties = [
116
+
127
117
// For docs, create just an empty object
128
118
j . property ( "init" , j . identifier ( "docs" ) , j . objectExpression ( [ ] ) )
129
119
] ;
@@ -136,7 +126,9 @@ function createMetaObjectExpression(schemaNode, schemaNodeComments, isRuleFixabl
136
126
137
127
// The schema definition may not exist in some plugins
138
128
if ( schemaNode ) {
139
- var schemaNodeProperty = j . property ( "init" , j . identifier ( "schema" ) , schemaNode ) ;
129
+ const schemaNodeProperty = j . property ( "init" , j . identifier ( "schema" ) , schemaNode ) ;
130
+
131
+
140
132
// Restore comments that were removed when the old format node was removed
141
133
schemaNodeProperty . comments = schemaNodeComments ;
142
134
properties . push ( schemaNodeProperty ) ;
@@ -147,21 +139,20 @@ function createMetaObjectExpression(schemaNode, schemaNodeComments, isRuleFixabl
147
139
148
140
/**
149
141
* Creates the `exports` expression that wil contain the rule definition in the new format
150
- *
151
- * @param {Object } ruleDefinitionNode - node that was the rule definition in the old rule format
152
- * @param {Object } ruleDefinitionNodeComments - comments that were above the old schema rule definition
153
- * @param {Object } ruleMetaDefinitionNode - node that will be the meta definition expression
142
+ * @param {Object } ruleDefinitionNode node that was the rule definition in the old rule format
143
+ * @param {Object } ruleDefinitionNodeComments comments that were above the old schema rule definition
144
+ * @param {Object } ruleMetaDefinitionNode node that will be the meta definition expression
154
145
* @returns {Object } ExpressionStatement
155
146
*/
156
147
function createExportsExpression ( ruleDefinitionNode , ruleDefinitionNodeComments , ruleMetaDefinitionNode ) {
157
- var exportsExpression = j . expressionStatement (
148
+ const exportsExpression = j . expressionStatement (
158
149
j . assignmentExpression (
159
- "=" ,
160
- j . memberExpression ( j . identifier ( "module" ) , j . identifier ( "exports" ) , false ) ,
161
- j . objectExpression ( [
162
- j . property ( "init" , j . identifier ( "meta" ) , ruleMetaDefinitionNode ) ,
163
- j . property ( "init" , j . identifier ( "create" ) , ruleDefinitionNode )
164
- ] )
150
+ "=" ,
151
+ j . memberExpression ( j . identifier ( "module" ) , j . identifier ( "exports" ) , false ) ,
152
+ j . objectExpression ( [
153
+ j . property ( "init" , j . identifier ( "meta" ) , ruleMetaDefinitionNode ) ,
154
+ j . property ( "init" , j . identifier ( "create" ) , ruleDefinitionNode )
155
+ ] )
165
156
)
166
157
) ;
167
158
@@ -176,22 +167,25 @@ function createExportsExpression(ruleDefinitionNode, ruleDefinitionNodeComments,
176
167
//------------------------------------------------------------------------------
177
168
178
169
/**
179
- * @param {Object } fileInfo - holds information about the currently processed file.
180
- * @returns {String } the new source code, after being transformed.
170
+ * Transforms an ESLint rule from the old format to the new format.
171
+ * @param {Object } fileInfo holds information about the currently processed file.
172
+ * @returns {string } the new source code, after being transformed.
181
173
*/
182
174
module . exports = function ( fileInfo ) {
183
- var root = j ( fileInfo . source ) ;
175
+ const root = j ( fileInfo . source ) ;
184
176
185
177
if ( isAlreadyInNewFormat ( root ) ) {
178
+
186
179
// don't do anything and return
187
180
return root . toSource ( ) ;
188
181
}
189
182
190
183
// for most plugins, the old format should be:
191
184
// module.exports = function(context) { ... }
192
185
// but maybe some plugins are using a diferent variable name instead of `context`
193
- var ruleDefinitionExpression = getOldFormatRuleDefinition ( root ) . get ( ) . value . right ;
194
- var identifierNameForContextObject = "context" ;
186
+ const ruleDefinitionExpression = getOldFormatRuleDefinition ( root ) . get ( ) . value . right ;
187
+ let identifierNameForContextObject = "context" ;
188
+
195
189
if ( ruleDefinitionExpression . params && ruleDefinitionExpression . params . length > 0 ) {
196
190
identifierNameForContextObject = ruleDefinitionExpression . params [ 0 ] . name ;
197
191
}
@@ -202,37 +196,40 @@ module.exports = function(fileInfo) {
202
196
// ...
203
197
// fix: function() { ... }
204
198
// });
205
- var isRuleFixable = root
199
+ const isRuleFixable = root
206
200
. find ( j . Identifier )
207
- . filter ( function ( node ) {
208
- return (
209
- node . value . name === "fix" &&
201
+ . filter ( node => (
202
+ node . value . name === "fix" &&
210
203
node . parent . value . type === "Property" &&
211
204
node . parent . parent . parent . value . type === "CallExpression" &&
212
205
node . parent . parent . parent . value . callee . type === "MemberExpression" &&
213
206
node . parent . parent . parent . value . callee . object . name === identifierNameForContextObject &&
214
207
node . parent . parent . parent . value . callee . property . name === "report"
215
- ) ;
216
- } )
208
+ ) )
217
209
. size ( ) > 0 ;
218
210
219
- var oldFormatSchemaDefinition = getOldFormatSchemaDefinition ( root ) ;
220
- var schemaNode , schemaNodeComments ;
211
+ const oldFormatSchemaDefinition = getOldFormatSchemaDefinition ( root ) ;
212
+ let schemaNode , schemaNodeComments ;
213
+
214
+
221
215
// The schema definition may not exist in some plugins
222
216
if ( oldFormatSchemaDefinition . size ( ) > 0 ) {
223
217
schemaNode = getOldFormatSchemaDefinition ( root ) . get ( ) . value . right ;
218
+
224
219
// Store the comments too so we can attach it again later
225
220
schemaNodeComments = getOldFormatSchemaDefinition ( root ) . get ( ) . parent . value . leadingComments ;
226
221
getOldFormatSchemaDefinition ( root ) . remove ( ) ;
227
222
}
228
223
229
- var ruleDefinitionNode = getOldFormatRuleDefinition ( root ) . get ( ) . value . right ;
224
+ const ruleDefinitionNode = getOldFormatRuleDefinition ( root ) . get ( ) . value . right ;
225
+
230
226
// Store the comments too so we can attach it again later
231
- var ruleDefinitionNodeComments = getOldFormatRuleDefinition ( root ) . get ( ) . parent . value . leadingComments ;
227
+ const ruleDefinitionNodeComments = getOldFormatRuleDefinition ( root ) . get ( ) . parent . value . leadingComments ;
228
+
232
229
getOldFormatRuleDefinition ( root ) . remove ( ) ;
233
230
234
231
// Insert the rule definition in the new format at the end of the file
235
- var newFormat = createExportsExpression (
232
+ const newFormat = createExportsExpression (
236
233
ruleDefinitionNode ,
237
234
ruleDefinitionNodeComments ,
238
235
createMetaObjectExpression ( schemaNode , schemaNodeComments , isRuleFixable )
0 commit comments