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.
Files changed (115) hide show
  1. package/CHANGELOG.md +159 -0
  2. package/LICENSE +21 -0
  3. package/README.md +213 -2
  4. package/data/base-core-exports.json +5 -0
  5. package/data/public-only.json +35 -0
  6. package/data/stdlib-exports.json +180 -0
  7. package/package.json +76 -4
  8. package/src/index.js +226 -0
  9. package/src/language.js +46 -0
  10. package/src/macro-signatures.js +277 -0
  11. package/src/module-resolution.js +1187 -0
  12. package/src/orphans.js +631 -0
  13. package/src/parser/flags.js +25 -0
  14. package/src/parser/green-tree.js +41 -0
  15. package/src/parser/positions.js +29 -0
  16. package/src/parser/syntax-tree.js +91 -0
  17. package/src/parser/wasm-host.js +92 -0
  18. package/src/resolver/index.js +232 -0
  19. package/src/resolver/pkg-child.mjs +165 -0
  20. package/src/resolver/toml.js +63 -0
  21. package/src/resolver/warm.mjs +26 -0
  22. package/src/rules/common.js +52 -0
  23. package/src/rules/correctness-common.js +90 -0
  24. package/src/rules/growth-common.js +93 -0
  25. package/src/rules/idiom-common.js +116 -0
  26. package/src/rules/no-collapsible-if.js +38 -0
  27. package/src/rules/no-collect-before-range-iteration.js +24 -0
  28. package/src/rules/no-compare-negative-zero.js +50 -0
  29. package/src/rules/no-concat-growth-in-loop.js +18 -0
  30. package/src/rules/no-cond-assign.js +126 -0
  31. package/src/rules/no-const-reassign.js +61 -0
  32. package/src/rules/no-constant-condition.js +59 -0
  33. package/src/rules/no-debugger.js +96 -0
  34. package/src/rules/no-discarded-copy-result.js +50 -0
  35. package/src/rules/no-dupe-args.js +25 -0
  36. package/src/rules/no-dupe-elseif.js +57 -0
  37. package/src/rules/no-duplicate-dict-keys.js +95 -0
  38. package/src/rules/no-duplicate-keyword-arguments.js +40 -0
  39. package/src/rules/no-duplicate-logical-operands.js +85 -0
  40. package/src/rules/no-duplicate-method-signatures.js +90 -0
  41. package/src/rules/no-duplicate-struct-fields.js +38 -0
  42. package/src/rules/no-empty-block.js +49 -0
  43. package/src/rules/no-empty-catch.js +32 -0
  44. package/src/rules/no-empty-literal-range.js +26 -0
  45. package/src/rules/no-env-mutation.js +62 -0
  46. package/src/rules/no-eval-local-symbol.js +85 -0
  47. package/src/rules/no-fill-with-mutable.js +43 -0
  48. package/src/rules/no-full-slice-in-consumer.js +41 -0
  49. package/src/rules/no-identical-branches.js +44 -0
  50. package/src/rules/no-import-assign.js +64 -0
  51. package/src/rules/no-impossible-length-comparison.js +49 -0
  52. package/src/rules/no-invalid-foreign-call-signature.js +58 -0
  53. package/src/rules/no-isdefined-local-call.js +23 -0
  54. package/src/rules/no-loss-of-float-integer-precision.js +41 -0
  55. package/src/rules/no-nothing-return-conversion.js +127 -0
  56. package/src/rules/no-obvious-type-piracy.js +112 -0
  57. package/src/rules/no-orphan-file.js +39 -0
  58. package/src/rules/no-overwritten-literal-index.js +80 -0
  59. package/src/rules/no-reassigned-capture.js +107 -0
  60. package/src/rules/no-redundant-type-assertion.js +36 -0
  61. package/src/rules/no-redundant-union-members.js +42 -0
  62. package/src/rules/no-restricted-calls.js +74 -0
  63. package/src/rules/no-restricted-imports.js +105 -0
  64. package/src/rules/no-restricted-macros.js +61 -0
  65. package/src/rules/no-self-assign.js +65 -0
  66. package/src/rules/no-self-identity-comparison.js +37 -0
  67. package/src/rules/no-shadowed-loop-var.js +83 -0
  68. package/src/rules/no-show-macro.js +90 -0
  69. package/src/rules/no-single-iteration-loop.js +50 -0
  70. package/src/rules/no-string-growth-in-loop.js +18 -0
  71. package/src/rules/no-throw-literal.js +33 -0
  72. package/src/rules/no-unbound-type-parameters.js +91 -0
  73. package/src/rules/no-undef.js +284 -0
  74. package/src/rules/no-undefined-exports.js +60 -0
  75. package/src/rules/no-uninitialized-ref-read.js +38 -0
  76. package/src/rules/no-unreachable.js +82 -0
  77. package/src/rules/no-unsafe-finally.js +35 -0
  78. package/src/rules/no-unsafe-negation.js +77 -0
  79. package/src/rules/no-unused-expressions.js +137 -0
  80. package/src/rules/no-unused-type-parameters.js +55 -0
  81. package/src/rules/no-unused-vars.js +123 -0
  82. package/src/rules/no-use-before-define.js +139 -0
  83. package/src/rules/no-useless-assignment.js +75 -0
  84. package/src/rules/no-useless-catch.js +28 -0
  85. package/src/rules/parse-errors.js +19 -0
  86. package/src/rules/performance-common.js +119 -0
  87. package/src/rules/prefer-any.js +75 -0
  88. package/src/rules/prefer-const-global.js +103 -0
  89. package/src/rules/prefer-count-over-findall-length.js +46 -0
  90. package/src/rules/prefer-eachindex.js +120 -0
  91. package/src/rules/prefer-extreme-over-sort.js +73 -0
  92. package/src/rules/prefer-generator-sum.js +34 -0
  93. package/src/rules/prefer-identity-nothing.js +43 -0
  94. package/src/rules/prefer-in.js +67 -0
  95. package/src/rules/prefer-isa.js +70 -0
  96. package/src/rules/prefer-isempty.js +57 -0
  97. package/src/rules/prefer-ismissing.js +42 -0
  98. package/src/rules/prefer-parenthesized-interpolation.js +160 -0
  99. package/src/rules/prefer-size-dimension.js +19 -0
  100. package/src/rules/prefer-sized-push-buffer.js +81 -0
  101. package/src/rules/prefer-typed-empty-array.js +90 -0
  102. package/src/rules/require-typed-global.js +60 -0
  103. package/src/rules/restricted-common.js +211 -0
  104. package/src/rules/semantic-common.js +105 -0
  105. package/src/rules/use-isnan.js +28 -0
  106. package/src/scope.js +1338 -0
  107. package/src/source-code.js +100 -0
  108. package/types/ast.d.ts +49 -0
  109. package/types/index.d.ts +65 -0
  110. package/types/scope.d.ts +71 -0
  111. package/wasm/LICENSES/Julia.md +26 -0
  112. package/wasm/LICENSES/JuliaSyntax-MIT.md +46 -0
  113. package/wasm/LICENSES/WasmCodegen-MIT.txt +21 -0
  114. package/wasm/manifest.json +44 -0
  115. package/wasm/parser.wasm +0 -0
