eslint-plugin-th-rules 1.15.0 → 1.15.2
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/CHANGELOG.md +14 -0
- package/package.json +1 -1
- package/src/index.js +1 -0
- package/src/rules/top-level-functions.js +18 -40
- package/tests/top-level-functions.test.js +30 -0
package/CHANGELOG.md
CHANGED
|
@@ -1,3 +1,17 @@
|
|
|
1
|
+
## [1.15.2](https://github.com/tomerh2001/eslint-plugin-th-rules/compare/v1.15.1...v1.15.2) (2024-12-30)
|
|
2
|
+
|
|
3
|
+
|
|
4
|
+
### Bug Fixes
|
|
5
|
+
|
|
6
|
+
* implement top-level functions rule with tests for ESLint ([c19c451](https://github.com/tomerh2001/eslint-plugin-th-rules/commit/c19c4515b66b314cc327ef84c674a1324ad7d204))
|
|
7
|
+
|
|
8
|
+
## [1.15.1](https://github.com/tomerh2001/eslint-plugin-th-rules/compare/v1.15.0...v1.15.1) (2024-12-30)
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
### Bug Fixes
|
|
12
|
+
|
|
13
|
+
* enable top-level functions rule in ESLint configuration ([ce694ad](https://github.com/tomerh2001/eslint-plugin-th-rules/commit/ce694ad302388f13e04af8e3d951c8cf00c0603f))
|
|
14
|
+
|
|
1
15
|
# [1.15.0](https://github.com/tomerh2001/eslint-plugin-th-rules/compare/v1.14.1...v1.15.0) (2024-12-30)
|
|
2
16
|
|
|
3
17
|
|
package/package.json
CHANGED
package/src/index.js
CHANGED
|
@@ -18,6 +18,7 @@ const configs = {
|
|
|
18
18
|
'th-rules/no-destructuring': 'error',
|
|
19
19
|
'th-rules/no-default-export': 'error',
|
|
20
20
|
'th-rules/no-comments': 'error',
|
|
21
|
+
'th-rules/top-level-functions': 'error',
|
|
21
22
|
'unicorn/prefer-module': 'warn',
|
|
22
23
|
'unicorn/filename-case': 'off',
|
|
23
24
|
'unicorn/no-array-callback-reference': 'off',
|
|
@@ -1,14 +1,3 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* @type {Object}
|
|
3
|
-
* @property {string} type - The type of the rule, in this case 'suggestion'.
|
|
4
|
-
* @property {Object} docs - Documentation related to the rule.
|
|
5
|
-
* @property {string} docs.description - A brief description of the rule.
|
|
6
|
-
* @property {string} docs.category - The category of the rule, here 'Stylistic Issues'.
|
|
7
|
-
* @property {boolean} docs.recommended - Indicates whether the rule is recommended.
|
|
8
|
-
* @property {string} docs.url - The URL to the documentation of the rule.
|
|
9
|
-
* @property {string} fixable - Indicates if the rule is fixable, in this case 'code'.
|
|
10
|
-
* @property {Array} schema - The schema for the rule options.
|
|
11
|
-
*/
|
|
12
1
|
const meta = {
|
|
13
2
|
type: 'suggestion',
|
|
14
3
|
docs: {
|
|
@@ -21,39 +10,29 @@ const meta = {
|
|
|
21
10
|
schema: [],
|
|
22
11
|
};
|
|
23
12
|
|
|
24
|
-
/**
|
|
25
|
-
* ESLint rule to enforce naming of top-level functions.
|
|
26
|
-
*
|
|
27
|
-
* @param {Object} context - The ESLint rule context.
|
|
28
|
-
* @returns {Object} An object containing the rule's visitor methods.
|
|
29
|
-
*/
|
|
30
13
|
function create(context) {
|
|
31
14
|
return {
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
if (node.parent.type !== 'Program') {
|
|
15
|
+
VariableDeclarator(node) {
|
|
16
|
+
if (node.parent.parent.type !== 'Program') {
|
|
35
17
|
return;
|
|
36
18
|
}
|
|
37
19
|
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
declaration.init
|
|
41
|
-
&& (declaration.init.type === 'ArrowFunctionExpression'
|
|
42
|
-
|| declaration.init.type === 'FunctionExpression')
|
|
43
|
-
) {
|
|
44
|
-
context.report({
|
|
45
|
-
node: declaration,
|
|
46
|
-
message: 'Top-level functions must be named/regular functions.',
|
|
47
|
-
fix(fixer) {
|
|
48
|
-
const sourceCode = context.getSourceCode();
|
|
49
|
-
const functionText = sourceCode.getText(declaration.init);
|
|
50
|
-
const functionName = declaration.id.name;
|
|
51
|
-
const fixedCode = `function ${functionName}${functionText.slice(functionText.indexOf('('))}`;
|
|
52
|
-
return fixer.replaceText(declaration, fixedCode);
|
|
53
|
-
},
|
|
54
|
-
});
|
|
55
|
-
}
|
|
20
|
+
if (!(node.init && (node.init.type === 'ArrowFunctionExpression' || node.init.type === 'FunctionExpression'))) {
|
|
21
|
+
return;
|
|
56
22
|
}
|
|
23
|
+
|
|
24
|
+
const sourceCode = context.getSourceCode();
|
|
25
|
+
const functionText = sourceCode.getText(node.init);
|
|
26
|
+
const functionName = node.id.name;
|
|
27
|
+
|
|
28
|
+
context.report({
|
|
29
|
+
node: node.init,
|
|
30
|
+
message: 'Top-level functions must be named/regular functions.',
|
|
31
|
+
fix(fixer) {
|
|
32
|
+
const fixedCode = `function ${functionName}${functionText.slice(functionText.indexOf('('))}`;
|
|
33
|
+
return fixer.replaceText(node, fixedCode);
|
|
34
|
+
},
|
|
35
|
+
});
|
|
57
36
|
},
|
|
58
37
|
|
|
59
38
|
FunctionDeclaration(node) {
|
|
@@ -66,10 +45,9 @@ function create(context) {
|
|
|
66
45
|
node,
|
|
67
46
|
message: 'Top-level functions must be named.',
|
|
68
47
|
fix(fixer) {
|
|
69
|
-
const sourceCode = context.getSourceCode();
|
|
70
48
|
const functionName = 'defaultFunction';
|
|
49
|
+
const sourceCode = context.getSourceCode();
|
|
71
50
|
const functionText = sourceCode.getText(node);
|
|
72
|
-
|
|
73
51
|
const fixedCode = functionText.replace('function (', `function ${functionName}(`);
|
|
74
52
|
|
|
75
53
|
return fixer.replaceText(node, fixedCode);
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
const {RuleTester} = require('eslint');
|
|
2
|
+
const rule = require('../src/rules/top-level-functions');
|
|
3
|
+
|
|
4
|
+
const ruleTester = new RuleTester({
|
|
5
|
+
parserOptions: {ecmaVersion: 2021, sourceType: 'module'},
|
|
6
|
+
});
|
|
7
|
+
|
|
8
|
+
ruleTester.run('top-level-functions', rule, {
|
|
9
|
+
valid: [
|
|
10
|
+
'function test() {}',
|
|
11
|
+
'function anotherFunction() {}',
|
|
12
|
+
],
|
|
13
|
+
invalid: [
|
|
14
|
+
{
|
|
15
|
+
code: 'const test = () => {};',
|
|
16
|
+
errors: [{message: 'Top-level functions must be named/regular functions.'}],
|
|
17
|
+
output: 'function test() {}',
|
|
18
|
+
},
|
|
19
|
+
{
|
|
20
|
+
code: 'const response = (res, parameters = {}) => {};',
|
|
21
|
+
errors: [{message: 'Top-level functions must be named/regular functions.'}],
|
|
22
|
+
output: 'function response(res, parameters = {}) {}',
|
|
23
|
+
},
|
|
24
|
+
{
|
|
25
|
+
code: 'const func = function () {};',
|
|
26
|
+
errors: [{message: 'Top-level functions must be named/regular functions.'}],
|
|
27
|
+
output: 'function func() {}',
|
|
28
|
+
},
|
|
29
|
+
],
|
|
30
|
+
});
|