@cookshack/eslint-config 3.0.0 → 5.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.
@@ -121,7 +121,8 @@ function getConditionalContext
121
121
  return ''
122
122
  }
123
123
 
124
- function nodeContains(node, target) {
124
+ function nodeContains
125
+ (node, target) {
125
126
  if (node == target)
126
127
  return true
127
128
  if (node && typeof node == 'object')
@@ -131,7 +132,8 @@ function nodeContains(node, target) {
131
132
  return false
132
133
  }
133
134
 
134
- function nodeHas(value, target) {
135
+ function nodeHas
136
+ (value, target) {
135
137
  if (value == target)
136
138
  return true
137
139
  if (Array.isArray(value))
@@ -234,7 +236,8 @@ function isProperAncestor
234
236
  return 0
235
237
  }
236
238
 
237
- function scopeStart(scope) {
239
+ function scopeStart
240
+ (scope) {
238
241
  if (scope.block == null)
239
242
  return Infinity
240
243
  if (scope.type == 'function' && scope.block.id)
@@ -264,12 +267,11 @@ function buildScopeTree
264
267
  (scope, prefix, scopeToNode, astToTree) {
265
268
  let node, siblingNum
266
269
 
267
- node = {
268
- scope,
269
- prefix,
270
- items: [],
271
- children: []
272
- }
270
+ node = { scope,
271
+ prefix,
272
+ items: [],
273
+ children: [] }
274
+
273
275
  scopeToNode.set(scope, node)
274
276
  if (scope.block && astToTree)
275
277
  astToTree.set(scope.block, node)
@@ -402,11 +404,9 @@ function checkScopeNode
402
404
  trace(indent, '5', variable.name, 'is too broad')
403
405
 
404
406
  reported.add(variable)
405
- context.report({
406
- node: defNode,
407
- messageId: 'tooBroad',
408
- data: { name: variable.name }
409
- })
407
+ context.report({ node: defNode,
408
+ messageId: 'tooBroad',
409
+ data: { name: variable.name } })
410
410
  }
411
411
  }
412
412
  }
@@ -452,17 +452,16 @@ function createNarrowestScope
452
452
  clearPrintBuffer()
453
453
  scopeManager = context.sourceCode.scopeManager
454
454
  if (scopeManager)
455
- return {
456
- 'Program:exit'() {
457
- let tree, scopeToNode
458
-
459
- scopeToNode = new Map
460
- nextVarId = 0
461
- tree = buildScopeTree(scopeManager.scopes[0], '1', scopeToNode)
462
- checkScopeNode(context, tree, null, scopeToNode)
463
- printTree(tree, 0)
464
- }
465
- }
455
+ return { 'Program:exit'
456
+ () {
457
+ let tree, scopeToNode
458
+
459
+ scopeToNode = new Map
460
+ nextVarId = 0
461
+ tree = buildScopeTree(scopeManager.scopes[0], '1', scopeToNode)
462
+ checkScopeNode(context, tree, null, scopeToNode)
463
+ printTree(tree, 0)
464
+ } }
466
465
  }
467
466
 
468
467
  export
@@ -1,20 +1,20 @@
1
1
  function createPositiveVibes
2
2
  (context) {
3
- return {
4
- UnaryExpression(node) {
5
- if (node.operator == '!')
6
- context.report({ node,
7
- messageId: 'positiveVibes' })
8
- },
9
- BinaryExpression(node) {
10
- if (node.operator == '!=')
11
- context.report({ node,
12
- messageId: 'equality' })
13
- else if (node.operator == '!==')
14
- context.report({ node,
15
- messageId: 'strictEquality' })
16
- }
17
- }
3
+ return { UnaryExpression
4
+ (node) {
5
+ if (node.operator == '!')
6
+ context.report({ node,
7
+ messageId: 'positiveVibes' })
8
+ },
9
+ BinaryExpression
10
+ (node) {
11
+ if (node.operator == '!=')
12
+ context.report({ node,
13
+ messageId: 'equality' })
14
+ else if (node.operator == '!==')
15
+ context.report({ node,
16
+ messageId: 'strictEquality' })
17
+ } }
18
18
  }
19
19
 
20
20
  export
@@ -3,9 +3,11 @@ default { meta: { type: 'problem',
3
3
  docs: { description: 'Enforce use of == instead of ===.' },
4
4
  messages: { risky: 'Use ==.' },
5
5
  schema: [] },
6
- create(context) {
7
- return { BinaryExpression(node) {
8
- if (node.operator == '===')
9
- context.report({ node, messageId: 'risky' })
10
- } }
6
+ create
7
+ (context) {
8
+ return { BinaryExpression
9
+ (node) {
10
+ if (node.operator == '===')
11
+ context.report({ node, messageId: 'risky' })
12
+ } }
11
13
  } }
@@ -1,5 +1,4 @@
1
- function
2
- VariableDeclaration
1
+ function VariableDeclaration
3
2
  (context, node) {
4
3
  let parent
5
4
 
@@ -24,6 +23,7 @@ VariableDeclaration
24
23
  }
25
24
  }
26
25
  }
26
+
27
27
  function create
28
28
  (context) {
29
29
  return { VariableDeclaration: node => VariableDeclaration(context, node) }
@@ -0,0 +1,114 @@
1
+ import { RuleTester } from 'eslint'
2
+ import { plugins } from '../../index.js'
3
+
4
+ let ruleTester, validCases, invalidCases
5
+
6
+ ruleTester = new RuleTester()
7
+ validCases = []
8
+ invalidCases = []
9
+
10
+ function pass
11
+ (code) {
12
+ validCases.push({ code })
13
+ }
14
+
15
+ function fail
16
+ (count, code) {
17
+ let errors
18
+
19
+ errors = []
20
+ while (count > 0) {
21
+ errors.push({ messageId: 'fnArgsNl' })
22
+ count--
23
+ }
24
+ invalidCases.push({ code, errors })
25
+ }
26
+
27
+ pass(`function f
28
+ (arg) {
29
+ }`)
30
+
31
+ pass(`function f
32
+ () {}`)
33
+
34
+ pass(`function f
35
+ (arg1, arg2) {}`)
36
+
37
+ pass(`function f
38
+ (
39
+ arg
40
+ ) {}`)
41
+
42
+ pass(`export default
43
+ function
44
+ () {
45
+ return 1
46
+ }
47
+ `)
48
+
49
+ pass(`
50
+ function f
51
+ () {
52
+ return { f1
53
+ () {
54
+ return 1
55
+ } }
56
+ }
57
+ `)
58
+
59
+ pass(`
60
+ function f
61
+ () {
62
+ return {
63
+ f1
64
+ () {
65
+ return 1
66
+ }
67
+ }
68
+ }
69
+ `)
70
+
71
+ pass(`
72
+ class Marker {
73
+ constructor
74
+ (name, num) {
75
+ }
76
+ toDOM
77
+ () {
78
+ }
79
+ }
80
+ `)
81
+
82
+ pass(`
83
+ area = { get name
84
+ () {
85
+ return name
86
+ },
87
+ get value
88
+ () {
89
+ return value
90
+ } }
91
+ `)
92
+
93
+ fail(1, 'function f (arg) {}')
94
+
95
+ fail(1, 'function f() {}')
96
+
97
+ fail(1, `function f (arg,
98
+ arg2) {
99
+ }`)
100
+
101
+ fail(1, `function f
102
+
103
+ (arg) {}`)
104
+
105
+ fail(1, `function f
106
+
107
+
108
+ (arg) {}`)
109
+
110
+ globalThis.describe('fn-args-nl',
111
+ () => ruleTester.run('fn-args-nl',
112
+ plugins.cookshack.rules['fn-args-nl'],
113
+ { valid: validCases,
114
+ invalid: invalidCases }))
@@ -0,0 +1,198 @@
1
+ import { RuleTester } from 'eslint'
2
+ import { plugins } from '../../index.js'
3
+
4
+ let ruleTester, validCases, invalidCases
5
+
6
+ ruleTester = new RuleTester()
7
+ validCases = []
8
+ invalidCases = []
9
+
10
+ function pass
11
+ (code) {
12
+ validCases.push({ code })
13
+ }
14
+
15
+ function fail
16
+ (count, code) {
17
+ let errors
18
+
19
+ if (code == undefined) {
20
+ code = count
21
+ count = 1
22
+ }
23
+ errors = []
24
+ while (count > 0) {
25
+ errors.push({ messageId: 'indentStruct' })
26
+ count--
27
+ }
28
+ invalidCases.push({ code, errors })
29
+ }
30
+
31
+ pass('let x = { a: 1, b: 2 }')
32
+
33
+ pass(`
34
+ let x = {
35
+ a: 1,
36
+ b: 2
37
+ }`)
38
+
39
+ pass(`
40
+ let x = { a: 1,
41
+ b: 2 }`)
42
+
43
+ pass(`
44
+ let x = {
45
+ a
46
+ () {}
47
+ }`)
48
+
49
+ pass(`
50
+ let x = {
51
+ a
52
+ () {},
53
+ b
54
+ () {}
55
+ }`)
56
+
57
+ pass(`
58
+ function f
59
+ () {
60
+ return { a: 1,
61
+ b: 2 }
62
+ }`)
63
+
64
+ pass(`
65
+ function f1
66
+ () {
67
+ return { f1
68
+ () {
69
+ return 1
70
+ },
71
+ b: 2 }
72
+ }`)
73
+
74
+ pass(`
75
+ function f2
76
+ () {
77
+ return { a: 1,
78
+ f2
79
+ () {
80
+ return 2
81
+ } }
82
+ }`)
83
+
84
+ pass(`
85
+ let x = { a: 1,
86
+ b: 2 }
87
+ `)
88
+
89
+ pass(`
90
+ let x = { a: 1,
91
+ b: 2 } /*stuff {, } */
92
+ `)
93
+
94
+ pass(`
95
+ let x = { a: 1,
96
+ b: 2 } /*stuff*/
97
+ `)
98
+
99
+ pass(`
100
+ let x = { a: { a: 1,
101
+ b: 2 } }
102
+ `)
103
+
104
+ pass(`
105
+ let x = { a: { a: 1,
106
+ b: 2 } } ; function f(){ return 1}
107
+ `)
108
+
109
+ pass(`function create
110
+ (context) {
111
+ return { VariableDeclaration
112
+ (node) {
113
+ if (node.kind == 'const' || node.kind == 'var')
114
+ context.report({ node, messageId: 'useLet' })
115
+ } }
116
+ }`)
117
+
118
+ pass(`function two
119
+ () {
120
+ return { 'Program:exit'
121
+ () {
122
+ // do
123
+ } }
124
+ }`)
125
+
126
+ pass(`
127
+ let x = { get a
128
+ () {
129
+ return 1
130
+ },
131
+ get b
132
+ () {
133
+ return 2
134
+ } }`)
135
+
136
+ pass(`
137
+ let x = { async f
138
+ () {
139
+ return 1
140
+ },
141
+ async g
142
+ () {
143
+ return 2
144
+ } }`)
145
+
146
+ fail(2, `
147
+ let x = {
148
+ a: 1,
149
+ b: 2
150
+ }`)
151
+
152
+ fail(1, `
153
+ let x = {
154
+ a: 1,
155
+ b: 2
156
+ }`)
157
+
158
+ fail(1, `
159
+ let x = { a: 1,
160
+ b: 2 }`)
161
+
162
+ fail(1, `
163
+ let x = { a: 1,
164
+ b: 2 }`)
165
+
166
+ fail(1, `
167
+ let x = { a: 1,
168
+ b: 2 }`)
169
+
170
+ fail(2, `
171
+ let x = { a: 1,
172
+ b: 2,
173
+ c: 3 }`)
174
+
175
+ fail(1, `
176
+ let aHasArgsOnNextLine = { a
177
+ () {} }`)
178
+
179
+ fail(1, `
180
+ let x = { a: 1,
181
+ b: 2}
182
+ `)
183
+
184
+ fail(1, `
185
+ let x = { a: 1,
186
+ b: 2 }
187
+ `)
188
+
189
+ fail(1, `
190
+ let x = { a: { a: 1,
191
+ b: 2 } }
192
+ `)
193
+
194
+ globalThis.describe('indent-struct',
195
+ () => ruleTester.run('indent-struct',
196
+ plugins.cookshack.rules['indent-struct'],
197
+ { valid: validCases,
198
+ invalid: invalidCases }))
@@ -23,7 +23,8 @@ function tree
23
23
  return ''
24
24
  }
25
25
 
26
- function _pass(tc) {
26
+ function _pass
27
+ (tc) {
27
28
  let messages
28
29
 
29
30
  messages = linter.verify(tc.code, config)
@@ -31,7 +32,8 @@ function _pass(tc) {
31
32
  throw new Error('unexpected errors: ' + JSON.stringify(messages, null, 2) + '\n' + tree())
32
33
  }
33
34
 
34
- function _fail(tc) {
35
+ function _fail
36
+ (tc) {
35
37
  let messages
36
38
 
37
39
  messages = linter.verify(tc.code, config)
@@ -40,7 +42,8 @@ function _fail(tc) {
40
42
  throw new Error('expected ' + tc.errors.length + ' errors, got ' + messages.length + '\n' + JSON.stringify(messages, null, 2) + '\n' + tree())
41
43
  }
42
44
 
43
- function fail(message, code) {
45
+ function fail
46
+ (message, code) {
44
47
  let errors
45
48
 
46
49
  if (Array.isArray(message))
@@ -51,7 +54,8 @@ function fail(message, code) {
51
54
  invalidCases.push({ code, errors })
52
55
  }
53
56
 
54
- function pass(code) {
57
+ function pass
58
+ (code) {
55
59
  validCases.push({ code })
56
60
  }
57
61
 
@@ -13,16 +13,19 @@ config = [ { languageOptions: { ecmaVersion: 2025,
13
13
  plugins,
14
14
  rules: { 'cookshack/narrowest-scope': 'error' } } ]
15
15
 
16
- function pass(code, expected) {
16
+ function pass
17
+ (code, expected) {
17
18
  validCases.push({ code, expected })
18
19
  }
19
20
 
20
- function patch(expected, output) {
21
+ function patch
22
+ (expected, output) {
21
23
  return diffLines(expected.trim(), output.trim()).flatMap(p => p.value.split('\n').filter(l => l).map(l => ({ ...p, line: l })))
22
24
  .map(p => (p.removed ? '- ' : p.added ? '+ ' : ' ') + p.line).join('\n')
23
25
  }
24
26
 
25
- function _pass(tc) {
27
+ function _pass
28
+ (tc) {
26
29
  let messages, output
27
30
 
28
31
  messages = linter.verify(tc.code, config)
@@ -34,7 +37,8 @@ function _pass(tc) {
34
37
  throw new Error('output mismatch:\n' + patch(tc.expected, output))
35
38
  }
36
39
 
37
- function _fail(tc) {
40
+ function _fail
41
+ (tc) {
38
42
  let messages, output
39
43
 
40
44
  messages = linter.verify(tc.code, config)
@@ -47,7 +51,8 @@ function _fail(tc) {
47
51
  throw new Error('expected ' + tc.errors.length + ' errors, got ' + messages.length + '\n' + JSON.stringify(messages, null, 2))
48
52
  }
49
53
 
50
- function fail(count, code, expected) {
54
+ function fail
55
+ (count, code, expected) {
51
56
  let errors
52
57
 
53
58
  errors = []
@@ -0,0 +1,42 @@
1
+ import { RuleTester } from 'eslint'
2
+ import { Linter } from 'eslint'
3
+
4
+ let ruleTester, validCases, invalidCases, rule
5
+
6
+ rule = new Linter({ configType: 'eslintrc' }).getRules().get('no-shadow')
7
+ ruleTester = new RuleTester()
8
+ validCases = []
9
+ invalidCases = []
10
+
11
+ function pass
12
+ (code) {
13
+ validCases.push({ code, options: [ { builtinGlobals: true } ] })
14
+ }
15
+
16
+ function fail
17
+ (messageId, code) {
18
+ invalidCases.push({ code,
19
+ options: [ { builtinGlobals: true } ],
20
+ errors: [ { messageId } ] })
21
+ }
22
+
23
+ pass('var a = 3; function b() { var c = 10; }')
24
+ pass('let x = 1; { let y = 2; }')
25
+ pass('function a() { let x = 1 } function b() { let x = 2 }')
26
+ pass('var a = 1; if (x) { var a = 2; }')
27
+
28
+ fail('noShadow', 'var a = 3; function b() { var a = 10; }')
29
+ fail('noShadow', 'let x = 1; { let x = 2; }')
30
+ fail('noShadow', 'let a = 1; function foo(a) { return a }')
31
+ fail('noShadow', 'function f(x) { function g() { let x = 2; } }')
32
+ fail('noShadow', 'const a = 1; function foo() { const a = 2; }')
33
+ fail('noShadow', 'let a = 1; if (x) { let a = 2; }')
34
+
35
+ fail('noShadowGlobal', 'function foo() { let Object = 1; }')
36
+ fail('noShadowGlobal', 'function foo() { let Array = []; }')
37
+
38
+ globalThis.describe('no-shadow',
39
+ () => ruleTester.run('no-shadow',
40
+ rule,
41
+ { valid: validCases,
42
+ invalid: invalidCases }))