eslint-plugin-no-arrow-this 1.1.9 → 1.3.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/dist/rules/no-arrow-this.js +102 -0
- package/example/index.js +17 -18
- package/package.json +8 -12
- package/src/rules/no-arrow-this.js +89 -69
|
@@ -0,0 +1,102 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
module.exports = {
|
|
4
|
+
meta: {
|
|
5
|
+
type: 'suggestion',
|
|
6
|
+
docs: {
|
|
7
|
+
description: 'get rid of using "this" keyword in arrow functions',
|
|
8
|
+
category: 'code style~convention',
|
|
9
|
+
recommended: false
|
|
10
|
+
},
|
|
11
|
+
fixable: null,
|
|
12
|
+
schema: [
|
|
13
|
+
{
|
|
14
|
+
type: 'object',
|
|
15
|
+
properties: {
|
|
16
|
+
onlyGlobals: { type: 'boolean' }
|
|
17
|
+
},
|
|
18
|
+
additionalProperties: false
|
|
19
|
+
}
|
|
20
|
+
]
|
|
21
|
+
},
|
|
22
|
+
create (context) {
|
|
23
|
+
// lowercased name of AST Arrow Function
|
|
24
|
+
const AST_AF = 'ArrowFunctionExpression'.toLowerCase();
|
|
25
|
+
// lowercased name of AST Program Start
|
|
26
|
+
const AST_PRG = 'Program'.toLowerCase();
|
|
27
|
+
|
|
28
|
+
// Here are list of nodes for check breaking
|
|
29
|
+
// If one of them found, then everything is good
|
|
30
|
+
// And it is not necessary to check anymore else
|
|
31
|
+
const breakList = [
|
|
32
|
+
'FunctionDeclaration',
|
|
33
|
+
'FunctionExpression',
|
|
34
|
+
AST_PRG
|
|
35
|
+
].map(name => name.toLowerCase());
|
|
36
|
+
|
|
37
|
+
// This options allows to warn only situations
|
|
38
|
+
// When context of arrow function
|
|
39
|
+
// points Strict to Global or Window
|
|
40
|
+
const onlyGlobals = Boolean(
|
|
41
|
+
Array.isArray(context.options) &&
|
|
42
|
+
context.options[0] &&
|
|
43
|
+
context.options[0].onlyGlobals
|
|
44
|
+
);
|
|
45
|
+
|
|
46
|
+
// Warn message depends of onlyGlobals option
|
|
47
|
+
const name = onlyGlobals ? 'global "this"' : '"this"';
|
|
48
|
+
|
|
49
|
+
var type, parent;
|
|
50
|
+
|
|
51
|
+
// checking function depends of onlyGlobals
|
|
52
|
+
const prepareCheckFn = (node) => {
|
|
53
|
+
parent = node;
|
|
54
|
+
|
|
55
|
+
const switchParent = () => {
|
|
56
|
+
// switching AST node
|
|
57
|
+
// upper from current
|
|
58
|
+
parent = parent.parent;
|
|
59
|
+
type = parent.type.toLowerCase();
|
|
60
|
+
};
|
|
61
|
+
|
|
62
|
+
if (!onlyGlobals) {
|
|
63
|
+
return () => {
|
|
64
|
+
switchParent();
|
|
65
|
+
if (breakList.includes(type)) {
|
|
66
|
+
return null;
|
|
67
|
+
}
|
|
68
|
+
if (type === AST_AF) {
|
|
69
|
+
return true;
|
|
70
|
+
}
|
|
71
|
+
return false;
|
|
72
|
+
};
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
let found = false;
|
|
76
|
+
return () => {
|
|
77
|
+
switchParent();
|
|
78
|
+
if (breakList.includes(type)) {
|
|
79
|
+
return (found && type === AST_PRG) ? true : null;
|
|
80
|
+
}
|
|
81
|
+
if (type === AST_AF) {
|
|
82
|
+
found = true;
|
|
83
|
+
}
|
|
84
|
+
return false;
|
|
85
|
+
};
|
|
86
|
+
|
|
87
|
+
};
|
|
88
|
+
|
|
89
|
+
return {
|
|
90
|
+
ThisExpression (node) {
|
|
91
|
+
const check = prepareCheckFn(node);
|
|
92
|
+
var result = false;
|
|
93
|
+
while (result === false) {
|
|
94
|
+
result = check();
|
|
95
|
+
}
|
|
96
|
+
if (result) {
|
|
97
|
+
context.report({ node, message: `do not use ${name} in an arrow function` });
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
};
|
|
101
|
+
}
|
|
102
|
+
};
|
package/example/index.js
CHANGED
|
@@ -1,31 +1,30 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
|
-
const Linter = require('eslint')
|
|
4
|
-
const
|
|
5
|
-
|
|
6
|
-
linter.defineRules(require('../dist/index.js').rules);
|
|
3
|
+
const { Linter } = require('eslint');
|
|
4
|
+
const plugin = require('../dist/index.js');
|
|
7
5
|
|
|
6
|
+
const linter = new Linter();
|
|
8
7
|
const sample = require('fs').readFileSync('example/sample.js').toString();
|
|
9
8
|
|
|
10
|
-
const config =
|
|
9
|
+
const config = {
|
|
10
|
+
plugins: { 'no-arrow-this': plugin },
|
|
11
|
+
languageOptions: {
|
|
12
|
+
ecmaVersion: 2018,
|
|
13
|
+
sourceType: 'module'
|
|
14
|
+
},
|
|
15
|
+
rules: {}
|
|
16
|
+
};
|
|
11
17
|
|
|
12
18
|
console.log('\n1st test!');
|
|
13
19
|
console.log('All suspicious condidions:\n\n');
|
|
14
|
-
config.rules['no-arrow-this'] = 'warn';
|
|
15
|
-
|
|
16
|
-
const results1 = linter.verify(
|
|
17
|
-
sample,
|
|
18
|
-
config
|
|
19
|
-
);
|
|
20
|
+
config.rules['no-arrow-this/no-arrow-this'] = 'warn';
|
|
21
|
+
const results1 = linter.verify(sample, config);
|
|
20
22
|
console.log(results1);
|
|
21
23
|
|
|
22
24
|
console.log('\n\n2nd test!');
|
|
23
25
|
console.log('Only [global~window] suspicious:\n\n');
|
|
24
|
-
config.rules['no-arrow-this'] = ['warn', {
|
|
25
|
-
onlyGlobals
|
|
26
|
+
config.rules['no-arrow-this/no-arrow-this'] = ['warn', {
|
|
27
|
+
onlyGlobals: true
|
|
26
28
|
}];
|
|
27
|
-
const results2 = linter.verify(
|
|
28
|
-
|
|
29
|
-
config
|
|
30
|
-
);
|
|
31
|
-
console.log(results2);
|
|
29
|
+
const results2 = linter.verify(sample, config);
|
|
30
|
+
console.log(results2);
|
package/package.json
CHANGED
|
@@ -1,14 +1,16 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "eslint-plugin-no-arrow-this",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.3.0",
|
|
4
4
|
"author": "went.out@gmail.com",
|
|
5
5
|
"description": "ESLint rule plugin 4 warning \"this\" keyword inside of arrow functions",
|
|
6
6
|
"main": "dist/index.js",
|
|
7
7
|
"scripts": {
|
|
8
8
|
"build": "rm -rf ./dist && mkdir ./dist && cp -r ./src/* ./dist",
|
|
9
|
-
"
|
|
9
|
+
"prepack": "npm run build",
|
|
10
|
+
"lint": "eslint src/ test/ example/",
|
|
10
11
|
"test": "mocha && echo 'NOW let me show an Example!' && npm run example",
|
|
11
|
-
"example": "node example/index.js"
|
|
12
|
+
"example": "node example/index.js",
|
|
13
|
+
"postinstall": "npm run build"
|
|
12
14
|
},
|
|
13
15
|
"repository": {
|
|
14
16
|
"type": "git",
|
|
@@ -29,6 +31,7 @@
|
|
|
29
31
|
},
|
|
30
32
|
"files": [
|
|
31
33
|
"README.md",
|
|
34
|
+
"dist",
|
|
32
35
|
"src",
|
|
33
36
|
"example"
|
|
34
37
|
],
|
|
@@ -43,15 +46,8 @@
|
|
|
43
46
|
"engines": {
|
|
44
47
|
"node": ">=8.11.3"
|
|
45
48
|
},
|
|
46
|
-
"dependencies": {},
|
|
47
49
|
"devDependencies": {
|
|
48
|
-
"
|
|
49
|
-
"
|
|
50
|
-
"babel-eslint": "^8.2.6",
|
|
51
|
-
"babel-loader": "7.1.1",
|
|
52
|
-
"babel-register": "^6.26.0",
|
|
53
|
-
"babel-polyfill": "^6.26.0",
|
|
54
|
-
"eslint": "^5.3.0",
|
|
55
|
-
"mocha": "^5.2.0"
|
|
50
|
+
"eslint": "^9.39.5",
|
|
51
|
+
"mocha": "^11.2.0"
|
|
56
52
|
}
|
|
57
53
|
}
|
|
@@ -1,82 +1,102 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
|
-
module.exports =
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
3
|
+
module.exports = {
|
|
4
|
+
meta: {
|
|
5
|
+
type: 'suggestion',
|
|
6
|
+
docs: {
|
|
7
|
+
description: 'get rid of using "this" keyword in arrow functions',
|
|
8
|
+
category: 'code style~convention',
|
|
9
|
+
recommended: false
|
|
10
|
+
},
|
|
11
|
+
fixable: null,
|
|
12
|
+
schema: [
|
|
13
|
+
{
|
|
14
|
+
type: 'object',
|
|
15
|
+
properties: {
|
|
16
|
+
onlyGlobals: { type: 'boolean' }
|
|
17
|
+
},
|
|
18
|
+
additionalProperties: false
|
|
19
|
+
}
|
|
20
|
+
]
|
|
21
|
+
},
|
|
22
|
+
create (context) {
|
|
23
|
+
// lowercased name of AST Arrow Function
|
|
24
|
+
const AST_AF = 'ArrowFunctionExpression'.toLowerCase();
|
|
25
|
+
// lowercased name of AST Program Start
|
|
26
|
+
const AST_PRG = 'Program'.toLowerCase();
|
|
8
27
|
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
28
|
+
// Here are list of nodes for check breaking
|
|
29
|
+
// If one of them found, then everything is good
|
|
30
|
+
// And it is not necessary to check anymore else
|
|
31
|
+
const breakList = [
|
|
32
|
+
'FunctionDeclaration',
|
|
33
|
+
'FunctionExpression',
|
|
34
|
+
AST_PRG
|
|
35
|
+
].map(name => name.toLowerCase());
|
|
36
|
+
|
|
37
|
+
// This options allows to warn only situations
|
|
38
|
+
// When context of arrow function
|
|
39
|
+
// points Strict to Global or Window
|
|
40
|
+
const onlyGlobals = Boolean(
|
|
41
|
+
Array.isArray(context.options) &&
|
|
42
|
+
context.options[0] &&
|
|
43
|
+
context.options[0].onlyGlobals
|
|
44
|
+
);
|
|
45
|
+
|
|
46
|
+
// Warn message depends of onlyGlobals option
|
|
47
|
+
const name = onlyGlobals ? 'global "this"' : '"this"';
|
|
48
|
+
|
|
49
|
+
var type, parent;
|
|
50
|
+
|
|
51
|
+
// checking function depends of onlyGlobals
|
|
52
|
+
const prepareCheckFn = (node) => {
|
|
53
|
+
parent = node;
|
|
54
|
+
|
|
55
|
+
const switchParent = () => {
|
|
56
|
+
// switching AST node
|
|
57
|
+
// upper from current
|
|
58
|
+
parent = parent.parent;
|
|
59
|
+
type = parent.type.toLowerCase();
|
|
60
|
+
};
|
|
61
|
+
|
|
62
|
+
if (!onlyGlobals) {
|
|
63
|
+
return () => {
|
|
64
|
+
switchParent();
|
|
65
|
+
if (breakList.includes(type)) {
|
|
66
|
+
return null;
|
|
67
|
+
}
|
|
68
|
+
if (type === AST_AF) {
|
|
69
|
+
return true;
|
|
70
|
+
}
|
|
71
|
+
return false;
|
|
72
|
+
};
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
let found = false;
|
|
44
76
|
return () => {
|
|
45
77
|
switchParent();
|
|
46
78
|
if (breakList.includes(type)) {
|
|
47
|
-
return null;
|
|
79
|
+
return (found && type === AST_PRG) ? true : null;
|
|
48
80
|
}
|
|
49
81
|
if (type === AST_AF) {
|
|
50
|
-
|
|
82
|
+
found = true;
|
|
51
83
|
}
|
|
52
84
|
return false;
|
|
53
85
|
};
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
let found = false;
|
|
57
|
-
return () => {
|
|
58
|
-
switchParent();
|
|
59
|
-
if (breakList.includes(type)) {
|
|
60
|
-
return (found && type === AST_PRG) ? true : null;
|
|
61
|
-
}
|
|
62
|
-
if (type === AST_AF) {
|
|
63
|
-
found = true;
|
|
64
|
-
}
|
|
65
|
-
return false;
|
|
86
|
+
|
|
66
87
|
};
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
context.report(node, `do not use ${name} in an arrow function`);
|
|
88
|
+
|
|
89
|
+
return {
|
|
90
|
+
ThisExpression (node) {
|
|
91
|
+
const check = prepareCheckFn(node);
|
|
92
|
+
var result = false;
|
|
93
|
+
while (result === false) {
|
|
94
|
+
result = check();
|
|
95
|
+
}
|
|
96
|
+
if (result) {
|
|
97
|
+
context.report({ node, message: `do not use ${name} in an arrow function` });
|
|
98
|
+
}
|
|
79
99
|
}
|
|
80
|
-
}
|
|
81
|
-
}
|
|
82
|
-
};
|
|
100
|
+
};
|
|
101
|
+
}
|
|
102
|
+
};
|