eslint-plugin-julia 0.0.0-stage → 0.3.1
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 +159 -0
- package/LICENSE +21 -0
- package/README.md +213 -2
- package/data/base-core-exports.json +5 -0
- package/data/public-only.json +35 -0
- package/data/stdlib-exports.json +180 -0
- package/package.json +76 -4
- package/src/index.js +226 -0
- package/src/language.js +46 -0
- package/src/macro-signatures.js +277 -0
- package/src/module-resolution.js +1187 -0
- package/src/orphans.js +631 -0
- package/src/parser/flags.js +25 -0
- package/src/parser/green-tree.js +41 -0
- package/src/parser/positions.js +29 -0
- package/src/parser/syntax-tree.js +91 -0
- package/src/parser/wasm-host.js +92 -0
- package/src/resolver/index.js +232 -0
- package/src/resolver/pkg-child.mjs +165 -0
- package/src/resolver/toml.js +63 -0
- package/src/resolver/warm.mjs +26 -0
- package/src/rules/common.js +52 -0
- package/src/rules/correctness-common.js +90 -0
- package/src/rules/growth-common.js +93 -0
- package/src/rules/idiom-common.js +116 -0
- package/src/rules/no-collapsible-if.js +38 -0
- package/src/rules/no-collect-before-range-iteration.js +24 -0
- package/src/rules/no-compare-negative-zero.js +50 -0
- package/src/rules/no-concat-growth-in-loop.js +18 -0
- package/src/rules/no-cond-assign.js +126 -0
- package/src/rules/no-const-reassign.js +61 -0
- package/src/rules/no-constant-condition.js +59 -0
- package/src/rules/no-debugger.js +96 -0
- package/src/rules/no-discarded-copy-result.js +50 -0
- package/src/rules/no-dupe-args.js +25 -0
- package/src/rules/no-dupe-elseif.js +57 -0
- package/src/rules/no-duplicate-dict-keys.js +95 -0
- package/src/rules/no-duplicate-keyword-arguments.js +40 -0
- package/src/rules/no-duplicate-logical-operands.js +85 -0
- package/src/rules/no-duplicate-method-signatures.js +90 -0
- package/src/rules/no-duplicate-struct-fields.js +38 -0
- package/src/rules/no-empty-block.js +49 -0
- package/src/rules/no-empty-catch.js +32 -0
- package/src/rules/no-empty-literal-range.js +26 -0
- package/src/rules/no-env-mutation.js +62 -0
- package/src/rules/no-eval-local-symbol.js +85 -0
- package/src/rules/no-fill-with-mutable.js +43 -0
- package/src/rules/no-full-slice-in-consumer.js +41 -0
- package/src/rules/no-identical-branches.js +44 -0
- package/src/rules/no-import-assign.js +64 -0
- package/src/rules/no-impossible-length-comparison.js +49 -0
- package/src/rules/no-invalid-foreign-call-signature.js +58 -0
- package/src/rules/no-isdefined-local-call.js +23 -0
- package/src/rules/no-loss-of-float-integer-precision.js +41 -0
- package/src/rules/no-nothing-return-conversion.js +127 -0
- package/src/rules/no-obvious-type-piracy.js +112 -0
- package/src/rules/no-orphan-file.js +39 -0
- package/src/rules/no-overwritten-literal-index.js +80 -0
- package/src/rules/no-reassigned-capture.js +107 -0
- package/src/rules/no-redundant-type-assertion.js +36 -0
- package/src/rules/no-redundant-union-members.js +42 -0
- package/src/rules/no-restricted-calls.js +74 -0
- package/src/rules/no-restricted-imports.js +105 -0
- package/src/rules/no-restricted-macros.js +61 -0
- package/src/rules/no-self-assign.js +65 -0
- package/src/rules/no-self-identity-comparison.js +37 -0
- package/src/rules/no-shadowed-loop-var.js +83 -0
- package/src/rules/no-show-macro.js +90 -0
- package/src/rules/no-single-iteration-loop.js +50 -0
- package/src/rules/no-string-growth-in-loop.js +18 -0
- package/src/rules/no-throw-literal.js +33 -0
- package/src/rules/no-unbound-type-parameters.js +91 -0
- package/src/rules/no-undef.js +284 -0
- package/src/rules/no-undefined-exports.js +60 -0
- package/src/rules/no-uninitialized-ref-read.js +38 -0
- package/src/rules/no-unreachable.js +82 -0
- package/src/rules/no-unsafe-finally.js +35 -0
- package/src/rules/no-unsafe-negation.js +77 -0
- package/src/rules/no-unused-expressions.js +137 -0
- package/src/rules/no-unused-type-parameters.js +55 -0
- package/src/rules/no-unused-vars.js +123 -0
- package/src/rules/no-use-before-define.js +139 -0
- package/src/rules/no-useless-assignment.js +75 -0
- package/src/rules/no-useless-catch.js +28 -0
- package/src/rules/parse-errors.js +19 -0
- package/src/rules/performance-common.js +119 -0
- package/src/rules/prefer-any.js +75 -0
- package/src/rules/prefer-const-global.js +103 -0
- package/src/rules/prefer-count-over-findall-length.js +46 -0
- package/src/rules/prefer-eachindex.js +120 -0
- package/src/rules/prefer-extreme-over-sort.js +73 -0
- package/src/rules/prefer-generator-sum.js +34 -0
- package/src/rules/prefer-identity-nothing.js +43 -0
- package/src/rules/prefer-in.js +67 -0
- package/src/rules/prefer-isa.js +70 -0
- package/src/rules/prefer-isempty.js +57 -0
- package/src/rules/prefer-ismissing.js +42 -0
- package/src/rules/prefer-parenthesized-interpolation.js +160 -0
- package/src/rules/prefer-size-dimension.js +19 -0
- package/src/rules/prefer-sized-push-buffer.js +81 -0
- package/src/rules/prefer-typed-empty-array.js +90 -0
- package/src/rules/require-typed-global.js +60 -0
- package/src/rules/restricted-common.js +211 -0
- package/src/rules/semantic-common.js +105 -0
- package/src/rules/use-isnan.js +28 -0
- package/src/scope.js +1338 -0
- package/src/source-code.js +100 -0
- package/types/ast.d.ts +49 -0
- package/types/index.d.ts +65 -0
- package/types/scope.d.ts +71 -0
- package/wasm/LICENSES/Julia.md +26 -0
- package/wasm/LICENSES/JuliaSyntax-MIT.md +46 -0
- package/wasm/LICENSES/WasmCodegen-MIT.txt +21 -0
- package/wasm/manifest.json +44 -0
- package/wasm/parser.wasm +0 -0
|
@@ -0,0 +1,137 @@
|
|
|
1
|
+
import { defineRule, isBareName, isOpaque } from './common.js';
|
|
2
|
+
import { isInfixCall } from '../parser/flags.js';
|
|
3
|
+
const sequences = new Set(['block', 'toplevel', 'JuliaFile']);
|
|
4
|
+
const numbers = new Set(['Integer', 'BinInt', 'OctInt', 'HexInt', 'Float', 'Float32', 'Bool']);
|
|
5
|
+
// A discarded comparison (`x == 1` as a statement) is almost always a typo for `=`, or a
|
|
6
|
+
// forgotten `@assert`/`&&`. Overloaded comparison operators with side effects are not a
|
|
7
|
+
// pattern worth protecting.
|
|
8
|
+
const comparisonOps = new Set([
|
|
9
|
+
'==',
|
|
10
|
+
'!=',
|
|
11
|
+
'===',
|
|
12
|
+
'!==',
|
|
13
|
+
'<',
|
|
14
|
+
'>',
|
|
15
|
+
'<=',
|
|
16
|
+
'>=',
|
|
17
|
+
'≈',
|
|
18
|
+
'≉',
|
|
19
|
+
'≠',
|
|
20
|
+
'≤',
|
|
21
|
+
'≥',
|
|
22
|
+
'∈',
|
|
23
|
+
'∉',
|
|
24
|
+
'in',
|
|
25
|
+
'isa',
|
|
26
|
+
]);
|
|
27
|
+
function isComparison(n) {
|
|
28
|
+
while (n.kind === 'parens' && n.children.length === 1) n = n.children[0];
|
|
29
|
+
if (n.kind === 'comparison') return true;
|
|
30
|
+
return (
|
|
31
|
+
['call', 'dotcall'].includes(n.kind) &&
|
|
32
|
+
n.interior &&
|
|
33
|
+
n.children.length === 3 &&
|
|
34
|
+
isInfixCall(n) &&
|
|
35
|
+
comparisonOps.has(n.children[1].value)
|
|
36
|
+
);
|
|
37
|
+
}
|
|
38
|
+
function candidate(n) {
|
|
39
|
+
if (isComparison(n)) return true;
|
|
40
|
+
if (isBareName(n)) return n.value !== 'nothing';
|
|
41
|
+
if (!n.interior && numbers.has(n.kind)) return true;
|
|
42
|
+
if (n.kind === 'parens' && n.children.length === 1) return candidate(n.children[0]);
|
|
43
|
+
if (n.kind === 'string') return n.children.every((c) => !c.interior && c.kind === 'String');
|
|
44
|
+
if (n.kind === 'char') return n.children.length === 1 && n.children[0].kind === 'Char';
|
|
45
|
+
return (
|
|
46
|
+
n.kind === 'quote' &&
|
|
47
|
+
Boolean(n.flags & 256) &&
|
|
48
|
+
n.children.length === 1 &&
|
|
49
|
+
isBareName(n.children[0])
|
|
50
|
+
);
|
|
51
|
+
}
|
|
52
|
+
function discarded(node, source) {
|
|
53
|
+
for (let child = node, p = source.getParent(child); p; child = p, p = source.getParent(p)) {
|
|
54
|
+
if (sequences.has(p.kind)) {
|
|
55
|
+
if (p.children.indexOf(child) < p.children.length - 1) return true;
|
|
56
|
+
continue;
|
|
57
|
+
}
|
|
58
|
+
if (['function', 'macro', '->', 'do', 'module', 'baremodule'].includes(p.kind)) return false;
|
|
59
|
+
if (['for', 'while', 'finally'].includes(p.kind)) return p.children.at(-1) === child;
|
|
60
|
+
if (['if', 'elseif', '?', 'try', 'catch', 'else', 'parens'].includes(p.kind)) continue;
|
|
61
|
+
if (p.kind === 'let' && p.children.at(-1) === child) continue;
|
|
62
|
+
return false;
|
|
63
|
+
}
|
|
64
|
+
return false;
|
|
65
|
+
}
|
|
66
|
+
export default defineRule({
|
|
67
|
+
meta: {
|
|
68
|
+
type: 'suggestion',
|
|
69
|
+
schema: [
|
|
70
|
+
{
|
|
71
|
+
type: 'object',
|
|
72
|
+
properties: { checkTopLevel: { type: 'boolean' } },
|
|
73
|
+
additionalProperties: false,
|
|
74
|
+
},
|
|
75
|
+
],
|
|
76
|
+
docs: {
|
|
77
|
+
description: 'Report simple standalone expressions whose value is definitely discarded.',
|
|
78
|
+
},
|
|
79
|
+
messages: {
|
|
80
|
+
issue: 'This standalone expression has a discarded value.',
|
|
81
|
+
comparison: "This comparison's result is discarded (did you mean `=`, or `@assert`?).",
|
|
82
|
+
},
|
|
83
|
+
},
|
|
84
|
+
create(context) {
|
|
85
|
+
const source = context.sourceCode;
|
|
86
|
+
const plutoNotebook = /^### A Pluto\.jl notebook ###(?:\r?\n|$)/u.test(source.text);
|
|
87
|
+
function check(node) {
|
|
88
|
+
if ((node.kind !== 'JuliaFile' && !node.interior) || isOpaque(node, source)) return;
|
|
89
|
+
const parent = source.getParent(node);
|
|
90
|
+
let localContext = false;
|
|
91
|
+
for (let p = node; p; p = source.getParent(p)) {
|
|
92
|
+
if (['string', 'cmdstring'].includes(p.kind)) return;
|
|
93
|
+
if (['function', '->', 'do', 'for', 'while', 'let'].includes(p.kind)) localContext = true;
|
|
94
|
+
}
|
|
95
|
+
// File/module expressions may be outputs of Literate or notebook cells.
|
|
96
|
+
if (!localContext && !context.options[0]?.checkTopLevel) return;
|
|
97
|
+
if (parent?.kind === 'let' && parent.children[0] === node) return;
|
|
98
|
+
for (let p = parent; p; p = source.getParent(p)) {
|
|
99
|
+
if (['function', '->', 'do'].includes(p.kind)) break;
|
|
100
|
+
if (p.kind === 'struct') return;
|
|
101
|
+
}
|
|
102
|
+
for (let i = 0; i < node.children.length; i++) {
|
|
103
|
+
const statement = node.children[i];
|
|
104
|
+
let value = statement;
|
|
105
|
+
while (value.kind === 'parens' && value.children.length === 1) value = value.children[0];
|
|
106
|
+
// Bare reads create reactive dependencies in Pluto notebook cells, and bare literals in
|
|
107
|
+
// cells are display/test scaffolding; comparisons are still reported.
|
|
108
|
+
if (plutoNotebook && !isComparison(value)) continue;
|
|
109
|
+
// Local documentation strings are not always wrapped in a parser doc node.
|
|
110
|
+
if (value.kind === 'string' && i === 0 && ['function', '->', 'do'].includes(parent?.kind))
|
|
111
|
+
continue;
|
|
112
|
+
if (
|
|
113
|
+
value.kind === 'string' &&
|
|
114
|
+
[
|
|
115
|
+
'function',
|
|
116
|
+
'macro',
|
|
117
|
+
'struct',
|
|
118
|
+
'module',
|
|
119
|
+
'baremodule',
|
|
120
|
+
'const',
|
|
121
|
+
'global',
|
|
122
|
+
'local',
|
|
123
|
+
'macrocall',
|
|
124
|
+
'=',
|
|
125
|
+
].includes(node.children[i + 1]?.kind)
|
|
126
|
+
)
|
|
127
|
+
continue;
|
|
128
|
+
if (candidate(statement) && discarded(statement, source))
|
|
129
|
+
context.report({
|
|
130
|
+
node: statement,
|
|
131
|
+
messageId: isComparison(statement) ? 'comparison' : 'issue',
|
|
132
|
+
});
|
|
133
|
+
}
|
|
134
|
+
}
|
|
135
|
+
return { block: check, toplevel: check, JuliaFile: check };
|
|
136
|
+
},
|
|
137
|
+
});
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
import { defineRule } from './common.js';
|
|
2
|
+
import { getScopeManager } from '../scope.js';
|
|
3
|
+
export default defineRule({
|
|
4
|
+
meta: {
|
|
5
|
+
type: 'problem',
|
|
6
|
+
docs: { description: 'Report type parameters unused outside their own declaration.' },
|
|
7
|
+
schema: [
|
|
8
|
+
{
|
|
9
|
+
type: 'object',
|
|
10
|
+
properties: {
|
|
11
|
+
checkStructs: { type: 'boolean' },
|
|
12
|
+
ignoreFunctionsWithDynamicCode: { type: 'boolean' },
|
|
13
|
+
ignoreParameters: { type: 'array', items: { type: 'string' } },
|
|
14
|
+
},
|
|
15
|
+
additionalProperties: false,
|
|
16
|
+
},
|
|
17
|
+
],
|
|
18
|
+
messages: { unused: 'Type parameter "{{name}}" is never used outside its own declaration.' },
|
|
19
|
+
},
|
|
20
|
+
create(context) {
|
|
21
|
+
return {
|
|
22
|
+
JuliaFile() {
|
|
23
|
+
const m = getScopeManager(context.sourceCode),
|
|
24
|
+
o = context.options[0] ?? {},
|
|
25
|
+
ignore = new Set(o.ignoreParameters);
|
|
26
|
+
for (const d of m.definitions) {
|
|
27
|
+
if (d.kind !== 'type-parameter' || ignore.has(d.name)) continue;
|
|
28
|
+
if (d.scope.kind === 'type' && !o.checkStructs) continue;
|
|
29
|
+
if (!['function', 'type'].includes(d.scope.kind)) continue;
|
|
30
|
+
if (
|
|
31
|
+
o.ignoreFunctionsWithDynamicCode &&
|
|
32
|
+
(context.sourceCode.getAncestors(d.node).some((n) => n.kind === 'macrocall') ||
|
|
33
|
+
m.scopes.some((s) => {
|
|
34
|
+
if (
|
|
35
|
+
s.dynamic &&
|
|
36
|
+
s.node.range[0] >= d.scope.node.range[0] &&
|
|
37
|
+
s.node.range[1] <= d.scope.node.range[1]
|
|
38
|
+
)
|
|
39
|
+
return true;
|
|
40
|
+
for (let at = s; at; at = at.parent) if (at === d.scope) return s.dynamic;
|
|
41
|
+
return false;
|
|
42
|
+
}))
|
|
43
|
+
)
|
|
44
|
+
continue;
|
|
45
|
+
const used = d.binding.references.some(
|
|
46
|
+
(r) =>
|
|
47
|
+
r.read &&
|
|
48
|
+
!(r.node.range[0] >= d.parameter.range[0] && r.node.range[1] <= d.parameter.range[1]),
|
|
49
|
+
);
|
|
50
|
+
if (!used) context.report({ node: d.node, messageId: 'unused', data: { name: d.name } });
|
|
51
|
+
}
|
|
52
|
+
},
|
|
53
|
+
};
|
|
54
|
+
},
|
|
55
|
+
});
|
|
@@ -0,0 +1,123 @@
|
|
|
1
|
+
import { defineRule } from './common.js';
|
|
2
|
+
import { getScopeManager } from '../scope.js';
|
|
3
|
+
function macroOption(definition, source) {
|
|
4
|
+
const assignment = definition.assignment;
|
|
5
|
+
if (!assignment) return false;
|
|
6
|
+
let bracket = false;
|
|
7
|
+
for (
|
|
8
|
+
let child = assignment, parent = source.getParent(child);
|
|
9
|
+
parent;
|
|
10
|
+
child = parent, parent = source.getParent(parent)
|
|
11
|
+
) {
|
|
12
|
+
if (['function', 'macro', '->', 'do'].includes(parent.kind)) return false;
|
|
13
|
+
if (parent.kind === 'macrocall')
|
|
14
|
+
return bracket || (child === assignment && parent.children.indexOf(child) > 1);
|
|
15
|
+
if (['vect', 'hcat', 'vcat', 'ncat'].includes(parent.kind)) bracket = true;
|
|
16
|
+
}
|
|
17
|
+
return false;
|
|
18
|
+
}
|
|
19
|
+
export default defineRule({
|
|
20
|
+
meta: {
|
|
21
|
+
type: 'problem',
|
|
22
|
+
docs: { description: 'Report local bindings that are never read.' },
|
|
23
|
+
schema: [
|
|
24
|
+
{
|
|
25
|
+
type: 'object',
|
|
26
|
+
properties: {
|
|
27
|
+
imports: { enum: ['all', 'none'] },
|
|
28
|
+
args: { enum: ['none', 'all'] },
|
|
29
|
+
ignoreKeywords: { type: 'boolean' },
|
|
30
|
+
caughtErrors: { enum: ['all', 'none'] },
|
|
31
|
+
ignorePattern: { type: 'string' },
|
|
32
|
+
dynamicReads: { enum: ['strict', 'ignore-containing-scope'] },
|
|
33
|
+
},
|
|
34
|
+
additionalProperties: false,
|
|
35
|
+
},
|
|
36
|
+
],
|
|
37
|
+
messages: {
|
|
38
|
+
unusedImport: 'Imported name "{{name}}" is never read in this module.',
|
|
39
|
+
unused: 'Local "{{name}}" is never read.',
|
|
40
|
+
},
|
|
41
|
+
},
|
|
42
|
+
create(context) {
|
|
43
|
+
return {
|
|
44
|
+
JuliaFile() {
|
|
45
|
+
const m = getScopeManager(context.sourceCode, context),
|
|
46
|
+
o = context.options[0] ?? {};
|
|
47
|
+
let ignore;
|
|
48
|
+
try {
|
|
49
|
+
ignore = new RegExp(o.ignorePattern ?? '^_', 'u');
|
|
50
|
+
} catch {
|
|
51
|
+
return;
|
|
52
|
+
}
|
|
53
|
+
const excluded = new Set([
|
|
54
|
+
'import',
|
|
55
|
+
'function',
|
|
56
|
+
'macro',
|
|
57
|
+
'type',
|
|
58
|
+
'module',
|
|
59
|
+
'module-self',
|
|
60
|
+
'type-parameter',
|
|
61
|
+
'intrinsic',
|
|
62
|
+
'implicit-argument',
|
|
63
|
+
'macro-binding',
|
|
64
|
+
]);
|
|
65
|
+
for (const s of m.scopes)
|
|
66
|
+
for (const b of s.bindings.values()) {
|
|
67
|
+
if (!b.name || ignore.test(b.name) || b.references.some((r) => r.read)) continue;
|
|
68
|
+
if (
|
|
69
|
+
b.definitions.length &&
|
|
70
|
+
b.definitions.every((d) => macroOption(d, context.sourceCode))
|
|
71
|
+
)
|
|
72
|
+
continue;
|
|
73
|
+
if (s.kind === 'module') {
|
|
74
|
+
if (
|
|
75
|
+
b.kind !== 'import' ||
|
|
76
|
+
b.external ||
|
|
77
|
+
!b.definitions.length ||
|
|
78
|
+
o.imports === 'none' ||
|
|
79
|
+
!b.definitions.some((d) => d.named || d.aliased) ||
|
|
80
|
+
b.definitions.some((d) => ['function', 'macro'].includes(d.kind))
|
|
81
|
+
)
|
|
82
|
+
continue;
|
|
83
|
+
const models = m.moduleResolution.scopes.get(s) ?? [];
|
|
84
|
+
if (
|
|
85
|
+
models.some(
|
|
86
|
+
(model) =>
|
|
87
|
+
model.reads.has(b.name) ||
|
|
88
|
+
model.exports.has(b.name) ||
|
|
89
|
+
model.unresolvedIncludes ||
|
|
90
|
+
model.dynamicEvaluation,
|
|
91
|
+
)
|
|
92
|
+
)
|
|
93
|
+
continue;
|
|
94
|
+
context.report({
|
|
95
|
+
node: b.definitions[0].node,
|
|
96
|
+
messageId: 'unusedImport',
|
|
97
|
+
data: { name: b.name },
|
|
98
|
+
});
|
|
99
|
+
continue;
|
|
100
|
+
}
|
|
101
|
+
if (excluded.has(b.kind)) continue;
|
|
102
|
+
if (
|
|
103
|
+
b.definitions.some((d) => ['argument', 'keyword'].includes(d.kind)) &&
|
|
104
|
+
o.args !== 'all'
|
|
105
|
+
)
|
|
106
|
+
continue;
|
|
107
|
+
if (o.ignoreKeywords && b.definitions.some((d) => d.kind === 'keyword')) continue;
|
|
108
|
+
if (o.caughtErrors === 'none' && b.kind === 'catch') continue;
|
|
109
|
+
if (o.dynamicReads === 'ignore-containing-scope') {
|
|
110
|
+
let dynamic = false;
|
|
111
|
+
for (let at = s; at && at.kind !== 'module'; at = at.parent) dynamic ||= at.dynamic;
|
|
112
|
+
if (dynamic) continue;
|
|
113
|
+
}
|
|
114
|
+
context.report({
|
|
115
|
+
node: b.definitions[0].node,
|
|
116
|
+
messageId: 'unused',
|
|
117
|
+
data: { name: b.name },
|
|
118
|
+
});
|
|
119
|
+
}
|
|
120
|
+
},
|
|
121
|
+
};
|
|
122
|
+
},
|
|
123
|
+
});
|
|
@@ -0,0 +1,139 @@
|
|
|
1
|
+
import { defineRule } from './common.js';
|
|
2
|
+
import { getScopeManager } from '../scope.js';
|
|
3
|
+
export default defineRule({
|
|
4
|
+
meta: {
|
|
5
|
+
type: 'problem',
|
|
6
|
+
docs: { description: 'Report local reads before straight-line initialization.' },
|
|
7
|
+
schema: [
|
|
8
|
+
{
|
|
9
|
+
type: 'object',
|
|
10
|
+
properties: {
|
|
11
|
+
checkGlobals: { type: 'boolean' },
|
|
12
|
+
functions: { type: 'boolean' },
|
|
13
|
+
dynamicInitialization: { enum: ['strict', 'ignore-containing-scope'] },
|
|
14
|
+
},
|
|
15
|
+
additionalProperties: false,
|
|
16
|
+
},
|
|
17
|
+
],
|
|
18
|
+
messages: { before: 'Local "{{name}}" is read before its first definition.' },
|
|
19
|
+
},
|
|
20
|
+
create(context) {
|
|
21
|
+
return {
|
|
22
|
+
JuliaFile() {
|
|
23
|
+
const source = context.sourceCode,
|
|
24
|
+
m = getScopeManager(source),
|
|
25
|
+
o = context.options[0] ?? {};
|
|
26
|
+
const signatures = new Map(m.functions.map((fn) => [fn.scope, fn.node.children[0].range]));
|
|
27
|
+
const barriers = new Set(['if', 'elseif', 'while', 'for', 'try', 'catch', '&&', '||', '?']);
|
|
28
|
+
function barrier(node) {
|
|
29
|
+
const pending = [node];
|
|
30
|
+
while (pending.length) {
|
|
31
|
+
const n = pending.pop();
|
|
32
|
+
if (['function', '->', 'do', 'module', 'struct'].includes(n.kind)) continue;
|
|
33
|
+
if (
|
|
34
|
+
barriers.has(n.kind) ||
|
|
35
|
+
['return', 'break', 'continue', 'generator'].includes(n.kind)
|
|
36
|
+
)
|
|
37
|
+
return true;
|
|
38
|
+
pending.push(...n.children);
|
|
39
|
+
}
|
|
40
|
+
return false;
|
|
41
|
+
}
|
|
42
|
+
function dynamicRegion(s, node) {
|
|
43
|
+
let containing = s;
|
|
44
|
+
for (let at = s; at && at.kind !== 'module'; at = at.parent)
|
|
45
|
+
if (at.kind === 'function') {
|
|
46
|
+
containing = at;
|
|
47
|
+
break;
|
|
48
|
+
}
|
|
49
|
+
return (
|
|
50
|
+
source.getAncestors(node).some((n) => n.kind === 'macrocall') ||
|
|
51
|
+
m.scopes.some((child) => {
|
|
52
|
+
if (
|
|
53
|
+
child.dynamic &&
|
|
54
|
+
child.node.range[0] >= containing.node.range[0] &&
|
|
55
|
+
child.node.range[1] <= containing.node.range[1]
|
|
56
|
+
)
|
|
57
|
+
return true;
|
|
58
|
+
for (let at = child; at; at = at.parent) if (at === containing) return child.dynamic;
|
|
59
|
+
return false;
|
|
60
|
+
})
|
|
61
|
+
);
|
|
62
|
+
}
|
|
63
|
+
function straight(node, s) {
|
|
64
|
+
for (let at = node; at && at !== s.node; at = source.getParent(at)) {
|
|
65
|
+
const parent = source.getParent(at);
|
|
66
|
+
if (barriers.has(at.kind)) return false;
|
|
67
|
+
if (
|
|
68
|
+
parent?.kind === 'block' ||
|
|
69
|
+
parent?.kind === 'toplevel' ||
|
|
70
|
+
parent?.kind === 'JuliaFile'
|
|
71
|
+
) {
|
|
72
|
+
for (const prev of parent.children) {
|
|
73
|
+
if (prev === at) break;
|
|
74
|
+
if (barrier(prev)) return false;
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
return true;
|
|
79
|
+
}
|
|
80
|
+
for (const r of m.references) {
|
|
81
|
+
const b = r.binding;
|
|
82
|
+
if (['signature', 'default'].includes(r.scope.kind)) continue;
|
|
83
|
+
if (!b || b.kind === 'builtin' || r.opaque || b.scope !== r.scope || !r.read) continue;
|
|
84
|
+
if (b.scope.kind === 'module' && !o.checkGlobals) continue;
|
|
85
|
+
if (
|
|
86
|
+
[
|
|
87
|
+
'type-parameter',
|
|
88
|
+
'import',
|
|
89
|
+
'intrinsic',
|
|
90
|
+
'implicit-argument',
|
|
91
|
+
'module-self',
|
|
92
|
+
'module',
|
|
93
|
+
'iteration',
|
|
94
|
+
'catch',
|
|
95
|
+
// declared by a macro: its position isn't known
|
|
96
|
+
'macro-binding',
|
|
97
|
+
].includes(b.kind)
|
|
98
|
+
)
|
|
99
|
+
continue;
|
|
100
|
+
if (['function', 'macro', 'type'].includes(b.kind) && o.functions !== true) continue;
|
|
101
|
+
if (
|
|
102
|
+
o.dynamicInitialization === 'ignore-containing-scope' &&
|
|
103
|
+
dynamicRegion(r.scope, r.node)
|
|
104
|
+
)
|
|
105
|
+
continue;
|
|
106
|
+
const signature = signatures.get(r.scope);
|
|
107
|
+
if (
|
|
108
|
+
signature &&
|
|
109
|
+
r.node.range[0] >= signature[0] &&
|
|
110
|
+
r.node.range[1] <= signature[1] &&
|
|
111
|
+
source.getAncestors(r.node).some((n) => n.kind === 'macrocall')
|
|
112
|
+
)
|
|
113
|
+
continue;
|
|
114
|
+
if (!straight(r.node, r.scope)) continue;
|
|
115
|
+
const initial = b.definitions
|
|
116
|
+
.filter(
|
|
117
|
+
(d) =>
|
|
118
|
+
['argument', 'keyword', 'iteration', 'catch', 'function', 'macro', 'type'].includes(
|
|
119
|
+
d.kind,
|
|
120
|
+
) ||
|
|
121
|
+
(d.kind === 'let' && d.assignment?.kind === '=') ||
|
|
122
|
+
d.initialized,
|
|
123
|
+
)
|
|
124
|
+
.filter((d) => straight(d.node, d.scope));
|
|
125
|
+
if (
|
|
126
|
+
initial.some(
|
|
127
|
+
(d) =>
|
|
128
|
+
(d.kind === 'assignment' ? d.assignment.range[1] : d.node.range[1]) <=
|
|
129
|
+
r.node.range[0],
|
|
130
|
+
)
|
|
131
|
+
)
|
|
132
|
+
continue;
|
|
133
|
+
// Bare local without an initializer does not initialize. A future local assignment does.
|
|
134
|
+
context.report({ node: r.node, messageId: 'before', data: { name: r.name } });
|
|
135
|
+
}
|
|
136
|
+
},
|
|
137
|
+
};
|
|
138
|
+
},
|
|
139
|
+
});
|
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
import {
|
|
2
|
+
check,
|
|
3
|
+
ordinary,
|
|
4
|
+
integer,
|
|
5
|
+
strip,
|
|
6
|
+
identifierName,
|
|
7
|
+
hasCaptureOrOpaqueUse,
|
|
8
|
+
span,
|
|
9
|
+
} from './semantic-common.js';
|
|
10
|
+
const hasDynamicScope = (scope) => scope.dynamic || scope.children.some(hasDynamicScope);
|
|
11
|
+
export default check(
|
|
12
|
+
'Report adjacent literal stores overwritten before a local is read.',
|
|
13
|
+
(context, m, source) => ({
|
|
14
|
+
JuliaFile() {
|
|
15
|
+
const smallLiteral = (node) => {
|
|
16
|
+
node = strip(node);
|
|
17
|
+
if (node?.kind === 'Bool') return true;
|
|
18
|
+
const value = integer(node);
|
|
19
|
+
return value !== null && value >= -2147483648n && value <= 2147483647n;
|
|
20
|
+
};
|
|
21
|
+
for (const fn of m.functions) {
|
|
22
|
+
if (fn.node.kind !== 'function' || !ordinary(fn.node, source)) continue;
|
|
23
|
+
const body = fn.node.children.at(-1);
|
|
24
|
+
if (body.kind !== 'block' || hasDynamicScope(fn.scope)) continue;
|
|
25
|
+
const statements = body.children;
|
|
26
|
+
for (let i = 0; i + 1 < statements.length; i++) {
|
|
27
|
+
const a = statements[i],
|
|
28
|
+
b = statements[i + 1];
|
|
29
|
+
if (
|
|
30
|
+
a.kind !== '=' ||
|
|
31
|
+
b.kind !== '=' ||
|
|
32
|
+
!smallLiteral(a.children[1]) ||
|
|
33
|
+
!smallLiteral(b.children[1])
|
|
34
|
+
)
|
|
35
|
+
continue;
|
|
36
|
+
if (identifierName(a.children[0]) === null || identifierName(b.children[0]) === null)
|
|
37
|
+
continue;
|
|
38
|
+
const binding = m.resolve(a.children[0]);
|
|
39
|
+
if (
|
|
40
|
+
!binding ||
|
|
41
|
+
binding !== m.resolve(b.children[0]) ||
|
|
42
|
+
binding.scope !== fn.scope ||
|
|
43
|
+
binding.constant ||
|
|
44
|
+
binding.definitions.some((d) => d.typed || d.declaration || d.opaque) ||
|
|
45
|
+
hasCaptureOrOpaqueUse(binding, source)
|
|
46
|
+
)
|
|
47
|
+
continue;
|
|
48
|
+
if (!binding.references.some((r) => r.read && r.node.range[0] > b.range[1])) continue;
|
|
49
|
+
const range = span(source, a);
|
|
50
|
+
const comments = source.comments.some(
|
|
51
|
+
(c) => c.range[0] >= a.range[0] && c.range[1] <= b.range[0],
|
|
52
|
+
);
|
|
53
|
+
context.report({
|
|
54
|
+
node: a,
|
|
55
|
+
messageId: 'diagnostic',
|
|
56
|
+
data: {
|
|
57
|
+
message:
|
|
58
|
+
'This literal local value is overwritten by the next statement before it is read.',
|
|
59
|
+
},
|
|
60
|
+
...(!comments && {
|
|
61
|
+
suggest: [
|
|
62
|
+
{
|
|
63
|
+
messageId: 'replace',
|
|
64
|
+
data: { message: 'Remove the overwritten store.' },
|
|
65
|
+
fix: (fixer) => fixer.removeRange(range),
|
|
66
|
+
},
|
|
67
|
+
],
|
|
68
|
+
}),
|
|
69
|
+
});
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
},
|
|
73
|
+
}),
|
|
74
|
+
{ suggestions: true },
|
|
75
|
+
);
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import { check, ordinary, strip, call } from './semantic-common.js';
|
|
2
|
+
export default check(
|
|
3
|
+
'Report catches that only rethrow the active exception.',
|
|
4
|
+
(context, m, source, options) => ({
|
|
5
|
+
catch(node) {
|
|
6
|
+
if (!node.interior || !ordinary(node, source)) return;
|
|
7
|
+
const parent = source.getParent(node);
|
|
8
|
+
if (parent?.kind !== 'try' || parent.children.length !== 2) return;
|
|
9
|
+
const body = node.children.at(-1);
|
|
10
|
+
if (body?.kind !== 'block' || body.children.length !== 1) return;
|
|
11
|
+
const expression = strip(body.children[0]);
|
|
12
|
+
if (strip(expression.children[0])?.kind === 'curly') return;
|
|
13
|
+
const args = call(expression, 'rethrow', m);
|
|
14
|
+
if (!args || args.length) return;
|
|
15
|
+
if (
|
|
16
|
+
options.allowComment !== false &&
|
|
17
|
+
source.comments.some((c) => c.range[0] >= node.range[0] && c.range[1] <= node.range[1])
|
|
18
|
+
)
|
|
19
|
+
return;
|
|
20
|
+
context.report({
|
|
21
|
+
node,
|
|
22
|
+
messageId: 'diagnostic',
|
|
23
|
+
data: { message: 'This catch only rethrows the active exception.' },
|
|
24
|
+
});
|
|
25
|
+
},
|
|
26
|
+
}),
|
|
27
|
+
{ properties: { allowComment: { type: 'boolean' } } },
|
|
28
|
+
);
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import { defineRule } from './common.js';
|
|
2
|
+
export default defineRule({
|
|
3
|
+
meta: {
|
|
4
|
+
type: 'problem',
|
|
5
|
+
schema: [],
|
|
6
|
+
docs: { description: 'Report JuliaSyntax errors in recovery mode.' },
|
|
7
|
+
messages: { parse: '{{message}}' },
|
|
8
|
+
},
|
|
9
|
+
create(context) {
|
|
10
|
+
return {
|
|
11
|
+
JuliaFile() {
|
|
12
|
+
for (const d of context.sourceCode.diagnostics) {
|
|
13
|
+
if (d.level === 1)
|
|
14
|
+
context.report({ loc: d.loc, messageId: 'parse', data: { message: d.message } });
|
|
15
|
+
}
|
|
16
|
+
},
|
|
17
|
+
};
|
|
18
|
+
},
|
|
19
|
+
});
|