|
| 1 | +/** |
| 2 | + * @fileoverview Transform that migrates an ESLint rule definitions from the old format: |
| 3 | + * |
| 4 | + * ``` |
| 5 | + * module.exports = function(context) { ... } |
| 6 | + * ``` |
| 7 | + * |
| 8 | + * to the new format: |
| 9 | + * |
| 10 | + * ``` |
| 11 | + * module.exports = { |
| 12 | + * meta: { |
| 13 | + * docs: {}, |
| 14 | + * schema: [] |
| 15 | + * }, |
| 16 | + * create: function(context) { ... } |
| 17 | + * }; |
| 18 | + * ``` |
| 19 | + * @author Vitor Balocco |
| 20 | + */ |
| 21 | + |
| 22 | +"use strict"; |
| 23 | + |
| 24 | +var j = require("jscodeshift"); |
| 25 | + |
| 26 | +//------------------------------------------------------------------------------ |
| 27 | +// Helpers |
| 28 | +//------------------------------------------------------------------------------ |
| 29 | + |
| 30 | +/** |
| 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 |
| 35 | + */ |
| 36 | +function isAlreadyInNewFormat(rootNode) { |
| 37 | + // If there's already a module.exports.meta property, we assume the rule |
| 38 | + // is already in the new format. |
| 39 | + return rootNode |
| 40 | + .find(j.Property) |
| 41 | + .filter(function(node) { |
| 42 | + return ( |
| 43 | + node.value.key.name === "meta" && |
| 44 | + node.parent.value.type === "ObjectExpression" && |
| 45 | + node.parent.parent.value.type === "AssignmentExpression" && |
| 46 | + node.parent.parent.value.left.type === "MemberExpression" && |
| 47 | + node.parent.parent.value.left.object.name === "module" && |
| 48 | + node.parent.parent.value.left.property.name === "exports" |
| 49 | + ); |
| 50 | + }) |
| 51 | + .size() > 0; |
| 52 | +} |
| 53 | + |
| 54 | +/** |
| 55 | + * 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 |
| 59 | + */ |
| 60 | +function isFunctionOrArrowFunctionExpression(node) { |
| 61 | + return node.type === "FunctionExpression" || node.type === "ArrowFunctionExpression"; |
| 62 | +} |
| 63 | + |
| 64 | +/** |
| 65 | + * 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 |
| 69 | + */ |
| 70 | +function isOldFormatRuleDefinition(node) { |
| 71 | + return isFunctionOrArrowFunctionExpression(node) || node.type === "CallExpression"; |
| 72 | +} |
| 73 | + |
| 74 | +/** |
| 75 | + * Returns the node in `rootNode` that is the rule definition in the old format, |
| 76 | + * which will be in the format: |
| 77 | + * module.exports = function(context) { ... }; |
| 78 | + * |
| 79 | + * @param {Object} rootNode - where to look for the rule definition node |
| 80 | + * @returns {Object} node - rule definition expression node |
| 81 | + */ |
| 82 | +function getOldFormatRuleDefinition(rootNode) { |
| 83 | + return rootNode |
| 84 | + .find(j.AssignmentExpression) |
| 85 | + .filter(function(node) { |
| 86 | + return ( |
| 87 | + node.value.left.type === "MemberExpression" && |
| 88 | + node.value.left.property.name === "exports" && |
| 89 | + node.value.left.object.name === "module" && |
| 90 | + isOldFormatRuleDefinition(node.value.right) |
| 91 | + ); |
| 92 | + }); |
| 93 | +} |
| 94 | + |
| 95 | +/** |
| 96 | + * Returns the node in `rootNode` that is the schema definition in the old format, |
| 97 | + * which will be in the format: |
| 98 | + * module.exports.schema = [ ... ]; |
| 99 | + * |
| 100 | + * @param {Object} rootNode - where to look for the rule definition node |
| 101 | + * @returns {Object} node - rule definition expression node |
| 102 | + */ |
| 103 | +function getOldFormatSchemaDefinition(rootNode) { |
| 104 | + return rootNode |
| 105 | + .find(j.AssignmentExpression) |
| 106 | + .filter(function(node) { |
| 107 | + return ( |
| 108 | + node.value.left.type === "MemberExpression" && |
| 109 | + node.value.left.property.name === "schema" && |
| 110 | + node.value.left.object.type === "MemberExpression" && |
| 111 | + node.value.left.object.object.name === "module" && |
| 112 | + node.value.left.object.property.name === "exports" |
| 113 | + ); |
| 114 | + }); |
| 115 | +} |
| 116 | + |
| 117 | +/** |
| 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 | +*/ |
| 125 | +function createMetaObjectExpression(schemaNode, schemaNodeComments, isRuleFixable) { |
| 126 | + var properties = [ |
| 127 | + // For docs, create just an empty object |
| 128 | + j.property("init", j.identifier("docs"), j.objectExpression([])) |
| 129 | + ]; |
| 130 | + |
| 131 | + if (isRuleFixable) { |
| 132 | + properties.push( |
| 133 | + j.property("init", j.identifier("fixable"), j.literal("code")) |
| 134 | + ); |
| 135 | + } |
| 136 | + |
| 137 | + // The schema definition may not exist in some plugins |
| 138 | + if (schemaNode) { |
| 139 | + var schemaNodeProperty = j.property("init", j.identifier("schema"), schemaNode); |
| 140 | + // Restore comments that were removed when the old format node was removed |
| 141 | + schemaNodeProperty.comments = schemaNodeComments; |
| 142 | + properties.push(schemaNodeProperty); |
| 143 | + } |
| 144 | + |
| 145 | + return j.objectExpression(properties); |
| 146 | +} |
| 147 | + |
| 148 | +/** |
| 149 | + * 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 |
| 154 | + * @returns {Object} ExpressionStatement |
| 155 | + */ |
| 156 | +function createExportsExpression(ruleDefinitionNode, ruleDefinitionNodeComments, ruleMetaDefinitionNode) { |
| 157 | + var exportsExpression = j.expressionStatement( |
| 158 | + 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 | + ]) |
| 165 | + ) |
| 166 | + ); |
| 167 | + |
| 168 | + // Restore comments that were removed when the old format node was removed |
| 169 | + exportsExpression.comments = ruleDefinitionNodeComments; |
| 170 | + |
| 171 | + return exportsExpression; |
| 172 | +} |
| 173 | + |
| 174 | +//------------------------------------------------------------------------------ |
| 175 | +// Transform Definition |
| 176 | +//------------------------------------------------------------------------------ |
| 177 | + |
| 178 | +/** |
| 179 | + * @param {Object} fileInfo - holds information about the currently processed file. |
| 180 | + * @returns {String} the new source code, after being transformed. |
| 181 | + */ |
| 182 | +module.exports = function(fileInfo) { |
| 183 | + var root = j(fileInfo.source); |
| 184 | + |
| 185 | + if (isAlreadyInNewFormat(root)) { |
| 186 | + // don't do anything and return |
| 187 | + return root.toSource(); |
| 188 | + } |
| 189 | + |
| 190 | + // for most plugins, the old format should be: |
| 191 | + // module.exports = function(context) { ... } |
| 192 | + // 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"; |
| 195 | + if (ruleDefinitionExpression.params && ruleDefinitionExpression.params.length > 0) { |
| 196 | + identifierNameForContextObject = ruleDefinitionExpression.params[0].name; |
| 197 | + } |
| 198 | + |
| 199 | + // If the rule has a call for context.report and a property `fix` is being passed in, |
| 200 | + // then we consider that the rule is fixable: |
| 201 | + // context.report({ |
| 202 | + // ... |
| 203 | + // fix: function() { ... } |
| 204 | + // }); |
| 205 | + var isRuleFixable = root |
| 206 | + .find(j.Identifier) |
| 207 | + .filter(function(node) { |
| 208 | + return ( |
| 209 | + node.value.name === "fix" && |
| 210 | + node.parent.value.type === "Property" && |
| 211 | + node.parent.parent.parent.value.type === "CallExpression" && |
| 212 | + node.parent.parent.parent.value.callee.type === "MemberExpression" && |
| 213 | + node.parent.parent.parent.value.callee.object.name === identifierNameForContextObject && |
| 214 | + node.parent.parent.parent.value.callee.property.name === "report" |
| 215 | + ); |
| 216 | + }) |
| 217 | + .size() > 0; |
| 218 | + |
| 219 | + var oldFormatSchemaDefinition = getOldFormatSchemaDefinition(root); |
| 220 | + var schemaNode, schemaNodeComments; |
| 221 | + // The schema definition may not exist in some plugins |
| 222 | + if (oldFormatSchemaDefinition.size() > 0) { |
| 223 | + schemaNode = getOldFormatSchemaDefinition(root).get().value.right; |
| 224 | + // Store the comments too so we can attach it again later |
| 225 | + schemaNodeComments = getOldFormatSchemaDefinition(root).get().parent.value.leadingComments; |
| 226 | + getOldFormatSchemaDefinition(root).remove(); |
| 227 | + } |
| 228 | + |
| 229 | + var ruleDefinitionNode = getOldFormatRuleDefinition(root).get().value.right; |
| 230 | + // Store the comments too so we can attach it again later |
| 231 | + var ruleDefinitionNodeComments = getOldFormatRuleDefinition(root).get().parent.value.leadingComments; |
| 232 | + getOldFormatRuleDefinition(root).remove(); |
| 233 | + |
| 234 | + // Insert the rule definition in the new format at the end of the file |
| 235 | + var newFormat = createExportsExpression( |
| 236 | + ruleDefinitionNode, |
| 237 | + ruleDefinitionNodeComments, |
| 238 | + createMetaObjectExpression(schemaNode, schemaNodeComments, isRuleFixable) |
| 239 | + ); |
| 240 | + |
| 241 | + root.find(j.Program).get().value.body.push(newFormat); |
| 242 | + return root.toSource(); |
| 243 | +}; |
0 commit comments