@@ -0,0 +1,85 @@
1
+ import { defineRule, isBareName, isOpaque, normalizedTreeKey } from './common.js';
2
+ function unwrap(n) {
3
+ while (n.kind === 'parens' && n.children.length === 1) n = n.children[0];
4
+ return n;
5
+ }
6
+ function conditionContext(node, source) {
7
+ for (let child = node, p = source.getParent(child); p; child = p, p = source.getParent(p)) {
8
+ if (['parens', '&&', '||'].includes(p.kind)) continue;
9
+ return ['if', 'elseif', 'while', '?'].includes(p.kind) && p.children[0] === child;
10
+ }
11
+ return false;
12
+ }
13
+ export default defineRule({
14
+ meta: {
15
+ type: 'problem',
16
+ hasSuggestions: true,
17
+ schema: [],
18
+ docs: {
19
+ description: 'Report repeated bare names in a logical chain without intervening effects.',
20
+ },
21
+ messages: {
22
+ issue: 'Repeated logical operand.',
23
+ remove: 'Remove the repeated operand in this Boolean condition.',
24
+ },
25
+ },
26
+ create(context) {
27
+ const source = context.sourceCode;
28
+ function check(node) {
29
+ if (!node.interior || isOpaque(node, source)) return;
30
+ let p = source.getParent(node);
31
+ while (p?.kind === 'parens') p = source.getParent(p);
32
+ if (p?.kind === node.kind) return;
33
+ const operands = [],
34
+ stack = [node];
35
+ while (stack.length) {
36
+ const n = unwrap(stack.pop());
37
+ if (n.kind === node.kind && n.interior) stack.push(...n.children.toReversed());
38
+ else operands.push(n);
39
+ }
40
+ const seen = new Set();
41
+ for (let i = 0; i < operands.length; i++) {
42
+ const n = operands[i];
43
+ if (!isBareName(n)) {
44
+ seen.clear();
45
+ continue;
46
+ }
47
+ const key = normalizedTreeKey(n);
48
+ if (seen.has(key)) {
49
+ const canSuggest =
50
+ conditionContext(node, source) &&
51
+ !source.comments.some(
52
+ (c) => c.range[0] >= node.range[0] && c.range[1] <= node.range[1],
53
+ );
54
+ const replacement = operands
55
+ .filter((_, j) => j !== i)
56
+ .map((o) => `(${source.getText(o)})`)
57
+ .join(` ${node.kind} `);
58
+ context.report({
59
+ node: n,
60
+ messageId: 'issue',
61
+ ...(canSuggest && {
62
+ suggest: [
63
+ {
64
+ messageId: 'remove',
65
+ fix: (fixer) =>
66
+ fixer.replaceTextRange(
67
+ [
68
+ source.tokens.find(
69
+ (t) => t.range[0] >= node.range[0] && t.range[1] <= node.range[1],
70
+ ).range[0],
71
+ node.range[1],
72
+ ],
73
+ replacement,
74
+ ),
75
+ },
76
+ ],
77
+ }),
78
+ });
79
+ }
80
+ seen.add(key);
81
+ }
82
+ }
83
+ return { '[kind="&&"]': check, '[kind="||"]': check };
84
+ },
85
+ });
@@ -0,0 +1,90 @@
1
+ import { rule, ordinary, identifierName, strip, suggest } from './idiom-common.js';
2
+ export default rule(
3
+ 'Report later identical supported positional method signatures.',
4
+ { allowRedefinition: { type: 'boolean' } },
5
+ (context, m, source, options) => ({
6
+ JuliaFile() {
7
+ if (options.allowRedefinition) return;
8
+ const seen = new Map(),
9
+ identities = new Map();
10
+ let serial = 0;
11
+ const id = (b) => {
12
+ if (!identities.has(b)) identities.set(b, ++serial);
13
+ return identities.get(b);
14
+ };
15
+ for (const fn of m.functions) {
16
+ if (
17
+ fn.node.kind !== 'function' ||
18
+ !fn.call ||
19
+ fn.typeParameters.length ||
20
+ !ordinary(fn.node, source)
21
+ )
22
+ continue;
23
+ const head = strip(fn.call.children[0]),
24
+ binding = m.resolve(head);
25
+ if (
26
+ identifierName(head) === null ||
27
+ !binding ||
28
+ binding.scope.kind !== 'module' ||
29
+ binding.definitions.some((d) => d.kind === 'type')
30
+ )
31
+ continue;
32
+ let unconditional = true;
33
+ for (
34
+ let at = source.getParent(fn.node);
35
+ at && at !== binding.scope.node;
36
+ at = source.getParent(at)
37
+ )
38
+ if (!['block', 'toplevel', 'JuliaFile'].includes(at.kind)) unconditional = false;
39
+ if (!unconditional) continue;
40
+ const types = [];
41
+ let supported = true;
42
+ for (const arg of fn.call.children.slice(1).filter((n) => n.kind !== 'parameters')) {
43
+ if (identifierName(arg) !== null) {
44
+ types.push('Any');
45
+ continue;
46
+ }
47
+ if (arg.kind !== '::') {
48
+ supported = false;
49
+ break;
50
+ }
51
+ const type = strip(arg.children.at(-1)),
52
+ typeBinding = m.resolve(type);
53
+ let builtinName = typeBinding?.kind === 'builtin' ? typeBinding.name : null;
54
+ if (type.kind === '.' && m.isUnshadowedBuiltin(type, identifierName(type.children[1])))
55
+ builtinName = identifierName(type.children[1]);
56
+ if (!builtinName && typeBinding)
57
+ for (const d of typeBinding.definitions)
58
+ if (d.kind === 'import' && ['Base', 'Core'].includes(d.moduleName)) {
59
+ const original = d.imported
60
+ .split(/\s+as\s+/u)[0]
61
+ .split('.')
62
+ .at(-1)
63
+ .trim();
64
+ if (m.isUnshadowedBuiltin(type, original)) builtinName = original;
65
+ }
66
+ if (builtinName) types.push(builtinName === 'Any' ? 'Any' : `builtin:${builtinName}`);
67
+ else if (
68
+ identifierName(type) !== null &&
69
+ typeBinding?.definitions.some((d) => d.kind === 'type')
70
+ )
71
+ types.push(`type:${id(typeBinding)}`);
72
+ else {
73
+ supported = false;
74
+ break;
75
+ }
76
+ }
77
+ if (!supported) continue;
78
+ const key = `${id(binding)}:${types.join(',')}`;
79
+ if (seen.has(key))
80
+ suggest(
81
+ context,
82
+ fn.call,
83
+ 'This positional method signature repeats an earlier method in the same module; keywords do not distinguish dispatch.',
84
+ );
85
+ seen.set(key, fn);
86
+ }
87
+ },
88
+ }),
89
+ false,
90
+ );
@@ -0,0 +1,38 @@
1
+ import { defineRule, isBareName, isOpaque } from './common.js';
2
+ function fieldName(node) {
3
+ if (node.kind === 'const' && node.interior && node.children.length === 1) node = node.children[0];
4
+ if (isBareName(node)) return node;
5
+ if (
6
+ node.kind === '::' &&
7
+ node.interior &&
8
+ node.children.length === 2 &&
9
+ isBareName(node.children[0])
10
+ )
11
+ return node.children[0];
12
+ return null;
13
+ }
14
+ export default defineRule({
15
+ meta: {
16
+ type: 'problem',
17
+ schema: [],
18
+ docs: { description: 'Disallow repeated direct struct field names.' },
19
+ messages: { duplicate: 'Duplicate struct field "{{name}}".' },
20
+ },
21
+ create(context) {
22
+ return {
23
+ struct(node) {
24
+ if (!node.interior || isOpaque(node, context.sourceCode)) return;
25
+ const body = node.children.at(-1);
26
+ if (body?.kind !== 'block' || !body.interior) return;
27
+ const seen = new Set();
28
+ for (const declaration of body.children) {
29
+ const name = fieldName(declaration);
30
+ if (!name) continue;
31
+ if (seen.has(name.value))
32
+ context.report({ node: name, messageId: 'duplicate', data: { name: name.value } });
33
+ seen.add(name.value);
34
+ }
35
+ },
36
+ };
37
+ },
38
+ });
@@ -0,0 +1,49 @@
1
+ import { defineRule, isOpaque } from './common.js';
2
+ function empty(body) {
3
+ const stack = [body];
4
+ while (stack.length) {
5
+ const n = stack.pop();
6
+ if (!n.interior || !['block', 'toplevel'].includes(n.kind)) return false;
7
+ stack.push(...n.children);
8
+ }
9
+ return true;
10
+ }
11
+ export default defineRule({
12
+ meta: {
13
+ type: 'suggestion',
14
+ schema: [
15
+ {
16
+ type: 'object',
17
+ properties: { allowComment: { type: 'boolean' }, allowEmptyWhile: { type: 'boolean' } },
18
+ additionalProperties: false,
19
+ },
20
+ ],
21
+ docs: { description: 'Report empty executable control-flow and let bodies.' },
22
+ messages: { issue: 'Empty {{kind}} body.' },
23
+ },
24
+ create(context) {
25
+ const source = context.sourceCode,
26
+ options = context.options[0] ?? {};
27
+ function check(node) {
28
+ if (
29
+ !node.interior ||
30
+ isOpaque(node, source) ||
31
+ (node.kind === 'while' && options.allowEmptyWhile)
32
+ )
33
+ return;
34
+ const bodies = ['if', 'elseif'].includes(node.kind)
35
+ ? node.children.slice(1).filter((n) => n.kind === 'block')
36
+ : [node.children.at(-1)];
37
+ for (const body of bodies) {
38
+ if (body?.kind !== 'block' || !empty(body)) continue;
39
+ if (
40
+ (options.allowComment ?? true) &&
41
+ source.comments.some((c) => c.range[0] >= body.range[0] && c.range[1] <= body.range[1])
42
+ )
43
+ continue;
44
+ context.report({ node: body, messageId: 'issue', data: { kind: node.kind } });
45
+ }
46
+ }
47
+ return { if: check, elseif: check, for: check, while: check, let: check };
48
+ },
49
+ });
@@ -0,0 +1,32 @@
1
+ import { defineRule, isOpaque } from './common.js';
2
+ export default defineRule({
3
+ meta: {
4
+ type: 'suggestion',
5
+ docs: { description: 'Disallow empty catch blocks.' },
6
+ schema: [
7
+ {
8
+ type: 'object',
9
+ properties: { allowComment: { type: 'boolean' } },
10
+ additionalProperties: false,
11
+ },
12
+ ],
13
+ messages: { empty: 'Empty catch block.' },
14
+ },
15
+ create(context) {
16
+ return {
17
+ catch(node) {
18
+ if (!node.interior || isOpaque(node, context.sourceCode)) return;
19
+ const body = node.children.at(-1);
20
+ if (body?.kind !== 'block' || !body.interior || body.children.length) return;
21
+ if (
22
+ context.options[0]?.allowComment &&
23
+ context.sourceCode.comments.some(
24
+ (c) => c.range[0] >= body.range[0] && c.range[1] <= body.range[1],
25
+ )
26
+ )
27
+ return;
28
+ context.report({ node, messageId: 'empty' });
29
+ },
30
+ };
31
+ },
32
+ });
@@ -0,0 +1,26 @@
1
+ import { checkRule, ordinary, strip, smallInteger, report } from './correctness-common.js';
2
+ import { isInfixCall } from '../parser/flags.js';
3
+ export default checkRule(
4
+ 'Report literal ranges that cannot iterate toward their endpoint.',
5
+ (context, m, source) => ({
6
+ iteration(node) {
7
+ if (!node.interior || !ordinary(node, source)) return;
8
+ for (const clause of node.children) {
9
+ const range = strip(clause.children[1]);
10
+ if (!range || !isInfixCall(range) || ![3, 4].includes(range.children.length)) continue;
11
+ const values = [range.children[0], ...range.children.slice(2)].map(smallInteger);
12
+ if (values.some((v) => v === null)) continue;
13
+ const [start, step, end] = values.length === 2 ? [values[0], 1, values[1]] : values;
14
+ if (
15
+ ((step > 0 && start > end) || (step < 0 && start < end)) &&
16
+ m.isUnshadowedBuiltin(range.children[1], ':')
17
+ )
18
+ report(
19
+ context,
20
+ range,
21
+ 'This literal range is empty: its step points away from the endpoint.',
22
+ );
23
+ }
24
+ },
25
+ }),
26
+ );
@@ -0,0 +1,62 @@
1
+ import { defineRule } from './common.js';
2
+ import { getScopeManager, identifierName } from '../scope.js';
3
+ import { ordinary, strip } from './idiom-common.js';
4
+ import { isInfixCall } from '../parser/flags.js';
5
+
6
+ const mutators = ['delete!', 'pop!', 'get!', 'push!', 'empty!', 'merge!', 'setindex!'];
7
+ export default defineRule({
8
+ meta: {
9
+ type: 'suggestion',
10
+ docs: { description: 'Review direct writes to the process environment.' },
11
+ schema: [
12
+ {
13
+ type: 'object',
14
+ additionalProperties: false,
15
+ properties: { allowInInit: { type: 'boolean' } },
16
+ },
17
+ ],
18
+ defaultOptions: [{ allowInInit: false }],
19
+ messages: {
20
+ mutation: 'This writes to the process environment; prefer scoped withenv where possible.',
21
+ },
22
+ },
23
+ create(context) {
24
+ const source = context.sourceCode,
25
+ m = getScopeManager(source, context);
26
+ const functions = new Map(m.functions.map((fn) => [fn.scope, fn]));
27
+ function exempt(node) {
28
+ if (!context.options[0]?.allowInInit) return false;
29
+ for (let s = m.scopeFor(node); s && s.kind !== 'module'; s = s.parent) {
30
+ if (s.kind !== 'function') continue;
31
+ const fn = functions.get(s);
32
+ return (
33
+ s.parent.kind === 'module' &&
34
+ fn?.call?.children.length === 1 &&
35
+ identifierName(fn.call.children[0]) === '__init__'
36
+ );
37
+ }
38
+ return false;
39
+ }
40
+ const env = (n) => m.isUnshadowedBuiltin(strip(n), 'ENV');
41
+ function report(node) {
42
+ if (ordinary(node, source) && !exempt(node)) context.report({ node, messageId: 'mutation' });
43
+ }
44
+ function assignment(node) {
45
+ const target = strip(node.children[0]);
46
+ if (target?.kind === 'ref' && env(target.children[0])) report(node);
47
+ }
48
+ return {
49
+ '*': (node) => {
50
+ if (['=', 'op=', '.='].includes(node.kind)) assignment(node);
51
+ },
52
+ call(node) {
53
+ if (!ordinary(node, source) || isInfixCall(node)) return;
54
+ const name = mutators.find((name) => m.isUnshadowedBuiltin(strip(node.children[0]), name));
55
+ if (!name) return;
56
+ const args = node.children.slice(1);
57
+ // get!(default_function, dictionary, key) is also used by do syntax.
58
+ if (env(args[0]) || (name === 'get!' && args.length >= 3 && env(args[1]))) report(node);
59
+ },
60
+ };
61
+ },
62
+ });
@@ -0,0 +1,85 @@
1
+ import {
2
+ checkRule,
3
+ ordinary,
4
+ strip,
5
+ localSymbol,
6
+ nodes,
7
+ identifierName,
8
+ report,
9
+ } from './correctness-common.js';
10
+ export default checkRule(
11
+ 'Report eval of a local symbol without an established module global.',
12
+ (context, m, source) => ({
13
+ call(node) {
14
+ if (!ordinary(node, source)) return;
15
+ const head = strip(node.children[0]);
16
+ const name = identifierName(head?.kind === '.' ? head.children[1] : head);
17
+ if (name !== 'eval') return;
18
+ const symbol = node.children.at(-1);
19
+ if (
20
+ symbol?.kind !== 'quote' ||
21
+ symbol.children.length !== 1 ||
22
+ symbol.children[0].kind !== 'Identifier'
23
+ )
24
+ return;
25
+ const scope = m.scopeFor(node),
26
+ module = scope?.module;
27
+ if (!module || module.bare) return;
28
+ const args = node.children.slice(1);
29
+ const ownEval = identifierName(head) === 'eval' && m.isUnshadowedBuiltin(head, 'eval');
30
+ const coreEval =
31
+ head.kind === '.' &&
32
+ m.isUnshadowedBuiltin(head.children[0], 'Core') &&
33
+ args.length === 2 &&
34
+ args[0].kind === 'macrocall' &&
35
+ args[0].children.length === 1 &&
36
+ identifierName(args[0].children[0]) === '@__MODULE__' &&
37
+ m.lookup('@__MODULE__', scope)?.kind === 'builtin';
38
+ if (!(ownEval && args.length === 1) && !coreEval) return;
39
+ const binding = localSymbol(args.at(-1), node, m);
40
+ if (!binding || m.lookup(binding.name, module)) return;
41
+ const models = m.moduleResolution?.scopes.get(module);
42
+ if (
43
+ !models?.length ||
44
+ models.some(
45
+ (model) => model.unknown.size || model.unknownExports || model.unresolvedIncludes,
46
+ )
47
+ )
48
+ return;
49
+ // The resolver marks this call itself as dynamic evaluation. Inspect the visible
50
+ // source instead, rejecting every other dynamic writer and every include.
51
+ // Included files cannot establish absence of a future dynamic global reliably.
52
+ for (const n of nodes(module.node)) {
53
+ if (n === node) continue;
54
+ if (['macrocall', 'include', 'global'].includes(n.kind)) {
55
+ if (coreEval && n === args[0]) continue;
56
+ return;
57
+ }
58
+ if (n.kind !== 'call') continue;
59
+ const h = strip(n.children[0]),
60
+ spelling = identifierName(h?.kind === '.' ? h.children[1] : h);
61
+ if (
62
+ [
63
+ 'eval',
64
+ 'include',
65
+ 'include_string',
66
+ 'setglobal!',
67
+ 'setglobalonce!',
68
+ 'modifyglobal!',
69
+ 'swapglobal!',
70
+ 'replaceglobal!',
71
+ ].includes(spelling)
72
+ )
73
+ return;
74
+ }
75
+ // Reject globals introduced through any include, even one with known exports.
76
+ if (models.some((model) => model.dynamicEvaluation && m.moduleResolution.files.length > 1))
77
+ return;
78
+ report(
79
+ context,
80
+ node,
81
+ `eval looks up module globals, not the local binding "${binding.name}".`,
82
+ );
83
+ },
84
+ }),
85
+ );
@@ -0,0 +1,43 @@
1
+ import { rule, ordinary, call, strip, integer, suggest } from './idiom-common.js';
2
+ export default rule(
3
+ 'Warn when fill shares one fresh mutable object across slots.',
4
+ {
5
+ unknownDimensions: { type: 'boolean' },
6
+ },
7
+ (context, m, source, options) => ({
8
+ call(node) {
9
+ const args = call(node, 'fill', m);
10
+ if (
11
+ !args ||
12
+ args.length < 2 ||
13
+ !ordinary(node, source) ||
14
+ args.some((n) => ['...', 'parameters'].includes(n.kind))
15
+ )
16
+ return;
17
+ const value = strip(args[0]);
18
+ const fresh =
19
+ ['vect', 'hcat', 'vcat', 'typed_vcat', 'typed_hcat'].includes(value.kind) ||
20
+ (value.kind === 'ref' &&
21
+ ['Int', 'Float64', 'Any', 'String'].some((n) =>
22
+ m.isUnshadowedBuiltin(value.children[0], n),
23
+ )) ||
24
+ ['Dict', 'Ref', 'zeros', 'ones'].some((name) => call(value, name, m));
25
+ if (!fresh) return;
26
+ const dims =
27
+ args.length === 2 && strip(args[1]).kind === 'tuple'
28
+ ? strip(args[1]).children
29
+ : args.slice(1);
30
+ const sizes = dims.map(integer);
31
+ if (sizes.some((n) => n !== null && n <= 0n)) return;
32
+ if (sizes.some((n) => n === null)) {
33
+ if (options.unknownDimensions === false) return;
34
+ } else if (!sizes.length || sizes.every((n) => n === 1n)) return;
35
+ suggest(
36
+ context,
37
+ node,
38
+ 'fill stores the same mutable object in every slot; use independent allocations if sharing is unintended.',
39
+ );
40
+ },
41
+ }),
42
+ false,
43
+ );
@@ -0,0 +1,41 @@
1
+ import { rule, ordinary, call, strip, text, suggest } from './idiom-common.js';
2
+ import { initializedLocal, arrayLiteral } from './performance-common.js';
3
+ export default rule(
4
+ 'Review a full local Array copy immediately consumed by length, sum or a loop.',
5
+ {},
6
+ (context, m, source) => ({
7
+ ref(node) {
8
+ if (!ordinary(node, source) || node.children.length !== 2 || node.children[1].kind !== ':')
9
+ return;
10
+ const array = node.children[0],
11
+ init = initializedLocal(array, m, source);
12
+ if (!init || !arrayLiteral(init, m)) return;
13
+ let at = node,
14
+ parent = source.getParent(at);
15
+ while (parent?.kind === 'parens') {
16
+ at = parent;
17
+ parent = source.getParent(at);
18
+ }
19
+ const length = call(parent, 'length', m),
20
+ sum = call(parent, 'sum', m);
21
+ const isLength = length?.length === 1 && strip(length[0]) === node;
22
+ const isSum = sum?.length === 1 && strip(sum[0]) === node;
23
+ const clause = source.getParent(parent),
24
+ loop = source.getParent(clause);
25
+ const iteration =
26
+ ['in', '='].includes(parent?.kind) &&
27
+ strip(parent.children[1]) === node &&
28
+ clause?.kind === 'iteration' &&
29
+ loop?.kind === 'for';
30
+ if (!isLength && !isSum && !iteration) return;
31
+ suggest(
32
+ context,
33
+ node,
34
+ isLength
35
+ ? 'This full Array copy is used only for length; consider querying the original array.'
36
+ : 'This consumer immediately uses a full Array copy; review snapshot, mutation, shape and reduction semantics before removing it.',
37
+ isLength ? text(source, array) : null,
38
+ );
39
+ },
40
+ }),
41
+ );
@@ -0,0 +1,44 @@
1
+ import { defineRule, isOpaque, normalizedTreeKey } from './common.js';
2
+ export default defineRule({
3
+ meta: {
4
+ type: 'problem',
5
+ schema: [],
6
+ docs: { description: 'Report identical nonempty conditional branches.' },
7
+ messages: { issue: 'Conditional branch repeats the body at line {{line}}.' },
8
+ },
9
+ create(context) {
10
+ function check(node) {
11
+ if (!node.interior || isOpaque(node, context.sourceCode)) return;
12
+ const bodies = [];
13
+ if (node.kind === '?') bodies.push(...node.children.slice(1));
14
+ else
15
+ for (let branch = node; branch;) {
16
+ bodies.push(branch.children[1]);
17
+ const tail = branch.children[2];
18
+ if (tail?.kind === 'elseif') branch = tail;
19
+ else {
20
+ if (tail) bodies.push(tail);
21
+ break;
22
+ }
23
+ }
24
+ const nonempty = (n) => n && (n.kind !== 'block' || n.children.length > 0);
25
+ const keys = bodies.map((n) => (nonempty(n) ? normalizedTreeKey(n) : null));
26
+ if (keys.length > 1 && keys[0] !== null && keys.every((k) => k === keys[0])) {
27
+ context.report({ node, messageId: 'issue', data: { line: bodies[0].loc.start.line } });
28
+ return;
29
+ }
30
+ const seen = new Map();
31
+ for (let i = 0; i < bodies.length; i++) {
32
+ if (keys[i] === null) continue;
33
+ if (seen.has(keys[i]))
34
+ context.report({
35
+ node: bodies[i],
36
+ messageId: 'issue',
37
+ data: { line: seen.get(keys[i]) },
38
+ });
39
+ else seen.set(keys[i], bodies[i].loc.start.line);
40
+ }
41
+ }
42
+ return { if: check, '[kind="?"]': check };
43
+ },
44
+ });
@@ -0,0 +1,64 @@
1
+ import { defineRule } from './common.js';
2
+ import { getScopeManager } from '../scope.js';
3
+ export default defineRule({
4
+ meta: {
5
+ type: 'problem',
6
+ docs: { description: 'Disallow rebinding explicitly imported names.' },
7
+ schema: [
8
+ {
9
+ type: 'object',
10
+ properties: {
11
+ moduleExports: {
12
+ type: 'object',
13
+ additionalProperties: {
14
+ type: 'array',
15
+ items: { type: 'string' },
16
+ },
17
+ },
18
+ },
19
+ additionalProperties: false,
20
+ },
21
+ ],
22
+ messages: { assign: 'Cannot assign imported binding "{{name}}".' },
23
+ },
24
+ create(context) {
25
+ return {
26
+ JuliaFile() {
27
+ const m = getScopeManager(context.sourceCode),
28
+ o = context.options[0] ?? {},
29
+ source = context.sourceCode;
30
+ function arms(node) {
31
+ const result = new Map();
32
+ for (
33
+ let child = node, parent = source.getParent(child);
34
+ parent;
35
+ child = parent, parent = source.getParent(parent)
36
+ )
37
+ if (['if', 'elseif'].includes(parent.kind))
38
+ result.set(parent, parent.children.indexOf(child));
39
+ return result;
40
+ }
41
+ function exclusive(a, b) {
42
+ const left = arms(a),
43
+ right = arms(b);
44
+ return [...left].some(
45
+ ([n, arm]) => arm > 0 && right.has(n) && right.get(n) > 0 && right.get(n) !== arm,
46
+ );
47
+ }
48
+ for (const d of m.definitions) {
49
+ // A macro declaration (`@independent_variables t`) isn't a written assignment.
50
+ if (!d.initialized || d.kind === 'macro-binding' || d.binding.scope.kind !== 'module')
51
+ continue;
52
+ const explicit = d.binding.definitions.some(
53
+ (n) => n.kind === 'import' && !exclusive(d.node, n.node),
54
+ );
55
+ const configured = d.binding.scope.bareUsing.some((n) =>
56
+ o.moduleExports?.[n]?.includes(d.name),
57
+ );
58
+ if (explicit || configured)
59
+ context.report({ node: d.node, messageId: 'assign', data: { name: d.name } });
60
+ }
61
+ },
62
+ };
63
+ },
64
+ });