-
Notifications
You must be signed in to change notification settings - Fork 238
/
eslint.config.js
205 lines (201 loc) · 7.37 KB
/
eslint.config.js
1
2
3
4
5
6
7
8
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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
const js = require('@eslint/js');
const globals = require('globals');
module.exports = [
// Base on eslint recommended
js.configs.recommended,
// Common for all files
{
languageOptions: {
ecmaVersion: 2022,
sourceType: 'commonjs'
},
rules: {
// Unused variables commonly indicate logic errors
'no-unused-vars': [
'error',
{
// Unused arguments are useful, eg. it can be nice for blocks to accept `args` even if they don't use it
args: 'none',
// Allow silently eating try { } catch (e) { }
caughtErrors: 'none',
// Variables starting with _ are intentionally unused
argsIgnorePattern: '^_',
varsIgnorePattern: '^_',
}
],
// Allow while (true) { }
'no-constant-condition': [
'error',
{
checkLoops: false
}
],
// Allow empty catch {} blocks
'no-empty': [
'error',
{
allowEmptyCatch: true
}
],
// Returning a value from a constructor() implies a mistake
'no-constructor-return': 'error',
// new Promise(async () => {}) implies a mistake
'no-async-promise-executor': 'warn',
// x === x implies a mistake
'no-self-compare': 'error',
// Using ${...} in a non-template-string implies a mistake
'no-template-curly-in-string': 'error',
// Loops that only iterate once imply a mistake
'no-unreachable-loop': 'error',
// Detect some untrusted code execution
'no-eval': 'error',
'no-implied-eval': 'error',
'no-new-func': 'error',
'no-script-url': 'error',
// Combinations of || and && are unreadable and may not do what you expect
'no-mixed-operators': [
'error',
{
groups: [
['&&', '||']
]
}
],
// Disallow async functions that don't need to be. This is important as a Promise and non-Promise return value
// significantly impacts the behavior of projects.
'require-await': 'error'
}
},
// For development server
{
files: [
'development/**'
],
languageOptions: {
globals: {
...globals.commonjs,
...globals.node
}
}
},
// For extensions
{
files: [
'extensions/**'
],
languageOptions: {
globals: {
...globals.browser,
Blockly: 'readonly',
Scratch: 'readonly',
ScratchBlocks: 'readonly',
ScratchExtensions: 'readonly',
scaffolding: 'readonly'
}
},
rules: {
// Require each extension to use strict mode
'strict': ['error', 'function'],
// Disallow APIs that are replaced by Scratch.* APIs
// This is not comprehensive, but it should be enough to prevent the most common ways for these to be written.
'no-restricted-globals': [
'error',
{
name: 'vm',
message: 'Use Scratch.vm instead of the global vm object. You also can use const vm = Scratch.vm;'
}
],
'no-restricted-syntax': [
'error',
{
selector: 'CallExpression[callee.name=fetch]',
message: 'Use Scratch.fetch() instead of fetch()'
},
{
selector: 'CallExpression[callee.object.name=window][callee.property.name=fetch]',
message: 'Use Scratch.fetch() instead of window.fetch()'
},
{
selector: 'CallExpression[callee.name=open]',
message: 'Use Scratch.openWindow() instead of open()'
},
{
selector: 'CallExpression[callee.object.name=window][callee.property.name=open]',
message: 'Use Scratch.openWindow() instead of window.open()'
},
{
selector: 'AssignmentExpression[left.object.name=location][left.property.name=href]',
message: 'Use Scratch.redirect() instead of location.href = ...'
},
{
selector: 'AssignmentExpression[left.object.object.name=window][left.object.property.name=location][left.property.name=href]',
message: 'Use Scratch.redirect() instead of window.location.href = ...'
},
{
selector: 'AssignmentExpression[left.name=location]',
message: 'Use Scratch.redirect() instead of location = ...'
},
{
selector: 'AssignmentExpression[left.object.name=window][left.property.name=location]',
message: 'Use Scratch.redirect() instead of window.location = ...'
},
{
selector: 'CallExpression[callee.object.name=location][callee.property.name=assign]',
message: 'Use Scratch.redirect() instead of location.assign()'
},
{
selector: 'CallExpression[callee.object.object.name=window][callee.object.property.name=location][callee.property.name=assign]',
message: 'Use Scratch.redirect() instead of window.location.assign()'
},
{
selector: 'CallExpression[callee.object.name=location][callee.property.name=replace]',
message: 'Use Scratch.redirect() instead of location.replace()'
},
{
selector: 'CallExpression[callee.object.object.name=window][callee.object.property.name=location][callee.property.name=replace]',
message: 'Use Scratch.redirect() instead of window.location.replace()'
},
{
selector: 'NewExpression[callee.name=XMLHttpRequest]',
message: 'Use Scratch.fetch() instead of XMLHttpRequest'
},
{
selector: 'NewExpression[callee.name=WebSocket]',
message: 'Ensure that `await Scratch.canFetch(url)` is checked first, then add // eslint-disable-next-line no-restricted-syntax'
},
{
selector: 'NewExpression[callee.name=Image]',
message: 'Ensure that `await Scratch.canFetch(url)` is checked first, then add // eslint-disable-next-line no-restricted-syntax'
},
{
selector: 'NewExpression[callee.name=Audio]',
message: 'Ensure that `await Scratch.canFetch(url)` is checked first, then add // eslint-disable-next-line no-restricted-syntax'
},
{
selector: 'Program > :not(ExpressionStatement[expression.type=CallExpression][expression.callee.type=/FunctionExpression/])',
message: 'All extension code must be within (function (Scratch) { ... })(Scratch);'
},
{
selector: 'CallExpression[callee.object.object.name=Scratch][callee.object.property.name=translate][callee.property.name=setup]',
message: 'Do not call Scratch.translate.setup() yourself. Just use Scratch.translate() and let the build script handle it.'
},
{
selector: 'MethodDefinition[key.name=getInfo] Property[key.name=id][value.callee.property.name=translate]',
message: 'Do not translate extension ID'
},
{
selector: 'MethodDefinition[key.name=docsURI] Property[key.name=id][value.callee.property.name=translate]',
message: 'Do not translate docsURI'
},
{
selector: 'MethodDefinition[key.name=getInfo] Property[key.name=opcode][value.callee.property.name=translate]',
message: 'Do not translate block opcode'
},
{
selector: 'MemberExpression[object.name=window][property.name=vm]',
message: 'Use Scratch.vm instead of window.vm'
}
]
}
}
];