Socket
Socket
Sign inDemoInstall

eslint-plugin-n

Package Overview
Dependencies
Maintainers
1
Versions
59
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

eslint-plugin-n - npm Package Compare versions

Comparing version 15.1.0 to 15.2.0

89

lib/rules/exports-style.js

@@ -142,2 +142,86 @@ /**

function getReplacementForProperty(property, sourceCode) {
if (property.type !== "Property" || property.kind !== "init") {
// We don't have a nice syntax for adding these directly on the exports object. Give up on fixing the whole thing:
// property.kind === 'get':
// module.exports = { get foo() { ... } }
// property.kind === 'set':
// module.exports = { set foo() { ... } }
// property.type === 'SpreadElement':
// module.exports = { ...foo }
return null
}
let fixedValue = sourceCode.getText(property.value)
if (property.method) {
fixedValue = `function${
property.value.generator ? "*" : ""
} ${fixedValue}`
if (property.value.async) {
fixedValue = `async ${fixedValue}`
}
}
const lines = sourceCode
.getCommentsBefore(property)
.map(comment => sourceCode.getText(comment))
if (property.key.type === "Literal" || property.computed) {
// String or dynamic key:
// module.exports = { [ ... ]: ... } or { "foo": ... }
lines.push(
`exports[${sourceCode.getText(property.key)}] = ${fixedValue};`
)
} else if (property.key.type === "Identifier") {
// Regular identifier:
// module.exports = { foo: ... }
lines.push(`exports.${property.key.name} = ${fixedValue};`)
} else {
// Some other unknown property type. Conservatively give up on fixing the whole thing.
return null
}
lines.push(
...sourceCode
.getCommentsAfter(property)
.map(comment => sourceCode.getText(comment))
)
return lines.join("\n")
}
// Check for a top level module.exports = { ... }
function isModuleExportsObjectAssignment(node) {
return (
node.parent.type === "AssignmentExpression" &&
node.parent.parent.type === "ExpressionStatement" &&
node.parent.parent.parent.type === "Program" &&
node.parent.right.type === "ObjectExpression"
)
}
// Check for module.exports.foo or module.exports.bar reference or assignment
function isModuleExportsReference(node) {
return (
node.parent.type === "MemberExpression" && node.parent.object === node
)
}
function fixModuleExports(node, sourceCode, fixer) {
if (isModuleExportsReference(node)) {
return fixer.replaceText(node, "exports")
}
if (!isModuleExportsObjectAssignment(node)) {
return null
}
const statements = []
const properties = node.parent.right.properties
for (const property of properties) {
const statement = getReplacementForProperty(property, sourceCode)
if (statement) {
statements.push(statement)
} else {
// No replacement available, give up on the whole thing
return null
}
}
return fixer.replaceText(node.parent, statements.join("\n\n"))
}
module.exports = {

@@ -152,3 +236,3 @@ meta: {

type: "suggestion",
fixable: null,
fixable: "code",
schema: [

@@ -258,2 +342,5 @@ {

"Unexpected access to 'module.exports'. Use 'exports' instead.",
fix(fixer) {
return fixModuleExports(node, sourceCode, fixer)
},
})

@@ -260,0 +347,0 @@ }

2

package.json
{
"name": "eslint-plugin-n",
"version": "15.1.0",
"version": "15.2.0",
"description": "Additional ESLint's rules for Node.js",

@@ -5,0 +5,0 @@ "engines": {

@@ -96,3 +96,3 @@ # eslint-plugin-n

| [n/callback-return](./docs/rules/callback-return.md) | require `return` statements after callbacks | |
| [n/exports-style](./docs/rules/exports-style.md) | enforce either `module.exports` or `exports` | |
| [n/exports-style](./docs/rules/exports-style.md) | enforce either `module.exports` or `exports` | ✒️ |
| [n/file-extension-in-import](./docs/rules/file-extension-in-import.md) | enforce the style of file extensions in `import` declarations | ✒️ |

@@ -171,3 +171,3 @@ | [n/global-require](./docs/rules/global-require.md) | require `require()` calls to be placed at top-level module scope | |

- [GitHub Releases](https://github.com/weiran-zsd/eslint-plugin-n/releases)
- [GitHub Releases](https://github.com/weiran-zsd/eslint-plugin-node/releases)

@@ -174,0 +174,0 @@ ## ❤️ Contributing

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc