exports-style.js 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  1. /**
  2. * @author Toru Nagashima
  3. * @copyright 2016 Toru Nagashima. All rights reserved.
  4. * See LICENSE file in root directory for full license.
  5. */
  6. "use strict"
  7. //------------------------------------------------------------------------------
  8. // Helpers
  9. //------------------------------------------------------------------------------
  10. /*istanbul ignore next */
  11. /**
  12. * This function is copied from https://github.com/eslint/eslint/blob/2355f8d0de1d6732605420d15ddd4f1eee3c37b6/lib/ast-utils.js#L648-L684
  13. *
  14. * @param {ASTNode} node - The node to get.
  15. * @returns {string|null} The property name if static. Otherwise, null.
  16. * @private
  17. */
  18. function getStaticPropertyName(node) {
  19. let prop = null
  20. switch (node && node.type) {
  21. case "Property":
  22. case "MethodDefinition":
  23. prop = node.key
  24. break
  25. case "MemberExpression":
  26. prop = node.property
  27. break
  28. // no default
  29. }
  30. switch (prop && prop.type) {
  31. case "Literal":
  32. return String(prop.value)
  33. case "TemplateLiteral":
  34. if (prop.expressions.length === 0 && prop.quasis.length === 1) {
  35. return prop.quasis[0].value.cooked
  36. }
  37. break
  38. case "Identifier":
  39. if (!node.computed) {
  40. return prop.name
  41. }
  42. break
  43. // no default
  44. }
  45. return null
  46. }
  47. /**
  48. * Checks whether the given node is assignee or not.
  49. *
  50. * @param {ASTNode} node - The node to check.
  51. * @returns {boolean} `true` if the node is assignee.
  52. */
  53. function isAssignee(node) {
  54. return (
  55. node.parent.type === "AssignmentExpression" &&
  56. node.parent.left === node
  57. )
  58. }
  59. /**
  60. * Gets the top assignment expression node if the given node is an assignee.
  61. *
  62. * This is used to distinguish 2 assignees belong to the same assignment.
  63. * If the node is not an assignee, this returns null.
  64. *
  65. * @param {ASTNode} leafNode - The node to get.
  66. * @returns {ASTNode|null} The top assignment expression node, or null.
  67. */
  68. function getTopAssignment(leafNode) {
  69. let node = leafNode
  70. // Skip MemberExpressions.
  71. while (node.parent.type === "MemberExpression" && node.parent.object === node) {
  72. node = node.parent
  73. }
  74. // Check assignments.
  75. if (!isAssignee(node)) {
  76. return null
  77. }
  78. // Find the top.
  79. while (node.parent.type === "AssignmentExpression") {
  80. node = node.parent
  81. }
  82. return node
  83. }
  84. /**
  85. * Gets top assignment nodes of the given node list.
  86. *
  87. * @param {ASTNode[]} nodes - The node list to get.
  88. * @returns {ASTNode[]} Gotten top assignment nodes.
  89. */
  90. function createAssignmentList(nodes) {
  91. return nodes.map(getTopAssignment).filter(Boolean)
  92. }
  93. /**
  94. * Gets the reference of `module.exports` from the given scope.
  95. *
  96. * @param {escope.Scope} scope - The scope to get.
  97. * @returns {ASTNode[]} Gotten MemberExpression node list.
  98. */
  99. function getModuleExportsNodes(scope) {
  100. const variable = scope.set.get("module")
  101. if (variable == null) {
  102. return []
  103. }
  104. return variable.references
  105. .map(reference => reference.identifier.parent)
  106. .filter(node => (
  107. node.type === "MemberExpression" &&
  108. getStaticPropertyName(node) === "exports"
  109. ))
  110. }
  111. /**
  112. * Gets the reference of `exports` from the given scope.
  113. *
  114. * @param {escope.Scope} scope - The scope to get.
  115. * @returns {ASTNode[]} Gotten Identifier node list.
  116. */
  117. function getExportsNodes(scope) {
  118. const variable = scope.set.get("exports")
  119. if (variable == null) {
  120. return []
  121. }
  122. return variable.references.map(reference => reference.identifier)
  123. }
  124. /**
  125. * The definition of this rule.
  126. *
  127. * @param {RuleContext} context - The rule context to check.
  128. * @returns {object} The definition of this rule.
  129. */
  130. function create(context) {
  131. const mode = context.options[0] || "module.exports"
  132. const batchAssignAllowed = Boolean(
  133. context.options[1] != null &&
  134. context.options[1].allowBatchAssign
  135. )
  136. const sourceCode = context.getSourceCode()
  137. /**
  138. * Gets the location info of reports.
  139. *
  140. * exports = foo
  141. * ^^^^^^^^^
  142. *
  143. * module.exports = foo
  144. * ^^^^^^^^^^^^^^^^
  145. *
  146. * @param {ASTNode} node - The node of `exports`/`module.exports`.
  147. * @returns {Location} The location info of reports.
  148. */
  149. function getLocation(node) {
  150. const token = sourceCode.getTokenAfter(node)
  151. return {
  152. start: node.loc.start,
  153. end: token.loc.end,
  154. }
  155. }
  156. /**
  157. * Enforces `module.exports`.
  158. * This warns references of `exports`.
  159. *
  160. * @returns {void}
  161. */
  162. function enforceModuleExports() {
  163. const globalScope = context.getScope()
  164. const exportsNodes = getExportsNodes(globalScope)
  165. const assignList = batchAssignAllowed
  166. ? createAssignmentList(getModuleExportsNodes(globalScope))
  167. : []
  168. for (const node of exportsNodes) {
  169. // Skip if it's a batch assignment.
  170. if (assignList.length > 0 &&
  171. assignList.indexOf(getTopAssignment(node)) !== -1
  172. ) {
  173. continue
  174. }
  175. // Report.
  176. context.report({
  177. node,
  178. loc: getLocation(node),
  179. message:
  180. "Unexpected access to 'exports'. " +
  181. "Use 'module.exports' instead.",
  182. })
  183. }
  184. }
  185. /**
  186. * Enforces `exports`.
  187. * This warns references of `module.exports`.
  188. *
  189. * @returns {void}
  190. */
  191. function enforceExports() {
  192. const globalScope = context.getScope()
  193. const exportsNodes = getExportsNodes(globalScope)
  194. const moduleExportsNodes = getModuleExportsNodes(globalScope)
  195. const assignList = batchAssignAllowed
  196. ? createAssignmentList(exportsNodes)
  197. : []
  198. const batchAssignList = []
  199. for (const node of moduleExportsNodes) {
  200. // Skip if it's a batch assignment.
  201. if (assignList.length > 0) {
  202. const found = assignList.indexOf(getTopAssignment(node))
  203. if (found !== -1) {
  204. batchAssignList.push(assignList[found])
  205. assignList.splice(found, 1)
  206. continue
  207. }
  208. }
  209. // Report.
  210. context.report({
  211. node,
  212. loc: getLocation(node),
  213. message:
  214. "Unexpected access to 'module.exports'. " +
  215. "Use 'exports' instead.",
  216. })
  217. }
  218. // Disallow direct assignment to `exports`.
  219. for (const node of exportsNodes) {
  220. // Skip if it's not assignee.
  221. if (!isAssignee(node)) {
  222. continue
  223. }
  224. // Check if it's a batch assignment.
  225. if (batchAssignList.indexOf(getTopAssignment(node)) !== -1) {
  226. continue
  227. }
  228. // Report.
  229. context.report({
  230. node,
  231. loc: getLocation(node),
  232. message:
  233. "Unexpected assignment to 'exports'. " +
  234. "Don't modify 'exports' itself.",
  235. })
  236. }
  237. }
  238. return {
  239. "Program:exit"() {
  240. switch (mode) {
  241. case "module.exports":
  242. enforceModuleExports()
  243. break
  244. case "exports":
  245. enforceExports()
  246. break
  247. // no default
  248. }
  249. },
  250. }
  251. }
  252. //------------------------------------------------------------------------------
  253. // Rule Definition
  254. //------------------------------------------------------------------------------
  255. module.exports = {
  256. create,
  257. meta: {
  258. docs: {
  259. description: "enforce either `module.exports` or `exports`",
  260. category: "Stylistic Issues",
  261. recommended: false,
  262. },
  263. fixable: false,
  264. schema: [
  265. { //
  266. enum: ["module.exports", "exports"],
  267. },
  268. {
  269. type: "object",
  270. properties: {allowBatchAssign: {type: "boolean"}},
  271. additionalProperties: false,
  272. },
  273. ],
  274. },
  275. }