eslint-plugin-no-arrow-this 1.2.0 → 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.
@@ -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').Linter;
4
- const linter = new Linter();
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 = require('../.eslintrc.js');
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 : true
26
+ config.rules['no-arrow-this/no-arrow-this'] = ['warn', {
27
+ onlyGlobals: true
26
28
  }];
27
- const results2 = linter.verify(
28
- sample,
29
- config
30
- );
31
- console.log(results2);
29
+ const results2 = linter.verify(sample, config);
30
+ console.log(results2);
package/package.json CHANGED
@@ -1,12 +1,13 @@
1
1
  {
2
2
  "name": "eslint-plugin-no-arrow-this",
3
- "version": "1.2.0",
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
- "lint": "eslint src/ test/",
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
12
  "example": "node example/index.js",
12
13
  "postinstall": "npm run build"
@@ -30,6 +31,7 @@
30
31
  },
31
32
  "files": [
32
33
  "README.md",
34
+ "dist",
33
35
  "src",
34
36
  "example"
35
37
  ],
@@ -44,15 +46,8 @@
44
46
  "engines": {
45
47
  "node": ">=8.11.3"
46
48
  },
47
- "dependencies": {},
48
49
  "devDependencies": {
49
- "babel-cli": "^6.26.0",
50
- "babel-core": "^6.26.3",
51
- "babel-eslint": "^8.2.6",
52
- "babel-loader": "7.1.1",
53
- "babel-register": "^6.26.0",
54
- "babel-polyfill": "^6.26.0",
55
- "eslint": "^5.3.0",
56
- "mocha": "^5.2.0"
50
+ "eslint": "^9.39.5",
51
+ "mocha": "^11.2.0"
57
52
  }
58
53
  }
@@ -1,82 +1,102 @@
1
1
  'use strict';
2
2
 
3
- module.exports = (context) => {
4
- // lowercased name of AST Arrow Function
5
- const AST_AF = 'ArrowFunctionExpression'.toLowerCase();
6
- // lowercased name of AST Program Start
7
- const AST_PRG = 'Program'.toLowerCase();
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
- // Here are list of nodes for check breaking
10
- // If one of them found, then everything is good
11
- // And it is not necessary to check anymore else
12
- const breakList = [
13
- 'FunctionDeclaration',
14
- 'FunctionExpression',
15
- AST_PRG
16
- ].map(name => name.toLowerCase());
17
-
18
- // This options allows to warn only situations
19
- // When context of arrow function
20
- // points Strict to Global or Window
21
- const onlyGlobals = (
22
- Array.isArray(context.options) &&
23
- context.options[0] &&
24
- context.options[0].onlyGlobals
25
- ) ? true : false;
26
-
27
- // Warn message depends of onlyGlobals option
28
- const name = onlyGlobals ? 'global "this"' : '"this"';
29
-
30
- var type, parent;
31
-
32
- // checking function depends of onlyGlobals
33
- const prepareCheckFn = (node) => {
34
- parent = node;
35
-
36
- const switchParent = () => {
37
- // switching AST node
38
- // upper from current
39
- parent = parent.parent;
40
- type = parent.type.toLowerCase();
41
- };
42
-
43
- if (!onlyGlobals) {
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
- return true;
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
- return {
71
- ThisExpression(node) {
72
- const check = prepareCheckFn(node);
73
- var result = false;
74
- while (result === false) {
75
- result = check();
76
- }
77
- if (result) {
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
+ };