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,49 @@
|
|
|
1
|
+
import { check, ordinary, binary, call, integer, strip } from './semantic-common.js';
|
|
2
|
+
export default check(
|
|
3
|
+
'Report comparisons inconsistent with nonnegative builtin length.',
|
|
4
|
+
(context, m, source, options) => ({
|
|
5
|
+
call(node) {
|
|
6
|
+
const p = binary(node);
|
|
7
|
+
if (!p || !ordinary(node, source)) return;
|
|
8
|
+
let [a, op, b] = p,
|
|
9
|
+
operator = op.value;
|
|
10
|
+
if (
|
|
11
|
+
!['<', '<=', '>', '>=', '==', '!='].includes(operator) ||
|
|
12
|
+
!m.isUnshadowedBuiltin(op, operator)
|
|
13
|
+
)
|
|
14
|
+
return;
|
|
15
|
+
const lengthArgs = (expression) => {
|
|
16
|
+
expression = strip(expression);
|
|
17
|
+
if (strip(expression.children[0])?.kind === 'curly') return null;
|
|
18
|
+
const args = call(expression, 'length', m);
|
|
19
|
+
return args?.length === 1 && !['...', 'parameters'].includes(strip(args[0]).kind)
|
|
20
|
+
? args
|
|
21
|
+
: null;
|
|
22
|
+
};
|
|
23
|
+
let args = lengthArgs(a),
|
|
24
|
+
bound = integer(b);
|
|
25
|
+
if (!args) {
|
|
26
|
+
args = lengthArgs(b);
|
|
27
|
+
bound = integer(a);
|
|
28
|
+
operator = { '<': '>', '<=': '>=', '>': '<', '>=': '<=', '==': '==', '!=': '!=' }[operator];
|
|
29
|
+
}
|
|
30
|
+
if (
|
|
31
|
+
args?.length !== 1 ||
|
|
32
|
+
bound === null ||
|
|
33
|
+
bound > 0n ||
|
|
34
|
+
(bound === 0n && !['<', '>='].includes(operator))
|
|
35
|
+
)
|
|
36
|
+
return;
|
|
37
|
+
const collection = strip(args[0]);
|
|
38
|
+
if (options.knownCollectionsOnly && !['tuple', 'vect', 'string'].includes(collection.kind))
|
|
39
|
+
return;
|
|
40
|
+
const result = ['>', '>=', '!='].includes(operator);
|
|
41
|
+
context.report({
|
|
42
|
+
node,
|
|
43
|
+
messageId: 'diagnostic',
|
|
44
|
+
data: { message: `A nonnegative length makes this comparison always ${result}.` },
|
|
45
|
+
});
|
|
46
|
+
},
|
|
47
|
+
}),
|
|
48
|
+
{ properties: { knownCollectionsOnly: { type: 'boolean' } } },
|
|
49
|
+
);
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
import {
|
|
2
|
+
checkRule,
|
|
3
|
+
ordinary,
|
|
4
|
+
strip,
|
|
5
|
+
call,
|
|
6
|
+
nodes,
|
|
7
|
+
report,
|
|
8
|
+
identifierName,
|
|
9
|
+
} from './correctness-common.js';
|
|
10
|
+
import { isOpaque } from './common.js';
|
|
11
|
+
export default checkRule(
|
|
12
|
+
'Report literal foreign-call arity and annotation errors.',
|
|
13
|
+
(context, m, source) => ({
|
|
14
|
+
call(node) {
|
|
15
|
+
if (!ordinary(node, source)) return;
|
|
16
|
+
const args = node.children.slice(1);
|
|
17
|
+
if (args.length < 3) return;
|
|
18
|
+
const tuple = strip(args[2]);
|
|
19
|
+
if (
|
|
20
|
+
tuple.kind !== 'tuple' ||
|
|
21
|
+
args.some((n) => [...nodes(n)].some((c) => ['...', 'parameters'].includes(c.kind)))
|
|
22
|
+
)
|
|
23
|
+
return;
|
|
24
|
+
if (tuple.children.length !== args.length - 3 && call(node, 'ccall', m))
|
|
25
|
+
report(
|
|
26
|
+
context,
|
|
27
|
+
node,
|
|
28
|
+
'The literal ccall type tuple and supplied value arguments have different lengths.',
|
|
29
|
+
);
|
|
30
|
+
},
|
|
31
|
+
macrocall(node) {
|
|
32
|
+
// @ccall is the sole transparent macro here. All enclosing macros stay opaque.
|
|
33
|
+
if (
|
|
34
|
+
!node.interior ||
|
|
35
|
+
source.getAncestors(node).some((n) => ['macrocall', 'quote', 'inert'].includes(n.kind))
|
|
36
|
+
)
|
|
37
|
+
return;
|
|
38
|
+
const [head, expression] = node.children;
|
|
39
|
+
const qualified =
|
|
40
|
+
head?.kind === '.' &&
|
|
41
|
+
identifierName(head.children[1]) === '@ccall' &&
|
|
42
|
+
m.isUnshadowedBuiltin(head.children[0], 'Base');
|
|
43
|
+
const binding = identifierName(head) === '@ccall' && m.lookup('@ccall', m.scopeFor(node));
|
|
44
|
+
if (!qualified && binding?.kind !== 'builtin') return;
|
|
45
|
+
const annotated = strip(expression);
|
|
46
|
+
if (node.children.length !== 2 || annotated?.kind !== '::') return;
|
|
47
|
+
const invocation = strip(annotated.children[0]);
|
|
48
|
+
if (invocation?.kind !== 'call' || isOpaque(source.getParent(node), source)) return;
|
|
49
|
+
const args = invocation.children
|
|
50
|
+
.slice(1)
|
|
51
|
+
.flatMap((n) => (n.kind === 'parameters' ? n.children : [n]));
|
|
52
|
+
if (args.some((n) => [...nodes(n)].some((c) => c.kind === '...'))) return;
|
|
53
|
+
for (const arg of args)
|
|
54
|
+
if (strip(arg).kind !== '::')
|
|
55
|
+
report(context, arg, 'Each @ccall value argument needs an explicit ::CType annotation.');
|
|
56
|
+
},
|
|
57
|
+
}),
|
|
58
|
+
);
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import { checkRule, ordinary, call, localSymbol, report } from './correctness-common.js';
|
|
2
|
+
export default checkRule(
|
|
3
|
+
'Report one-argument isdefined calls naming lexical locals.',
|
|
4
|
+
(context, m, source) => ({
|
|
5
|
+
call(node) {
|
|
6
|
+
if (
|
|
7
|
+
!ordinary(node, source) ||
|
|
8
|
+
node.children.length !== 2 ||
|
|
9
|
+
node.children[1]?.kind !== 'quote' ||
|
|
10
|
+
node.children[1].children.length !== 1 ||
|
|
11
|
+
node.children[1].children[0].kind !== 'Identifier'
|
|
12
|
+
)
|
|
13
|
+
return;
|
|
14
|
+
const args = call(node, 'isdefined', m);
|
|
15
|
+
if (args?.length !== 1 || !localSymbol(args[0], node, m)) return;
|
|
16
|
+
report(
|
|
17
|
+
context,
|
|
18
|
+
node,
|
|
19
|
+
'isdefined needs an object and field; use @isdefined(name) to test a local binding.',
|
|
20
|
+
);
|
|
21
|
+
},
|
|
22
|
+
}),
|
|
23
|
+
);
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
import { defineRule, isOpaque } from './common.js';
|
|
2
|
+
export default defineRule({
|
|
3
|
+
meta: {
|
|
4
|
+
type: 'problem',
|
|
5
|
+
schema: [],
|
|
6
|
+
docs: {
|
|
7
|
+
description: 'Report integer-valued decimal float literals that lose integer precision.',
|
|
8
|
+
},
|
|
9
|
+
messages: {
|
|
10
|
+
precision:
|
|
11
|
+
'This {{format}} literal cannot exactly represent the written integer {{integer}}.',
|
|
12
|
+
},
|
|
13
|
+
},
|
|
14
|
+
create(context) {
|
|
15
|
+
function inspect(node) {
|
|
16
|
+
if (isOpaque(node, context.sourceCode)) return;
|
|
17
|
+
const raw = node.value.replaceAll('_', '');
|
|
18
|
+
if (raw.length > 330) return;
|
|
19
|
+
const match = raw.match(/^(-?\d+)(?:\.(0+))?(f0)?$/u);
|
|
20
|
+
if (
|
|
21
|
+
!match ||
|
|
22
|
+
(node.kind === 'Float' && (!match[2] || match[3])) ||
|
|
23
|
+
(node.kind === 'Float32' && !match[3])
|
|
24
|
+
)
|
|
25
|
+
return;
|
|
26
|
+
let value = BigInt(match[1]);
|
|
27
|
+
if (value < 0n) value = -value;
|
|
28
|
+
const bits = node.kind === 'Float32' ? 24 : 53;
|
|
29
|
+
const max = ((1n << BigInt(bits)) - 1n) << BigInt((bits === 24 ? 127 : 1023) - bits + 1);
|
|
30
|
+
if (value > max) return;
|
|
31
|
+
const excess = value.toString(2).length - bits;
|
|
32
|
+
if (excess <= 0 || value % (1n << BigInt(excess)) === 0n) return;
|
|
33
|
+
context.report({
|
|
34
|
+
node,
|
|
35
|
+
messageId: 'precision',
|
|
36
|
+
data: { format: node.kind === 'Float32' ? 'Float32' : 'Float64', integer: match[1] },
|
|
37
|
+
});
|
|
38
|
+
}
|
|
39
|
+
return { Float: inspect, Float32: inspect };
|
|
40
|
+
},
|
|
41
|
+
});
|
|
@@ -0,0 +1,127 @@
|
|
|
1
|
+
import {
|
|
2
|
+
checkRule,
|
|
3
|
+
ordinary,
|
|
4
|
+
strip,
|
|
5
|
+
nodes,
|
|
6
|
+
statements,
|
|
7
|
+
call,
|
|
8
|
+
report,
|
|
9
|
+
smallInteger,
|
|
10
|
+
identifierName,
|
|
11
|
+
} from './correctness-common.js';
|
|
12
|
+
const types = ['Int', 'Int64', 'Float64', 'Bool', 'String'];
|
|
13
|
+
export default checkRule(
|
|
14
|
+
'Report obvious nothing returns converted to incompatible builtin types.',
|
|
15
|
+
(context, m, source) => ({
|
|
16
|
+
function(node) {
|
|
17
|
+
if (!node.interior || !ordinary(node, source)) return;
|
|
18
|
+
let signature = strip(node.children[0]);
|
|
19
|
+
while (signature?.kind === 'where') signature = strip(signature.children[0]);
|
|
20
|
+
if (signature?.kind !== '::' || signature.children.length !== 2) return;
|
|
21
|
+
const annotation = strip(signature.children[1]);
|
|
22
|
+
const type = identifierName(annotation.kind === '.' ? annotation.children[1] : annotation);
|
|
23
|
+
if (!types.includes(type) || !m.isUnshadowedBuiltin(annotation, type)) return;
|
|
24
|
+
const body = node.children.at(-1),
|
|
25
|
+
bad = new Set();
|
|
26
|
+
// Empty annotated methods are commonly intentional interface placeholders.
|
|
27
|
+
if (body.kind === 'block' && !statements(body).length) return;
|
|
28
|
+
// Unmodeled exception handling or macro expansion could replace the result.
|
|
29
|
+
function unsupported(n) {
|
|
30
|
+
if (['function', '->', 'do', 'quote', 'inert'].includes(n.kind)) return false;
|
|
31
|
+
return (
|
|
32
|
+
['try', 'macrocall', '&&', '||', '?'].includes(n.kind) || n.children.some(unsupported)
|
|
33
|
+
);
|
|
34
|
+
}
|
|
35
|
+
if (unsupported(body)) return;
|
|
36
|
+
function analyze(n, terminal) {
|
|
37
|
+
n = strip(n);
|
|
38
|
+
if (['break', 'continue'].includes(n.kind)) return false;
|
|
39
|
+
if (n.kind === 'return') {
|
|
40
|
+
if (!n.children.length || m.isUnshadowedBuiltin(strip(n.children[0]), 'nothing'))
|
|
41
|
+
bad.add(n);
|
|
42
|
+
return false;
|
|
43
|
+
}
|
|
44
|
+
if (['throw', 'error'].some((name) => call(n, name, m))) return false;
|
|
45
|
+
if (['block', 'toplevel'].includes(n.kind)) {
|
|
46
|
+
const parts = statements(n);
|
|
47
|
+
if (!parts.length && terminal) bad.add(n);
|
|
48
|
+
let reachable = true;
|
|
49
|
+
for (let i = 0; reachable && i < parts.length; i++)
|
|
50
|
+
reachable = analyze(parts[i], terminal && i === parts.length - 1);
|
|
51
|
+
return reachable;
|
|
52
|
+
}
|
|
53
|
+
if (['if', 'elseif'].includes(n.kind)) {
|
|
54
|
+
const [condition, yes, no] = n.children;
|
|
55
|
+
const constant = strip(condition).kind === 'Bool' ? strip(condition).value : null;
|
|
56
|
+
if (constant === 'true') return analyze(yes, terminal);
|
|
57
|
+
if (constant === 'false') {
|
|
58
|
+
if (no) return analyze(no, terminal);
|
|
59
|
+
if (terminal) bad.add(n);
|
|
60
|
+
return true;
|
|
61
|
+
}
|
|
62
|
+
const left = analyze(yes, terminal),
|
|
63
|
+
right = no ? analyze(no, terminal) : true;
|
|
64
|
+
if (!no && terminal) bad.add(n);
|
|
65
|
+
return left || right;
|
|
66
|
+
}
|
|
67
|
+
if (['for', 'while'].includes(n.kind)) {
|
|
68
|
+
// Inspect explicit returns without treating each iteration as a return value.
|
|
69
|
+
let nonempty = false,
|
|
70
|
+
empty =
|
|
71
|
+
n.kind === 'while' &&
|
|
72
|
+
strip(n.children[0]).kind === 'Bool' &&
|
|
73
|
+
strip(n.children[0]).value === 'false';
|
|
74
|
+
if (n.kind === 'for' && n.children[0]?.children.length === 1) {
|
|
75
|
+
const range = strip(n.children[0].children[0].children[1]);
|
|
76
|
+
if (
|
|
77
|
+
range?.kind === 'call' &&
|
|
78
|
+
[3, 4].includes(range.children.length) &&
|
|
79
|
+
range.children[1].value === ':' &&
|
|
80
|
+
m.isUnshadowedBuiltin(range.children[1], ':')
|
|
81
|
+
) {
|
|
82
|
+
const values = [range.children[0], ...range.children.slice(2)].map(smallInteger);
|
|
83
|
+
if (values.every((v) => v !== null)) {
|
|
84
|
+
const [a, step, b] = values.length === 2 ? [values[0], 1, values[1]] : values;
|
|
85
|
+
if (step === 0) return false; // Builtin range construction throws before the body runs.
|
|
86
|
+
nonempty = (step > 0 && a <= b) || (step < 0 && a >= b);
|
|
87
|
+
empty = (step > 0 && a > b) || (step < 0 && a < b);
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
if (empty) {
|
|
92
|
+
if (terminal) bad.add(n);
|
|
93
|
+
return true;
|
|
94
|
+
}
|
|
95
|
+
const bodyCompletes = analyze(n.children.at(-1), false);
|
|
96
|
+
// A guaranteed nonempty loop that returns or throws cannot fall through.
|
|
97
|
+
const exitsFunction =
|
|
98
|
+
!bodyCompletes &&
|
|
99
|
+
![...nodes(n.children.at(-1))].some((c) => ['break', 'continue'].includes(c.kind));
|
|
100
|
+
if (nonempty && exitsFunction) return false;
|
|
101
|
+
function targetsBreak(part) {
|
|
102
|
+
if (['for', 'while', 'function', '->', 'do', 'quote', 'inert'].includes(part.kind))
|
|
103
|
+
return false;
|
|
104
|
+
return part.kind === 'break' || part.children.some(targetsBreak);
|
|
105
|
+
}
|
|
106
|
+
const endless =
|
|
107
|
+
n.kind === 'while' &&
|
|
108
|
+
strip(n.children[0]).kind === 'Bool' &&
|
|
109
|
+
strip(n.children[0]).value === 'true' &&
|
|
110
|
+
!targetsBreak(n.children.at(-1));
|
|
111
|
+
if (terminal && !endless) bad.add(n);
|
|
112
|
+
return !endless;
|
|
113
|
+
}
|
|
114
|
+
if (['function', '->', 'do', 'quote', 'inert'].includes(n.kind)) return true;
|
|
115
|
+
if (terminal && m.isUnshadowedBuiltin(n, 'nothing')) bad.add(n);
|
|
116
|
+
return true;
|
|
117
|
+
}
|
|
118
|
+
analyze(body, true);
|
|
119
|
+
for (const n of bad)
|
|
120
|
+
report(
|
|
121
|
+
context,
|
|
122
|
+
n,
|
|
123
|
+
'This return path produces nothing, which cannot convert to the annotated builtin return type.',
|
|
124
|
+
);
|
|
125
|
+
},
|
|
126
|
+
}),
|
|
127
|
+
);
|
|
@@ -0,0 +1,112 @@
|
|
|
1
|
+
import { readFileSync } from 'node:fs';
|
|
2
|
+
import { dirname, join } from 'node:path';
|
|
3
|
+
import { check, ordinary, strip, identifierName, builtinTypes } from './semantic-common.js';
|
|
4
|
+
|
|
5
|
+
// Ownership is deliberately limited to named projects and explicit Base/Core
|
|
6
|
+
// generic extensions. External/imported bindings alone do not prove ownership.
|
|
7
|
+
function projectName(filename) {
|
|
8
|
+
if (!filename || /^<.*>$/u.test(filename)) return null;
|
|
9
|
+
for (let dir = dirname(filename); ;) {
|
|
10
|
+
try {
|
|
11
|
+
const text = readFileSync(join(dir, 'Project.toml'), 'utf8').split(/^\s*\[/mu)[0];
|
|
12
|
+
const name = text.match(/^\s*name\s*=\s*"([^"]+)"/mu)?.[1];
|
|
13
|
+
if (name) return name;
|
|
14
|
+
} catch {
|
|
15
|
+
/* No readable named project here: keep looking upwards. */
|
|
16
|
+
}
|
|
17
|
+
const parent = dirname(dir);
|
|
18
|
+
if (parent === dir) return null;
|
|
19
|
+
dir = parent;
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
const generics = new Set([
|
|
23
|
+
'length',
|
|
24
|
+
'isempty',
|
|
25
|
+
'copy',
|
|
26
|
+
'deepcopy',
|
|
27
|
+
'show',
|
|
28
|
+
'summary',
|
|
29
|
+
'print',
|
|
30
|
+
'println',
|
|
31
|
+
'getindex',
|
|
32
|
+
'setindex!',
|
|
33
|
+
'iterate',
|
|
34
|
+
'eltype',
|
|
35
|
+
'size',
|
|
36
|
+
'axes',
|
|
37
|
+
'first',
|
|
38
|
+
'last',
|
|
39
|
+
'hash',
|
|
40
|
+
'isequal',
|
|
41
|
+
'isless',
|
|
42
|
+
'convert',
|
|
43
|
+
'promote_rule',
|
|
44
|
+
'zero',
|
|
45
|
+
'one',
|
|
46
|
+
'keys',
|
|
47
|
+
'values',
|
|
48
|
+
'pairs',
|
|
49
|
+
'push!',
|
|
50
|
+
'append!',
|
|
51
|
+
'empty!',
|
|
52
|
+
'get',
|
|
53
|
+
'get!',
|
|
54
|
+
'haskey',
|
|
55
|
+
'delete!',
|
|
56
|
+
'in',
|
|
57
|
+
'string',
|
|
58
|
+
'repr',
|
|
59
|
+
'read',
|
|
60
|
+
'write',
|
|
61
|
+
'close',
|
|
62
|
+
'flush',
|
|
63
|
+
]);
|
|
64
|
+
export default check(
|
|
65
|
+
'Report simple all-builtin Base/Core method extensions in named projects.',
|
|
66
|
+
(context, m, source) => {
|
|
67
|
+
const owner = projectName(context.physicalFilename);
|
|
68
|
+
if (!owner || ['Base', 'Core'].includes(owner)) return {};
|
|
69
|
+
return {
|
|
70
|
+
JuliaFile() {
|
|
71
|
+
for (const fn of m.functions) {
|
|
72
|
+
if (
|
|
73
|
+
fn.node.kind !== 'function' ||
|
|
74
|
+
!fn.call ||
|
|
75
|
+
fn.typeParameters.length ||
|
|
76
|
+
!ordinary(fn.node, source)
|
|
77
|
+
)
|
|
78
|
+
continue;
|
|
79
|
+
let internal = false;
|
|
80
|
+
for (let s = fn.scope.parent; s; s = s.parent)
|
|
81
|
+
if (
|
|
82
|
+
s.kind === 'module' &&
|
|
83
|
+
['Base', 'Core'].includes(identifierName(s.node.children[0]))
|
|
84
|
+
)
|
|
85
|
+
internal = true;
|
|
86
|
+
if (internal) continue;
|
|
87
|
+
const head = strip(fn.call.children[0]);
|
|
88
|
+
if (head?.kind !== '.') continue;
|
|
89
|
+
const name = identifierName(head.children[1]);
|
|
90
|
+
if (!generics.has(name) || !m.isUnshadowedBuiltin(head, name)) continue;
|
|
91
|
+
const args = fn.call.children.slice(1);
|
|
92
|
+
if (!args.length) continue;
|
|
93
|
+
const foreign = args.every((arg) => {
|
|
94
|
+
if (identifierName(arg) !== null) return true; // unannotated Any
|
|
95
|
+
if (arg.kind !== '::' || ![1, 2].includes(arg.children.length)) return false;
|
|
96
|
+
const type = strip(arg.children.at(-1));
|
|
97
|
+
return [...builtinTypes].some((t) => m.isUnshadowedBuiltin(type, t));
|
|
98
|
+
});
|
|
99
|
+
if (!foreign) continue;
|
|
100
|
+
context.report({
|
|
101
|
+
node: fn.call,
|
|
102
|
+
messageId: 'diagnostic',
|
|
103
|
+
data: {
|
|
104
|
+
message:
|
|
105
|
+
'This Base/Core generic extension uses only builtin argument types; review possible type piracy.',
|
|
106
|
+
},
|
|
107
|
+
});
|
|
108
|
+
}
|
|
109
|
+
},
|
|
110
|
+
};
|
|
111
|
+
},
|
|
112
|
+
);
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
import { resolve } from 'node:path';
|
|
2
|
+
import { defineRule } from './common.js';
|
|
3
|
+
import { orphan } from '../orphans.js';
|
|
4
|
+
|
|
5
|
+
const virtual = (path) => !path || /^<.*>$/u.test(path);
|
|
6
|
+
const globs = { type: 'array', items: { type: 'string' }, uniqueItems: true };
|
|
7
|
+
|
|
8
|
+
export default defineRule({
|
|
9
|
+
meta: {
|
|
10
|
+
type: 'problem',
|
|
11
|
+
docs: { description: 'Report package files that no entry point includes.' },
|
|
12
|
+
schema: [
|
|
13
|
+
{
|
|
14
|
+
type: 'object',
|
|
15
|
+
properties: { entryPoints: globs, scripts: globs },
|
|
16
|
+
additionalProperties: false,
|
|
17
|
+
},
|
|
18
|
+
],
|
|
19
|
+
messages: {
|
|
20
|
+
orphan:
|
|
21
|
+
'This file is not included from any entry point ({{entries}}); it never runs. Delete it, include it, or add it to entryPoints.',
|
|
22
|
+
},
|
|
23
|
+
},
|
|
24
|
+
create(context) {
|
|
25
|
+
return {
|
|
26
|
+
JuliaFile() {
|
|
27
|
+
const file = context.physicalFilename;
|
|
28
|
+
if (virtual(file)) return;
|
|
29
|
+
const result = orphan(resolve(file), context.options[0] ?? {}, context.sourceCode.text);
|
|
30
|
+
if (!result) return;
|
|
31
|
+
context.report({
|
|
32
|
+
loc: { start: { line: 1, column: 0 }, end: { line: 1, column: 0 } },
|
|
33
|
+
messageId: 'orphan',
|
|
34
|
+
data: { entries: result.entries.join(', ') },
|
|
35
|
+
});
|
|
36
|
+
},
|
|
37
|
+
};
|
|
38
|
+
},
|
|
39
|
+
});
|
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
import {
|
|
2
|
+
checkRule,
|
|
3
|
+
ordinary,
|
|
4
|
+
strip,
|
|
5
|
+
smallInteger,
|
|
6
|
+
statements,
|
|
7
|
+
nodes,
|
|
8
|
+
report,
|
|
9
|
+
} from './correctness-common.js';
|
|
10
|
+
export default checkRule(
|
|
11
|
+
'Report overwritten stores to fresh local integer vectors.',
|
|
12
|
+
(context, m, source) => ({
|
|
13
|
+
block(node) {
|
|
14
|
+
if (!node.interior || !ordinary(node, source)) return;
|
|
15
|
+
// Only an ordinary function's outer block: no branch or exception regions.
|
|
16
|
+
const fn = source.getParent(node);
|
|
17
|
+
if (fn?.kind !== 'function' || fn.children.at(-1) !== node) return;
|
|
18
|
+
const parts = statements(node);
|
|
19
|
+
if (
|
|
20
|
+
[...nodes(node)].some((n) =>
|
|
21
|
+
['macrocall', 'function', '->', 'do', 'try', 'if', 'for', 'while', 'let'].includes(
|
|
22
|
+
n.kind,
|
|
23
|
+
),
|
|
24
|
+
)
|
|
25
|
+
)
|
|
26
|
+
return;
|
|
27
|
+
for (let i = 0; i + 2 < parts.length; i++) {
|
|
28
|
+
const [init, first, second] = parts.slice(i, i + 3);
|
|
29
|
+
if (![init, first, second].every((n) => n.kind === '=' && n.children.length === 2))
|
|
30
|
+
continue;
|
|
31
|
+
const [name, value] = init.children,
|
|
32
|
+
vector = strip(value);
|
|
33
|
+
if (
|
|
34
|
+
vector.kind !== 'vect' ||
|
|
35
|
+
!vector.children.length ||
|
|
36
|
+
vector.children.some((n) => smallInteger(n) === null) ||
|
|
37
|
+
strip(first.children[0]).kind !== 'ref' ||
|
|
38
|
+
strip(second.children[0]).kind !== 'ref'
|
|
39
|
+
)
|
|
40
|
+
continue;
|
|
41
|
+
const binding = m.resolve(name);
|
|
42
|
+
if (!binding || binding.scope.kind !== 'function' || binding.definitions.length !== 1)
|
|
43
|
+
continue;
|
|
44
|
+
// Captures and prior aliases are outside the fresh, uncaptured subset.
|
|
45
|
+
if (
|
|
46
|
+
binding.references.some(
|
|
47
|
+
(r) => r.scope !== binding.scope || r.node.range[0] < init.range[0],
|
|
48
|
+
)
|
|
49
|
+
)
|
|
50
|
+
continue;
|
|
51
|
+
const a = strip(first.children[0]),
|
|
52
|
+
b = strip(second.children[0]);
|
|
53
|
+
if (
|
|
54
|
+
a.kind !== 'ref' ||
|
|
55
|
+
b.kind !== 'ref' ||
|
|
56
|
+
a.children.length !== 2 ||
|
|
57
|
+
b.children.length !== 2 ||
|
|
58
|
+
m.resolve(a.children[0]) !== binding ||
|
|
59
|
+
m.resolve(b.children[0]) !== binding
|
|
60
|
+
)
|
|
61
|
+
continue;
|
|
62
|
+
const index = smallInteger(a.children[1]);
|
|
63
|
+
if (
|
|
64
|
+
index === null ||
|
|
65
|
+
index < 1 ||
|
|
66
|
+
index > vector.children.length ||
|
|
67
|
+
smallInteger(b.children[1]) !== index ||
|
|
68
|
+
smallInteger(first.children[1]) === null ||
|
|
69
|
+
smallInteger(second.children[1]) === null
|
|
70
|
+
)
|
|
71
|
+
continue;
|
|
72
|
+
report(
|
|
73
|
+
context,
|
|
74
|
+
first,
|
|
75
|
+
'This fresh vector element is overwritten immediately before it can be read.',
|
|
76
|
+
);
|
|
77
|
+
}
|
|
78
|
+
},
|
|
79
|
+
}),
|
|
80
|
+
);
|
|
@@ -0,0 +1,107 @@
|
|
|
1
|
+
import { defineRule, isOpaque } from './common.js';
|
|
2
|
+
import { getLocalScopeManager } from '../scope.js';
|
|
3
|
+
|
|
4
|
+
// A generator body and its filters lower to closures; its first iterable does
|
|
5
|
+
// not. ScopeManager deliberately models generator scopes without claiming that
|
|
6
|
+
// all their children execute inside the generated function.
|
|
7
|
+
function boundary(record, binding, source) {
|
|
8
|
+
for (let s = record.scope; s && s !== binding.scope; s = s.parent) {
|
|
9
|
+
if (s.kind === 'module') return null;
|
|
10
|
+
if (s.kind === 'function') return s.node.kind === 'macro' ? null : s.node;
|
|
11
|
+
const typedLoop =
|
|
12
|
+
s.kind === 'comprehension' &&
|
|
13
|
+
source.getParent(s.node)?.kind === 'typed_comprehension' &&
|
|
14
|
+
s.node.children.length === 2 &&
|
|
15
|
+
s.node.children[1].kind === 'iteration';
|
|
16
|
+
if (s.kind === 'comprehension' && !typedLoop) {
|
|
17
|
+
let child = record.node;
|
|
18
|
+
for (let p = source.getParent(child); p && p !== s.node; p = source.getParent(child))
|
|
19
|
+
child = p;
|
|
20
|
+
if (child === s.node.children[0]) return s.node;
|
|
21
|
+
for (let p = record.node; p && p !== s.node; p = source.getParent(p)) {
|
|
22
|
+
const parent = source.getParent(p);
|
|
23
|
+
if (parent?.kind === 'filter' && parent.children[0] !== p) return s.node;
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
return null;
|
|
28
|
+
}
|
|
29
|
+
function straightBefore(write, capture, source) {
|
|
30
|
+
if (write.assignment.range[1] > capture.range[0]) return false;
|
|
31
|
+
// Julia 1.13 can move one unconditional argument/iteration-variable assignment
|
|
32
|
+
// into an unboxed local when it dominates every capture in its branch.
|
|
33
|
+
let loop = write.binding.kind === 'iteration' ? write.binding.scope.node : null;
|
|
34
|
+
while (loop && !['for', 'generator'].includes(loop.kind)) loop = source.getParent(loop);
|
|
35
|
+
for (let p = source.getParent(write.assignment); p; p = source.getParent(p)) {
|
|
36
|
+
if (p === loop) return true;
|
|
37
|
+
if (
|
|
38
|
+
['block', 'parens'].includes(p.kind) &&
|
|
39
|
+
p.range[0] <= capture.range[0] &&
|
|
40
|
+
p.range[1] >= capture.range[1]
|
|
41
|
+
)
|
|
42
|
+
return true;
|
|
43
|
+
if (p === capture) return false;
|
|
44
|
+
if (['function', '->', 'do'].includes(p.kind)) return true;
|
|
45
|
+
if (!['block', 'parens', 'local', '::'].includes(p.kind)) return false;
|
|
46
|
+
}
|
|
47
|
+
return false;
|
|
48
|
+
}
|
|
49
|
+
export default defineRule({
|
|
50
|
+
meta: {
|
|
51
|
+
type: 'suggestion',
|
|
52
|
+
docs: { description: 'Review reassigned local captures that require boxing during lowering.' },
|
|
53
|
+
schema: [],
|
|
54
|
+
messages: {
|
|
55
|
+
boxed:
|
|
56
|
+
'Captured local "{{name}}" is reassigned and requires a box during lowering. Review this capture on performance-sensitive paths.',
|
|
57
|
+
},
|
|
58
|
+
},
|
|
59
|
+
create(context) {
|
|
60
|
+
const source = context.sourceCode;
|
|
61
|
+
return {
|
|
62
|
+
JuliaFile() {
|
|
63
|
+
const manager = getLocalScopeManager(source),
|
|
64
|
+
seen = new Set();
|
|
65
|
+
for (const scope of manager.scopes)
|
|
66
|
+
for (const binding of scope.bindings.values()) {
|
|
67
|
+
if (seen.has(binding)) continue;
|
|
68
|
+
seen.add(binding);
|
|
69
|
+
if (
|
|
70
|
+
!binding.scope ||
|
|
71
|
+
binding.scope.kind === 'module' ||
|
|
72
|
+
binding.constant ||
|
|
73
|
+
['function', 'macro', 'type', 'type-parameter', 'import'].includes(binding.kind)
|
|
74
|
+
)
|
|
75
|
+
continue;
|
|
76
|
+
// This is a source-level subset, not a macro expansion/lowering emulator.
|
|
77
|
+
const records = [...binding.definitions, ...binding.references];
|
|
78
|
+
if (records.some((r) => r.opaque || isOpaque(r.node, source))) continue;
|
|
79
|
+
const captures = records
|
|
80
|
+
.map((r) => ({ record: r, closure: boundary(r, binding, source) }))
|
|
81
|
+
.filter((c) => c.closure)
|
|
82
|
+
.sort((a, b) => a.record.node.range[0] - b.record.node.range[0]);
|
|
83
|
+
if (!captures.length) continue;
|
|
84
|
+
const writes = binding.writes.filter((w) => w.assignment);
|
|
85
|
+
const capturedWrite = writes.some((w) => boundary(w, binding, source));
|
|
86
|
+
const letInitializer = binding.definitions.some(
|
|
87
|
+
(d) => d.kind === 'let' && d.assignment?.kind === '=',
|
|
88
|
+
);
|
|
89
|
+
const count = writes.length + Number(letInitializer);
|
|
90
|
+
let boxed = capturedWrite || count > 1;
|
|
91
|
+
if (
|
|
92
|
+
!boxed &&
|
|
93
|
+
writes.length === 1 &&
|
|
94
|
+
['argument', 'keyword', 'iteration'].includes(binding.kind)
|
|
95
|
+
)
|
|
96
|
+
boxed = captures.some((c) => !straightBefore(writes[0], c.closure, source));
|
|
97
|
+
if (boxed)
|
|
98
|
+
context.report({
|
|
99
|
+
node: captures[0].record.node,
|
|
100
|
+
messageId: 'boxed',
|
|
101
|
+
data: { name: binding.name },
|
|
102
|
+
});
|
|
103
|
+
}
|
|
104
|
+
},
|
|
105
|
+
};
|
|
106
|
+
},
|
|
107
|
+
});
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
import { check, ordinary, strip, stableType, span, text } from './semantic-common.js';
|
|
2
|
+
export default check(
|
|
3
|
+
'Remove repeated nested expression type assertions.',
|
|
4
|
+
(context, m, source) => ({
|
|
5
|
+
'[kind="::"]'(node) {
|
|
6
|
+
if (node.children.length !== 2 || !ordinary(node, source)) return;
|
|
7
|
+
// A declaration/assignment target has conversion semantics, unlike an expression.
|
|
8
|
+
for (let at = node, p = source.getParent(at); p; at = p, p = source.getParent(p)) {
|
|
9
|
+
if (
|
|
10
|
+
(p.kind === '=' && p.children[0] === at) ||
|
|
11
|
+
['local', 'global', 'const', 'struct'].includes(p.kind)
|
|
12
|
+
)
|
|
13
|
+
return;
|
|
14
|
+
if (!['parens', '::', 'tuple'].includes(p.kind)) break;
|
|
15
|
+
}
|
|
16
|
+
const inner = strip(node.children[0]);
|
|
17
|
+
if (inner?.kind !== '::' || inner.children.length !== 2) return;
|
|
18
|
+
const type = stableType(node.children[1], m);
|
|
19
|
+
if (!type || type !== stableType(inner.children[1], m)) return;
|
|
20
|
+
const range = [span(source, node.children[0])[1], span(source, node)[1]];
|
|
21
|
+
const comments = source.comments.some(
|
|
22
|
+
(c) => c.range[0] >= range[0] && c.range[1] <= range[1],
|
|
23
|
+
);
|
|
24
|
+
context.report({
|
|
25
|
+
node,
|
|
26
|
+
messageId: 'diagnostic',
|
|
27
|
+
data: { message: 'The outer expression assertion repeats the same stable type.' },
|
|
28
|
+
...(!comments && {
|
|
29
|
+
fix: (fixer) =>
|
|
30
|
+
fixer.replaceTextRange(span(source, node), text(source, node.children[0])),
|
|
31
|
+
}),
|
|
32
|
+
});
|
|
33
|
+
},
|
|
34
|
+
}),
|
|
35
|
+
{ fixable: 'code' },
|
|
36
|
+
);
|