eslint-plugin-n 15.1.0 → 15.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -93,7 +93,7 @@ $ npm install --save-dev eslint eslint-plugin-n
93
93
  | Rule ID | Description | |
94
94
  |:--------|:------------|:--:|
95
95
  | [n/callback-return](./docs/rules/callback-return.md) | require `return` statements after callbacks | |
96
- | [n/exports-style](./docs/rules/exports-style.md) | enforce either `module.exports` or `exports` | |
96
+ | [n/exports-style](./docs/rules/exports-style.md) | enforce either `module.exports` or `exports` | ✒️ |
97
97
  | [n/file-extension-in-import](./docs/rules/file-extension-in-import.md) | enforce the style of file extensions in `import` declarations | ✒️ |
98
98
  | [n/global-require](./docs/rules/global-require.md) | require `require()` calls to be placed at top-level module scope | |
99
99
  | [n/no-mixed-requires](./docs/rules/no-mixed-requires.md) | disallow `require` calls to be mixed with regular variable declarations | |
@@ -167,7 +167,7 @@ Those preset config:
167
167
 
168
168
  ## 📰 Changelog
169
169
 
170
- - [GitHub Releases](https://github.com/weiran-zsd/eslint-plugin-n/releases)
170
+ - [GitHub Releases](https://github.com/weiran-zsd/eslint-plugin-node/releases)
171
171
 
172
172
  ## ❤️ Contributing
173
173
 
@@ -139,6 +139,90 @@ function getExportsNodes(scope) {
139
139
  return variable.references.map(reference => reference.identifier)
140
140
  }
141
141
 
142
+ function getReplacementForProperty(property, sourceCode) {
143
+ if (property.type !== "Property" || property.kind !== "init") {
144
+ // We don't have a nice syntax for adding these directly on the exports object. Give up on fixing the whole thing:
145
+ // property.kind === 'get':
146
+ // module.exports = { get foo() { ... } }
147
+ // property.kind === 'set':
148
+ // module.exports = { set foo() { ... } }
149
+ // property.type === 'SpreadElement':
150
+ // module.exports = { ...foo }
151
+ return null
152
+ }
153
+
154
+ let fixedValue = sourceCode.getText(property.value)
155
+ if (property.method) {
156
+ fixedValue = `function${
157
+ property.value.generator ? "*" : ""
158
+ } ${fixedValue}`
159
+ if (property.value.async) {
160
+ fixedValue = `async ${fixedValue}`
161
+ }
162
+ }
163
+ const lines = sourceCode
164
+ .getCommentsBefore(property)
165
+ .map(comment => sourceCode.getText(comment))
166
+ if (property.key.type === "Literal" || property.computed) {
167
+ // String or dynamic key:
168
+ // module.exports = { [ ... ]: ... } or { "foo": ... }
169
+ lines.push(
170
+ `exports[${sourceCode.getText(property.key)}] = ${fixedValue};`
171
+ )
172
+ } else if (property.key.type === "Identifier") {
173
+ // Regular identifier:
174
+ // module.exports = { foo: ... }
175
+ lines.push(`exports.${property.key.name} = ${fixedValue};`)
176
+ } else {
177
+ // Some other unknown property type. Conservatively give up on fixing the whole thing.
178
+ return null
179
+ }
180
+ lines.push(
181
+ ...sourceCode
182
+ .getCommentsAfter(property)
183
+ .map(comment => sourceCode.getText(comment))
184
+ )
185
+ return lines.join("\n")
186
+ }
187
+
188
+ // Check for a top level module.exports = { ... }
189
+ function isModuleExportsObjectAssignment(node) {
190
+ return (
191
+ node.parent.type === "AssignmentExpression" &&
192
+ node.parent.parent.type === "ExpressionStatement" &&
193
+ node.parent.parent.parent.type === "Program" &&
194
+ node.parent.right.type === "ObjectExpression"
195
+ )
196
+ }
197
+
198
+ // Check for module.exports.foo or module.exports.bar reference or assignment
199
+ function isModuleExportsReference(node) {
200
+ return (
201
+ node.parent.type === "MemberExpression" && node.parent.object === node
202
+ )
203
+ }
204
+
205
+ function fixModuleExports(node, sourceCode, fixer) {
206
+ if (isModuleExportsReference(node)) {
207
+ return fixer.replaceText(node, "exports")
208
+ }
209
+ if (!isModuleExportsObjectAssignment(node)) {
210
+ return null
211
+ }
212
+ const statements = []
213
+ const properties = node.parent.right.properties
214
+ for (const property of properties) {
215
+ const statement = getReplacementForProperty(property, sourceCode)
216
+ if (statement) {
217
+ statements.push(statement)
218
+ } else {
219
+ // No replacement available, give up on the whole thing
220
+ return null
221
+ }
222
+ }
223
+ return fixer.replaceText(node.parent, statements.join("\n\n"))
224
+ }
225
+
142
226
  module.exports = {
143
227
  meta: {
144
228
  docs: {
@@ -148,7 +232,7 @@ module.exports = {
148
232
  url: "https://github.com/weiran-zsd/eslint-plugin-node/blob/HEAD/docs/rules/exports-style.md",
149
233
  },
150
234
  type: "suggestion",
151
- fixable: null,
235
+ fixable: "code",
152
236
  schema: [
153
237
  {
154
238
  //
@@ -253,6 +337,9 @@ module.exports = {
253
337
  loc: getLocation(node),
254
338
  message:
255
339
  "Unexpected access to 'module.exports'. Use 'exports' instead.",
340
+ fix(fixer) {
341
+ return fixModuleExports(node, sourceCode, fixer)
342
+ },
256
343
  })
257
344
  }
258
345
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "eslint-plugin-n",
3
- "version": "15.1.0",
3
+ "version": "15.2.0",
4
4
  "description": "Additional ESLint's rules for Node.js",
5
5
  "engines": {
6
6
  "node": ">=12.22.0"