exports-style.js 8.5 KB

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