exports-style.js 8.2 KB

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