eslint-plugin-n 14.0.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.
Files changed (69) hide show
  1. package/LICENSE +22 -0
  2. package/README.md +189 -0
  3. package/lib/configs/_commons.js +76 -0
  4. package/lib/configs/recommended-module.js +27 -0
  5. package/lib/configs/recommended-script.js +24 -0
  6. package/lib/configs/recommended.js +18 -0
  7. package/lib/index.js +55 -0
  8. package/lib/rules/callback-return.js +185 -0
  9. package/lib/rules/exports-style.js +297 -0
  10. package/lib/rules/file-extension-in-import.js +129 -0
  11. package/lib/rules/global-require.js +91 -0
  12. package/lib/rules/handle-callback-err.js +94 -0
  13. package/lib/rules/no-callback-literal.js +82 -0
  14. package/lib/rules/no-deprecated-api.js +782 -0
  15. package/lib/rules/no-exports-assign.js +75 -0
  16. package/lib/rules/no-extraneous-import.js +49 -0
  17. package/lib/rules/no-extraneous-require.js +49 -0
  18. package/lib/rules/no-hide-core-modules.js +157 -0
  19. package/lib/rules/no-missing-import.js +47 -0
  20. package/lib/rules/no-missing-require.js +47 -0
  21. package/lib/rules/no-mixed-requires.js +255 -0
  22. package/lib/rules/no-new-require.js +39 -0
  23. package/lib/rules/no-path-concat.js +212 -0
  24. package/lib/rules/no-process-env.js +45 -0
  25. package/lib/rules/no-process-exit.js +36 -0
  26. package/lib/rules/no-restricted-import.js +61 -0
  27. package/lib/rules/no-restricted-require.js +61 -0
  28. package/lib/rules/no-sync.js +53 -0
  29. package/lib/rules/no-unpublished-bin.js +97 -0
  30. package/lib/rules/no-unpublished-import.js +49 -0
  31. package/lib/rules/no-unpublished-require.js +49 -0
  32. package/lib/rules/no-unsupported-features/es-builtins.js +181 -0
  33. package/lib/rules/no-unsupported-features/es-syntax.js +659 -0
  34. package/lib/rules/no-unsupported-features/node-builtins.js +417 -0
  35. package/lib/rules/no-unsupported-features.js +1542 -0
  36. package/lib/rules/prefer-global/buffer.js +49 -0
  37. package/lib/rules/prefer-global/console.js +46 -0
  38. package/lib/rules/prefer-global/process.js +46 -0
  39. package/lib/rules/prefer-global/text-decoder.js +49 -0
  40. package/lib/rules/prefer-global/text-encoder.js +49 -0
  41. package/lib/rules/prefer-global/url-search-params.js +49 -0
  42. package/lib/rules/prefer-global/url.js +48 -0
  43. package/lib/rules/prefer-promises/dns.js +74 -0
  44. package/lib/rules/prefer-promises/fs.js +76 -0
  45. package/lib/rules/process-exit-as-throw.js +164 -0
  46. package/lib/rules/shebang.js +170 -0
  47. package/lib/util/cache.js +58 -0
  48. package/lib/util/check-existence.js +40 -0
  49. package/lib/util/check-extraneous.js +52 -0
  50. package/lib/util/check-prefer-global.js +63 -0
  51. package/lib/util/check-publish.js +71 -0
  52. package/lib/util/check-restricted.js +109 -0
  53. package/lib/util/check-unsupported-builtins.js +108 -0
  54. package/lib/util/enumerate-property-names.js +39 -0
  55. package/lib/util/exists.js +58 -0
  56. package/lib/util/get-allow-modules.js +47 -0
  57. package/lib/util/get-configured-node-version.js +39 -0
  58. package/lib/util/get-convert-path.js +189 -0
  59. package/lib/util/get-npmignore.js +184 -0
  60. package/lib/util/get-package-json.js +75 -0
  61. package/lib/util/get-resolve-paths.js +44 -0
  62. package/lib/util/get-semver-range.js +30 -0
  63. package/lib/util/get-try-extensions.js +47 -0
  64. package/lib/util/import-target.js +85 -0
  65. package/lib/util/merge-visitors-in-place.js +46 -0
  66. package/lib/util/strip-import-path-params.js +10 -0
  67. package/lib/util/visit-import.js +66 -0
  68. package/lib/util/visit-require.js +60 -0
  69. package/package.json +91 -0
package/LICENSE ADDED
@@ -0,0 +1,22 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2015 Toru Nagashima
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
22
+
package/README.md ADDED
@@ -0,0 +1,189 @@
1
+ # eslint-plugin-n
2
+
3
+ > forked from [eslint-plugin-node](https://www.npmjs.com/package/eslint-plugin-node) v11.1.0. as the original repository seems [unmaintained](https://github.com/mysticatea/eslint-plugin-node/issues/300).
4
+
5
+ [![npm version](https://img.shields.io/npm/v/eslint-plugin-node.svg)](https://www.npmjs.com/package/eslint-plugin-node)
6
+ [![Downloads/month](https://img.shields.io/npm/dm/eslint-plugin-node.svg)](http://www.npmtrends.com/eslint-plugin-node)
7
+ [![Build Status](https://github.com/mysticatea/eslint-plugin-node/workflows/CI/badge.svg)](https://github.com/mysticatea/eslint-plugin-node/actions)
8
+ [![Coverage Status](https://codecov.io/gh/mysticatea/eslint-plugin-node/branch/master/graph/badge.svg)](https://codecov.io/gh/mysticatea/eslint-plugin-node)
9
+ [![Dependency Status](https://david-dm.org/mysticatea/eslint-plugin-node.svg)](https://david-dm.org/mysticatea/eslint-plugin-node)
10
+
11
+ Additional ESLint's rules for Node.js
12
+
13
+ ## 💿 Install & Usage
14
+
15
+ ```
16
+ $ npm install --save-dev eslint eslint-plugin-node
17
+ ```
18
+
19
+ - Requires Node.js `>=8.10.0`
20
+ - Requires ESLint `>=5.16.0`
21
+
22
+ **Note:** It recommends a use of [the "engines" field of package.json](https://docs.npmjs.com/files/package.json#engines). The "engines" field is used by `node/no-unsupported-features/*` rules.
23
+
24
+ **.eslintrc.json** (An example)
25
+
26
+ ```jsonc
27
+ {
28
+ "extends": [
29
+ "eslint:recommended",
30
+ "plugin:node/recommended"
31
+ ],
32
+ "parserOptions": {
33
+ // Only ESLint 6.2.0 and later support ES2020.
34
+ "ecmaVersion": 2020
35
+ },
36
+ "rules": {
37
+ "node/exports-style": ["error", "module.exports"],
38
+ "node/file-extension-in-import": ["error", "always"],
39
+ "node/prefer-global/buffer": ["error", "always"],
40
+ "node/prefer-global/console": ["error", "always"],
41
+ "node/prefer-global/process": ["error", "always"],
42
+ "node/prefer-global/url-search-params": ["error", "always"],
43
+ "node/prefer-global/url": ["error", "always"],
44
+ "node/prefer-promises/dns": "error",
45
+ "node/prefer-promises/fs": "error"
46
+ }
47
+ }
48
+ ```
49
+
50
+ **package.json** (An example)
51
+
52
+ ```json
53
+ {
54
+ "name": "your-module",
55
+ "version": "1.0.0",
56
+ "type": "commonjs",
57
+ "engines": {
58
+ "node": ">=8.10.0"
59
+ }
60
+ }
61
+ ```
62
+
63
+ ## 📖 Rules
64
+
65
+ - ⭐️ - the mark of recommended rules.
66
+ - ✒️ - the mark of fixable rules.
67
+
68
+ <!--RULES_TABLE_START-->
69
+ ### Possible Errors
70
+
71
+ | Rule ID | Description | |
72
+ |:--------|:------------|:--:|
73
+ | [node/handle-callback-err](./docs/rules/handle-callback-err.md) | require error handling in callbacks | |
74
+ | [node/no-callback-literal](./docs/rules/no-callback-literal.md) | ensure Node.js-style error-first callback pattern is followed | |
75
+ | [node/no-exports-assign](./docs/rules/no-exports-assign.md) | disallow the assignment to `exports` | ⭐️ |
76
+ | [node/no-extraneous-import](./docs/rules/no-extraneous-import.md) | disallow `import` declarations which import extraneous modules | ⭐️ |
77
+ | [node/no-extraneous-require](./docs/rules/no-extraneous-require.md) | disallow `require()` expressions which import extraneous modules | ⭐️ |
78
+ | [node/no-missing-import](./docs/rules/no-missing-import.md) | disallow `import` declarations which import non-existence modules | ⭐️ |
79
+ | [node/no-missing-require](./docs/rules/no-missing-require.md) | disallow `require()` expressions which import non-existence modules | ⭐️ |
80
+ | [node/no-new-require](./docs/rules/no-new-require.md) | disallow `new` operators with calls to `require` | |
81
+ | [node/no-path-concat](./docs/rules/no-path-concat.md) | disallow string concatenation with `__dirname` and `__filename` | |
82
+ | [node/no-process-exit](./docs/rules/no-process-exit.md) | disallow the use of `process.exit()` | |
83
+ | [node/no-unpublished-bin](./docs/rules/no-unpublished-bin.md) | disallow `bin` files that npm ignores | ⭐️ |
84
+ | [node/no-unpublished-import](./docs/rules/no-unpublished-import.md) | disallow `import` declarations which import private modules | ⭐️ |
85
+ | [node/no-unpublished-require](./docs/rules/no-unpublished-require.md) | disallow `require()` expressions which import private modules | ⭐️ |
86
+ | [node/no-unsupported-features/es-builtins](./docs/rules/no-unsupported-features/es-builtins.md) | disallow unsupported ECMAScript built-ins on the specified version | ⭐️ |
87
+ | [node/no-unsupported-features/es-syntax](./docs/rules/no-unsupported-features/es-syntax.md) | disallow unsupported ECMAScript syntax on the specified version | ⭐️ |
88
+ | [node/no-unsupported-features/node-builtins](./docs/rules/no-unsupported-features/node-builtins.md) | disallow unsupported Node.js built-in APIs on the specified version | ⭐️ |
89
+ | [node/process-exit-as-throw](./docs/rules/process-exit-as-throw.md) | make `process.exit()` expressions the same code path as `throw` | ⭐️ |
90
+ | [node/shebang](./docs/rules/shebang.md) | suggest correct usage of shebang | ⭐️✒️ |
91
+
92
+ ### Best Practices
93
+
94
+ | Rule ID | Description | |
95
+ |:--------|:------------|:--:|
96
+ | [node/no-deprecated-api](./docs/rules/no-deprecated-api.md) | disallow deprecated APIs | ⭐️ |
97
+
98
+ ### Stylistic Issues
99
+
100
+ | Rule ID | Description | |
101
+ |:--------|:------------|:--:|
102
+ | [node/callback-return](./docs/rules/callback-return.md) | require `return` statements after callbacks | |
103
+ | [node/exports-style](./docs/rules/exports-style.md) | enforce either `module.exports` or `exports` | |
104
+ | [node/file-extension-in-import](./docs/rules/file-extension-in-import.md) | enforce the style of file extensions in `import` declarations | ✒️ |
105
+ | [node/global-require](./docs/rules/global-require.md) | require `require()` calls to be placed at top-level module scope | |
106
+ | [node/no-mixed-requires](./docs/rules/no-mixed-requires.md) | disallow `require` calls to be mixed with regular variable declarations | |
107
+ | [node/no-process-env](./docs/rules/no-process-env.md) | disallow the use of `process.env` | |
108
+ | [node/no-restricted-import](./docs/rules/no-restricted-import.md) | disallow specified modules when loaded by `import` declarations | |
109
+ | [node/no-restricted-require](./docs/rules/no-restricted-require.md) | disallow specified modules when loaded by `require` | |
110
+ | [node/no-sync](./docs/rules/no-sync.md) | disallow synchronous methods | |
111
+ | [node/prefer-global/buffer](./docs/rules/prefer-global/buffer.md) | enforce either `Buffer` or `require("buffer").Buffer` | |
112
+ | [node/prefer-global/console](./docs/rules/prefer-global/console.md) | enforce either `console` or `require("console")` | |
113
+ | [node/prefer-global/process](./docs/rules/prefer-global/process.md) | enforce either `process` or `require("process")` | |
114
+ | [node/prefer-global/text-decoder](./docs/rules/prefer-global/text-decoder.md) | enforce either `TextDecoder` or `require("util").TextDecoder` | |
115
+ | [node/prefer-global/text-encoder](./docs/rules/prefer-global/text-encoder.md) | enforce either `TextEncoder` or `require("util").TextEncoder` | |
116
+ | [node/prefer-global/url-search-params](./docs/rules/prefer-global/url-search-params.md) | enforce either `URLSearchParams` or `require("url").URLSearchParams` | |
117
+ | [node/prefer-global/url](./docs/rules/prefer-global/url.md) | enforce either `URL` or `require("url").URL` | |
118
+ | [node/prefer-promises/dns](./docs/rules/prefer-promises/dns.md) | enforce `require("dns").promises` | |
119
+ | [node/prefer-promises/fs](./docs/rules/prefer-promises/fs.md) | enforce `require("fs").promises` | |
120
+
121
+ ### Deprecated rules
122
+
123
+ These rules have been deprecated in accordance with the [deprecation policy](https://eslint.org/docs/user-guide/rule-deprecation), and replaced by newer rules:
124
+
125
+ | Rule ID | Replaced by |
126
+ |:--------|:------------|
127
+ | [node/no-hide-core-modules](./docs/rules/no-hide-core-modules.md) | (nothing) |
128
+ | [node/no-unsupported-features](./docs/rules/no-unsupported-features.md) | [node/no-unsupported-features/es-syntax](./docs/rules/no-unsupported-features/es-syntax.md) and [node/no-unsupported-features/es-builtins](./docs/rules/no-unsupported-features/es-builtins.md) |
129
+
130
+ <!--RULES_TABLE_END-->
131
+
132
+ ## 🔧 Configs
133
+
134
+ This plugin provides three configs:
135
+
136
+ - `plugin:node/recommended` considers both CommonJS and ES Modules. If [`"type":"module"` field](https://medium.com/@nodejs/announcing-a-new-experimental-modules-1be8d2d6c2ff#b023) existed in package.json then it considers files as ES Modules. Otherwise it considers files as CommonJS. In addition, it considers `*.mjs` files as ES Modules and `*.cjs` files as CommonJS.
137
+ - `plugin:node/recommended-module` considers all files as ES Modules.
138
+ - `plugin:node/recommended-script` considers all files as CommonJS.
139
+
140
+ Those preset config:
141
+
142
+ - enable [no-process-exit](http://eslint.org/docs/rules/no-process-exit) rule because [the official document](https://nodejs.org/api/process.html#process_process_exit_code) does not recommend a use of `process.exit()`.
143
+ - enable plugin rules which are given :star: in the above table.
144
+ - add `{ecmaVersion: 2019}` and etc into `parserOptions`.
145
+ - add proper globals into `globals`.
146
+ - add this plugin into `plugins`.
147
+
148
+ ## 👫 FAQ
149
+
150
+ - Q: The `no-missing-import` / `no-missing-require` rules don't work with nested folders in SublimeLinter-eslint
151
+ - A: See [context.getFilename() in rule returns relative path](https://github.com/roadhump/SublimeLinter-eslint#contextgetfilename-in-rule-returns-relative-path) in the SublimeLinter-eslint FAQ.
152
+
153
+ ## 🚥 Semantic Versioning Policy
154
+
155
+ `eslint-plugin-node` follows [semantic versioning](http://semver.org/) and [ESLint's Semantic Versioning Policy](https://github.com/eslint/eslint#semantic-versioning-policy).
156
+
157
+ - Patch release (intended to not break your lint build)
158
+ - A bug fix in a rule that results in it reporting fewer errors.
159
+ - Improvements to documentation.
160
+ - Non-user-facing changes such as refactoring code, adding, deleting, or modifying tests, and increasing test coverage.
161
+ - Re-releasing after a failed release (i.e., publishing a release that doesn't work for anyone).
162
+ - Minor release (might break your lint build)
163
+ - A bug fix in a rule that results in it reporting more errors.
164
+ - A new rule is created.
165
+ - A new option to an existing rule is created.
166
+ - An existing rule is deprecated.
167
+ - Major release (likely to break your lint build)
168
+ - A support for old Node version is dropped.
169
+ - A support for old ESLint version is dropped.
170
+ - An existing rule is changed in it reporting more errors.
171
+ - An existing rule is removed.
172
+ - An existing option of a rule is removed.
173
+ - An existing config is updated.
174
+
175
+ ## 📰 Changelog
176
+
177
+ - [GitHub Releases](https://github.com/mysticatea/eslint-plugin-node/releases)
178
+
179
+ ## ❤️ Contributing
180
+
181
+ Welcome contributing!
182
+
183
+ Please use GitHub's Issues/PRs.
184
+
185
+ ### Development Tools
186
+
187
+ - `npm test` runs tests and measures coverage.
188
+ - `npm run coverage` shows the coverage result of `npm test` command.
189
+ - `npm run clean` removes the coverage result of `npm test` command.
@@ -0,0 +1,76 @@
1
+ "use strict"
2
+
3
+ module.exports = {
4
+ commonGlobals: {
5
+ // ECMAScript
6
+ ArrayBuffer: "readonly",
7
+ Atomics: "readonly",
8
+ BigInt: "readonly",
9
+ BigInt64Array: "readonly",
10
+ BigUint64Array: "readonly",
11
+ DataView: "readonly",
12
+ Float32Array: "readonly",
13
+ Float64Array: "readonly",
14
+ Int16Array: "readonly",
15
+ Int32Array: "readonly",
16
+ Int8Array: "readonly",
17
+ Map: "readonly",
18
+ Promise: "readonly",
19
+ Proxy: "readonly",
20
+ Reflect: "readonly",
21
+ Set: "readonly",
22
+ SharedArrayBuffer: "readonly",
23
+ Symbol: "readonly",
24
+ Uint16Array: "readonly",
25
+ Uint32Array: "readonly",
26
+ Uint8Array: "readonly",
27
+ Uint8ClampedArray: "readonly",
28
+ WeakMap: "readonly",
29
+ WeakSet: "readonly",
30
+
31
+ // ECMAScript (experimental)
32
+ globalThis: "readonly",
33
+
34
+ // ECMA-402
35
+ Intl: "readonly",
36
+
37
+ // Web Standard
38
+ TextDecoder: "readonly",
39
+ TextEncoder: "readonly",
40
+ URL: "readonly",
41
+ URLSearchParams: "readonly",
42
+ WebAssembly: "readonly",
43
+ clearInterval: "readonly",
44
+ clearTimeout: "readonly",
45
+ console: "readonly",
46
+ queueMicrotask: "readonly",
47
+ setInterval: "readonly",
48
+ setTimeout: "readonly",
49
+
50
+ // Node.js
51
+ Buffer: "readonly",
52
+ GLOBAL: "readonly",
53
+ clearImmediate: "readonly",
54
+ global: "readonly",
55
+ process: "readonly",
56
+ root: "readonly",
57
+ setImmediate: "readonly",
58
+ },
59
+ commonRules: {
60
+ "n/no-deprecated-api": "error",
61
+ "n/no-extraneous-import": "error",
62
+ "n/no-extraneous-require": "error",
63
+ "n/no-exports-assign": "error",
64
+ "n/no-missing-import": "error",
65
+ "n/no-missing-require": "error",
66
+ "n/no-process-exit": "error",
67
+ "n/no-unpublished-bin": "error",
68
+ "n/no-unpublished-import": "error",
69
+ "n/no-unpublished-require": "error",
70
+ "n/no-unsupported-features/es-builtins": "error",
71
+ "n/no-unsupported-features/es-syntax": "error",
72
+ "n/no-unsupported-features/node-builtins": "error",
73
+ "n/process-exit-as-throw": "error",
74
+ "n/shebang": "error",
75
+ },
76
+ }
@@ -0,0 +1,27 @@
1
+ "use strict"
2
+
3
+ const { commonGlobals, commonRules } = require("./_commons")
4
+
5
+ module.exports = {
6
+ globals: {
7
+ ...commonGlobals,
8
+ __dirname: "off",
9
+ __filename: "off",
10
+ exports: "off",
11
+ module: "off",
12
+ require: "off",
13
+ },
14
+ parserOptions: {
15
+ ecmaFeatures: { globalReturn: false },
16
+ ecmaVersion: 2019,
17
+ sourceType: "module",
18
+ },
19
+ plugins: ["n"],
20
+ rules: {
21
+ ...commonRules,
22
+ "n/no-unsupported-features/es-syntax": [
23
+ "error",
24
+ { ignores: ["modules"] },
25
+ ],
26
+ },
27
+ }
@@ -0,0 +1,24 @@
1
+ "use strict"
2
+
3
+ const { commonGlobals, commonRules } = require("./_commons")
4
+
5
+ module.exports = {
6
+ globals: {
7
+ ...commonGlobals,
8
+ __dirname: "readonly",
9
+ __filename: "readonly",
10
+ exports: "writable",
11
+ module: "readonly",
12
+ require: "readonly",
13
+ },
14
+ parserOptions: {
15
+ ecmaFeatures: { globalReturn: true },
16
+ ecmaVersion: 2019,
17
+ sourceType: "script",
18
+ },
19
+ plugins: ["n"],
20
+ rules: {
21
+ ...commonRules,
22
+ "n/no-unsupported-features/es-syntax": ["error", { ignores: [] }],
23
+ },
24
+ }
@@ -0,0 +1,18 @@
1
+ "use strict"
2
+
3
+ const getPackageJson = require("../util/get-package-json")
4
+ const moduleConfig = require("./recommended-module")
5
+ const scriptConfig = require("./recommended-script")
6
+
7
+ module.exports = () => {
8
+ const packageJson = getPackageJson()
9
+ const isModule = (packageJson && packageJson.type) === "module"
10
+
11
+ return {
12
+ ...(isModule ? moduleConfig : scriptConfig),
13
+ overrides: [
14
+ { files: ["*.cjs", ".*.cjs"], ...scriptConfig },
15
+ { files: ["*.mjs", ".*.mjs"], ...moduleConfig },
16
+ ],
17
+ }
18
+ }
package/lib/index.js ADDED
@@ -0,0 +1,55 @@
1
+ /* DON'T EDIT THIS FILE. This is generated by 'scripts/update-lib-index.js' */
2
+ "use strict"
3
+
4
+ module.exports = {
5
+ configs: {
6
+ "recommended-module": require("./configs/recommended-module"),
7
+ "recommended-script": require("./configs/recommended-script"),
8
+ get recommended() {
9
+ return require("./configs/recommended")()
10
+ },
11
+ },
12
+ rules: {
13
+ "callback-return": require("./rules/callback-return"),
14
+ "exports-style": require("./rules/exports-style"),
15
+ "file-extension-in-import": require("./rules/file-extension-in-import"),
16
+ "global-require": require("./rules/global-require"),
17
+ "handle-callback-err": require("./rules/handle-callback-err"),
18
+ "no-callback-literal": require("./rules/no-callback-literal"),
19
+ "no-deprecated-api": require("./rules/no-deprecated-api"),
20
+ "no-exports-assign": require("./rules/no-exports-assign"),
21
+ "no-extraneous-import": require("./rules/no-extraneous-import"),
22
+ "no-extraneous-require": require("./rules/no-extraneous-require"),
23
+ "no-missing-import": require("./rules/no-missing-import"),
24
+ "no-missing-require": require("./rules/no-missing-require"),
25
+ "no-mixed-requires": require("./rules/no-mixed-requires"),
26
+ "no-new-require": require("./rules/no-new-require"),
27
+ "no-path-concat": require("./rules/no-path-concat"),
28
+ "no-process-env": require("./rules/no-process-env"),
29
+ "no-process-exit": require("./rules/no-process-exit"),
30
+ "no-restricted-import": require("./rules/no-restricted-import"),
31
+ "no-restricted-require": require("./rules/no-restricted-require"),
32
+ "no-sync": require("./rules/no-sync"),
33
+ "no-unpublished-bin": require("./rules/no-unpublished-bin"),
34
+ "no-unpublished-import": require("./rules/no-unpublished-import"),
35
+ "no-unpublished-require": require("./rules/no-unpublished-require"),
36
+ "no-unsupported-features/es-builtins": require("./rules/no-unsupported-features/es-builtins"),
37
+ "no-unsupported-features/es-syntax": require("./rules/no-unsupported-features/es-syntax"),
38
+ "no-unsupported-features/node-builtins": require("./rules/no-unsupported-features/node-builtins"),
39
+ "prefer-global/buffer": require("./rules/prefer-global/buffer"),
40
+ "prefer-global/console": require("./rules/prefer-global/console"),
41
+ "prefer-global/process": require("./rules/prefer-global/process"),
42
+ "prefer-global/text-decoder": require("./rules/prefer-global/text-decoder"),
43
+ "prefer-global/text-encoder": require("./rules/prefer-global/text-encoder"),
44
+ "prefer-global/url-search-params": require("./rules/prefer-global/url-search-params"),
45
+ "prefer-global/url": require("./rules/prefer-global/url"),
46
+ "prefer-promises/dns": require("./rules/prefer-promises/dns"),
47
+ "prefer-promises/fs": require("./rules/prefer-promises/fs"),
48
+ "process-exit-as-throw": require("./rules/process-exit-as-throw"),
49
+ "shebang": require("./rules/shebang"),
50
+
51
+ // Deprecated rules.
52
+ "no-hide-core-modules": require("./rules/no-hide-core-modules"),
53
+ "no-unsupported-features": require("./rules/no-unsupported-features"),
54
+ },
55
+ }
@@ -0,0 +1,185 @@
1
+ /**
2
+ * @author Jamund Ferguson
3
+ * See LICENSE file in root directory for full license.
4
+ */
5
+ "use strict"
6
+
7
+ module.exports = {
8
+ meta: {
9
+ type: "suggestion",
10
+ docs: {
11
+ description: "require `return` statements after callbacks",
12
+ category: "Stylistic Issues",
13
+ recommended: false,
14
+ url:
15
+ "https://github.com/mysticatea/eslint-plugin-node/blob/HEAD/docs/rules/callback-return.md",
16
+ },
17
+ schema: [
18
+ {
19
+ type: "array",
20
+ items: { type: "string" },
21
+ },
22
+ ],
23
+ fixable: null,
24
+ messages: {
25
+ missingReturn: "Expected return with your callback function.",
26
+ },
27
+ },
28
+
29
+ create(context) {
30
+ const callbacks = context.options[0] || ["callback", "cb", "next"]
31
+ const sourceCode = context.getSourceCode()
32
+
33
+ /**
34
+ * Find the closest parent matching a list of types.
35
+ * @param {ASTNode} node The node whose parents we are searching
36
+ * @param {Array} types The node types to match
37
+ * @returns {ASTNode} The matched node or undefined.
38
+ */
39
+ function findClosestParentOfType(node, types) {
40
+ if (!node.parent) {
41
+ return null
42
+ }
43
+ if (types.indexOf(node.parent.type) === -1) {
44
+ return findClosestParentOfType(node.parent, types)
45
+ }
46
+ return node.parent
47
+ }
48
+
49
+ /**
50
+ * Check to see if a node contains only identifers
51
+ * @param {ASTNode} node The node to check
52
+ * @returns {boolean} Whether or not the node contains only identifers
53
+ */
54
+ function containsOnlyIdentifiers(node) {
55
+ if (node.type === "Identifier") {
56
+ return true
57
+ }
58
+
59
+ if (node.type === "MemberExpression") {
60
+ if (node.object.type === "Identifier") {
61
+ return true
62
+ }
63
+ if (node.object.type === "MemberExpression") {
64
+ return containsOnlyIdentifiers(node.object)
65
+ }
66
+ }
67
+
68
+ return false
69
+ }
70
+
71
+ /**
72
+ * Check to see if a CallExpression is in our callback list.
73
+ * @param {ASTNode} node The node to check against our callback names list.
74
+ * @returns {boolean} Whether or not this function matches our callback name.
75
+ */
76
+ function isCallback(node) {
77
+ return (
78
+ containsOnlyIdentifiers(node.callee) &&
79
+ callbacks.indexOf(sourceCode.getText(node.callee)) > -1
80
+ )
81
+ }
82
+
83
+ /**
84
+ * Determines whether or not the callback is part of a callback expression.
85
+ * @param {ASTNode} node The callback node
86
+ * @param {ASTNode} parentNode The expression node
87
+ * @returns {boolean} Whether or not this is part of a callback expression
88
+ */
89
+ function isCallbackExpression(node, parentNode) {
90
+ // ensure the parent node exists and is an expression
91
+ if (!parentNode || parentNode.type !== "ExpressionStatement") {
92
+ return false
93
+ }
94
+
95
+ // cb()
96
+ if (parentNode.expression === node) {
97
+ return true
98
+ }
99
+
100
+ // special case for cb && cb() and similar
101
+ if (
102
+ parentNode.expression.type === "BinaryExpression" ||
103
+ parentNode.expression.type === "LogicalExpression"
104
+ ) {
105
+ if (parentNode.expression.right === node) {
106
+ return true
107
+ }
108
+ }
109
+
110
+ return false
111
+ }
112
+
113
+ return {
114
+ CallExpression(node) {
115
+ // if we're not a callback we can return
116
+ if (!isCallback(node)) {
117
+ return
118
+ }
119
+
120
+ // find the closest block, return or loop
121
+ const closestBlock =
122
+ findClosestParentOfType(node, [
123
+ "BlockStatement",
124
+ "ReturnStatement",
125
+ "ArrowFunctionExpression",
126
+ ]) || {}
127
+
128
+ // if our parent is a return we know we're ok
129
+ if (closestBlock.type === "ReturnStatement") {
130
+ return
131
+ }
132
+
133
+ // arrow functions don't always have blocks and implicitly return
134
+ if (closestBlock.type === "ArrowFunctionExpression") {
135
+ return
136
+ }
137
+
138
+ // block statements are part of functions and most if statements
139
+ if (closestBlock.type === "BlockStatement") {
140
+ // find the last item in the block
141
+ const lastItem =
142
+ closestBlock.body[closestBlock.body.length - 1]
143
+
144
+ // if the callback is the last thing in a block that might be ok
145
+ if (isCallbackExpression(node, lastItem)) {
146
+ const parentType = closestBlock.parent.type
147
+
148
+ // but only if the block is part of a function
149
+ if (
150
+ parentType === "FunctionExpression" ||
151
+ parentType === "FunctionDeclaration" ||
152
+ parentType === "ArrowFunctionExpression"
153
+ ) {
154
+ return
155
+ }
156
+ }
157
+
158
+ // ending a block with a return is also ok
159
+ if (lastItem.type === "ReturnStatement") {
160
+ // but only if the callback is immediately before
161
+ if (
162
+ isCallbackExpression(
163
+ node,
164
+ closestBlock.body[closestBlock.body.length - 2]
165
+ )
166
+ ) {
167
+ return
168
+ }
169
+ }
170
+ }
171
+
172
+ // as long as you're the child of a function at this point you should be asked to return
173
+ if (
174
+ findClosestParentOfType(node, [
175
+ "FunctionDeclaration",
176
+ "FunctionExpression",
177
+ "ArrowFunctionExpression",
178
+ ])
179
+ ) {
180
+ context.report({ node, messageId: "missingReturn" })
181
+ }
182
+ },
183
+ }
184
+ },
185
+ }