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,126 @@
1
+ import { defineRule, isOpaque } from './common.js';
2
+ import { isInfixCall } from '../parser/flags.js';
3
+ const comparisons = new Set([
4
+ '==',
5
+ '!=',
6
+ '===',
7
+ '!==',
8
+ '<',
9
+ '<=',
10
+ '>',
11
+ '>=',
12
+ '≤',
13
+ '≥',
14
+ '≠',
15
+ '≡',
16
+ '≢',
17
+ 'isa',
18
+ 'in',
19
+ '∈',
20
+ '∉',
21
+ ]);
22
+ const boundaries = new Set(['function', 'macro', '->', 'do', 'macrocall', 'quote', 'inert']);
23
+ export default defineRule({
24
+ meta: {
25
+ type: 'problem',
26
+ hasSuggestions: true,
27
+ schema: [
28
+ {
29
+ type: 'object',
30
+ properties: { allowParenthesized: { type: 'boolean' } },
31
+ additionalProperties: false,
32
+ },
33
+ ],
34
+ docs: { description: 'Report assignments evaluated in conditions.' },
35
+ messages: {
36
+ issue: 'Assignment is evaluated in a Boolean condition.',
37
+ compare: 'Compare with == instead of assigning.',
38
+ },
39
+ },
40
+ create(context) {
41
+ const source = context.sourceCode,
42
+ reported = new WeakSet();
43
+ function tested(node) {
44
+ for (let child = node, p = source.getParent(child); p; child = p, p = source.getParent(p)) {
45
+ if (p.kind === 'parens') continue;
46
+ if (['&&', '||'].includes(p.kind)) {
47
+ if (p.children[0] === child) return true;
48
+ continue;
49
+ }
50
+ return ['if', 'elseif', 'while', '?'].includes(p.kind) && p.children[0] === child;
51
+ }
52
+ return false;
53
+ }
54
+ function check(node) {
55
+ if (!node.interior || isOpaque(node, source)) return;
56
+ const logical = ['&&', '||'].includes(node.kind);
57
+ if (logical) {
58
+ for (let p = source.getParent(node); p; p = source.getParent(p)) {
59
+ if (boundaries.has(p.kind) || ['block', 'toplevel'].includes(p.kind)) break;
60
+ if (p.kind === 'comparison' || (isInfixCall(p) && comparisons.has(p.children[1]?.value)))
61
+ return;
62
+ }
63
+ }
64
+ // A final short-circuit operand can be a value/assignment, not a Bool.
65
+ const roots = logical
66
+ ? tested(node)
67
+ ? node.children
68
+ : [node.children[0]]
69
+ : [node.children[0]];
70
+ for (const root of roots) {
71
+ const stack = [[root, false]];
72
+ while (stack.length) {
73
+ const [n, grouped] = stack.pop();
74
+ if (!n || boundaries.has(n.kind) || n.kind === 'finally') continue;
75
+ // Sequential setup and let bindings are not tested values.
76
+ if (['block', 'toplevel', 'let', 'catch', 'else'].includes(n.kind)) {
77
+ if (n.children.length) stack.push([n.children.at(-1), false]);
78
+ continue;
79
+ }
80
+ if (['for', 'while'].includes(n.kind)) continue;
81
+ if (n.kind === 'comparison' || (isInfixCall(n) && comparisons.has(n.children[1]?.value)))
82
+ continue;
83
+ const parent = source.getParent(n);
84
+ if (n.kind === 'parameters') continue;
85
+ // In call argument positions an unparenthesized = is keyword syntax.
86
+ if (n.kind === '=' && ['call', 'dotcall'].includes(parent?.kind)) continue;
87
+ const assignment = n.interior && ['=', 'op=', '.=', '.op='].includes(n.kind);
88
+ if (
89
+ assignment &&
90
+ !(grouped && context.options[0]?.allowParenthesized) &&
91
+ !reported.has(n)
92
+ ) {
93
+ reported.add(n);
94
+ const operator =
95
+ n.kind === '=' &&
96
+ tested(n) &&
97
+ source.tokens.find(
98
+ (t) =>
99
+ t.value === '=' &&
100
+ t.range[0] >= n.children[0].range[1] &&
101
+ t.range[1] <= n.children[1].range[0],
102
+ );
103
+ context.report({
104
+ node: n,
105
+ messageId: 'issue',
106
+ ...(operator && {
107
+ suggest: [
108
+ { messageId: 'compare', fix: (fixer) => fixer.replaceText(operator, '==') },
109
+ ],
110
+ }),
111
+ });
112
+ }
113
+ for (const child of n.children) stack.push([child, n.kind === 'parens']);
114
+ }
115
+ }
116
+ }
117
+ return {
118
+ if: check,
119
+ elseif: check,
120
+ while: check,
121
+ '[kind="?"]': check,
122
+ '[kind="&&"]': check,
123
+ '[kind="||"]': check,
124
+ };
125
+ },
126
+ });
@@ -0,0 +1,61 @@
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 const globals.' },
7
+ schema: [
8
+ {
9
+ type: 'object',
10
+ properties: { readonlyGlobals: { type: 'array', items: { type: 'string' } } },
11
+ additionalProperties: false,
12
+ },
13
+ ],
14
+ messages: { assign: 'Cannot reassign constant "{{name}}".' },
15
+ },
16
+ create(context) {
17
+ return {
18
+ JuliaFile() {
19
+ const m = getScopeManager(context.sourceCode),
20
+ readonly = new Set(context.options[0]?.readonlyGlobals),
21
+ source = context.sourceCode;
22
+ function arms(node) {
23
+ const result = new Map();
24
+ for (
25
+ let child = node, parent = source.getParent(child);
26
+ parent;
27
+ child = parent, parent = source.getParent(parent)
28
+ )
29
+ if (['if', 'elseif'].includes(parent.kind))
30
+ result.set(parent, parent.children.indexOf(child));
31
+ return result;
32
+ }
33
+ function exclusive(a, b) {
34
+ const left = arms(a),
35
+ right = arms(b);
36
+ return [...left].some(
37
+ ([n, arm]) => arm > 0 && right.has(n) && right.get(n) > 0 && right.get(n) !== arm,
38
+ );
39
+ }
40
+ for (const s of m.scopes.filter((s) => s.kind === 'module'))
41
+ for (const b of s.bindings.values()) {
42
+ const origins = b.definitions.filter((d) => d.constant && d.initialized);
43
+ if (!origins.length && !readonly.has(b.name)) continue;
44
+ for (const d of b.writes) {
45
+ if (
46
+ origins.length &&
47
+ !origins.some(
48
+ (c) =>
49
+ c !== d &&
50
+ (d.scope !== b.scope || c.node.range[0] < d.node.range[0]) &&
51
+ !exclusive(c.node, d.node),
52
+ )
53
+ )
54
+ continue;
55
+ context.report({ node: d.node, messageId: 'assign', data: { name: d.name } });
56
+ }
57
+ }
58
+ },
59
+ };
60
+ },
61
+ });
@@ -0,0 +1,59 @@
1
+ import { defineRule, isOpaque } from './common.js';
2
+ const numeric = new Set(['Integer', 'BinInt', 'OctInt', 'HexInt', 'Float', 'Float32']);
3
+ function literal(n) {
4
+ while (n.kind === 'parens' && n.children.length === 1) n = n.children[0];
5
+ if (!n.interior && (n.kind === 'Bool' || n.kind === 'Char' || numeric.has(n.kind))) return n;
6
+ if (n.kind === 'char' && n.interior && n.children.length === 1 && n.children[0].kind === 'Char')
7
+ return n;
8
+ if (
9
+ n.kind === 'string' &&
10
+ n.interior &&
11
+ n.children.every((c) => !c.interior && c.kind === 'String')
12
+ )
13
+ return n;
14
+ return null;
15
+ }
16
+ export default defineRule({
17
+ meta: {
18
+ type: 'problem',
19
+ schema: [
20
+ {
21
+ type: 'object',
22
+ properties: {
23
+ checkInfiniteLoops: { type: 'boolean' },
24
+ checkWhileTrue: { type: 'boolean' },
25
+ checkTernary: { type: 'boolean' },
26
+ },
27
+ additionalProperties: false,
28
+ },
29
+ ],
30
+ docs: { description: 'Report literal conditions, allowing while true by default.' },
31
+ messages: { issue: 'Literal condition is {{reason}}.' },
32
+ },
33
+ create(context) {
34
+ const opts = context.options[0] ?? {};
35
+ function check(node) {
36
+ if (
37
+ !node.interior ||
38
+ isOpaque(node, context.sourceCode) ||
39
+ (node.kind === '?' && opts.checkTernary === false)
40
+ )
41
+ return;
42
+ const value = literal(node.children[0]);
43
+ if (!value) return;
44
+ if (
45
+ node.kind === 'while' &&
46
+ value.kind === 'Bool' &&
47
+ value.value === 'true' &&
48
+ !(opts.checkInfiniteLoops ?? opts.checkWhileTrue ?? false)
49
+ )
50
+ return;
51
+ context.report({
52
+ node: node.children[0],
53
+ messageId: 'issue',
54
+ data: { reason: value.kind === 'Bool' ? 'constant' : 'not a Bool' },
55
+ });
56
+ }
57
+ return { if: check, elseif: check, while: check, '[kind="?"]': check };
58
+ },
59
+ });
@@ -0,0 +1,96 @@
1
+ import { defineRule } from './common.js';
2
+ import { getScopeManager } from '../scope.js';
3
+ import { originResolver, visible, writtenName, strip } from './restricted-common.js';
4
+ import { span } from './idiom-common.js';
5
+ const debuggerMacros = {
6
+ Infiltrator: ['@infiltrate', '@exfiltrate'],
7
+ Debugger: ['@enter', '@run', '@bp'],
8
+ };
9
+ const origins = new Set(
10
+ Object.entries(debuggerMacros).flatMap(([mod, names]) => names.map((name) => `${mod}.${name}`)),
11
+ );
12
+ // Infiltrator's macro names are distinctive, and Infiltrator is usually loaded from the REPL or
13
+ // startup.jl rather than imported by the file: report them bare when nothing in scope defines or
14
+ // imports the name, and in the `Main.@infiltrate` form Infiltrator documents for such code.
15
+ const distinctive = new Set(debuggerMacros.Infiltrator);
16
+ const mainQualified = /^Main\.(?:Infiltrator\.)?(@infiltrate|@exfiltrate)$/u;
17
+ export default defineRule({
18
+ meta: {
19
+ type: 'problem',
20
+ hasSuggestions: true,
21
+ docs: { description: 'Report interactive breakpoint macros left in source code.' },
22
+ defaultOptions: [{ checkImports: false, includeMacroPayloads: false }],
23
+ schema: [
24
+ {
25
+ type: 'object',
26
+ additionalProperties: false,
27
+ properties: {
28
+ checkImports: { type: 'boolean' },
29
+ includeMacroPayloads: { type: 'boolean' },
30
+ },
31
+ },
32
+ ],
33
+ messages: {
34
+ breakpoint: 'Remove the interactive debugger invocation {{name}}.',
35
+ debuggerImport: 'Remove the interactive debugger dependency {{name}} from this file.',
36
+ remove: 'Remove this debugger statement.',
37
+ },
38
+ },
39
+ create(context) {
40
+ const source = context.sourceCode,
41
+ m = getScopeManager(source, context),
42
+ options = context.options[0] ?? {};
43
+ const origin = originResolver(m, debuggerMacros);
44
+ function unimported(node) {
45
+ const head = node.children[0],
46
+ spelling = writtenName(head);
47
+ if (!spelling) return null;
48
+ const scope = m.scopeFor(node) ?? m.globalScope;
49
+ if (distinctive.has(spelling) && !(m.resolve(strip(head)) ?? m.lookup(spelling, scope)))
50
+ return `Infiltrator.${spelling}`;
51
+ const q = mainQualified.exec(spelling);
52
+ const main = q && m.lookup('Main', scope);
53
+ return q && (!main || main.kind === 'builtin') ? `Infiltrator.${q[1]}` : null;
54
+ }
55
+ function checkImport(node) {
56
+ if (!options.checkImports || !visible(node, source, options.includeMacroPayloads)) return;
57
+ const packages = new Set(
58
+ node.children
59
+ .map((c) => (c.kind === ':' || c.kind === 'as' ? c.children[0] : c))
60
+ .map((path) => source.getText(path).replace(/\s/gu, '').split('.')[0])
61
+ .filter((name) => Object.hasOwn(debuggerMacros, name)),
62
+ );
63
+ for (const name of packages)
64
+ context.report({ node, messageId: 'debuggerImport', data: { name } });
65
+ }
66
+ return {
67
+ macrocall(node) {
68
+ if (!visible(node, source, options.includeMacroPayloads)) return;
69
+ const name = origin(node.children[0], node) ?? unimported(node);
70
+ if (!origins.has(name)) return;
71
+ let statement = node,
72
+ parent = source.getParent(statement);
73
+ while (parent?.kind === 'parens') {
74
+ statement = parent;
75
+ parent = source.getParent(parent);
76
+ }
77
+ const range = span(source, statement);
78
+ const wholeStatement = ['block', 'toplevel'].includes(parent?.kind);
79
+ const comments = source.comments.some(
80
+ (c) => c.range[0] >= range[0] && c.range[1] <= range[1],
81
+ );
82
+ context.report({
83
+ node,
84
+ messageId: 'breakpoint',
85
+ data: { name },
86
+ ...(wholeStatement &&
87
+ !comments && {
88
+ suggest: [{ messageId: 'remove', fix: (fixer) => fixer.removeRange(range) }],
89
+ }),
90
+ });
91
+ },
92
+ using: checkImport,
93
+ import: checkImport,
94
+ };
95
+ },
96
+ });
@@ -0,0 +1,50 @@
1
+ import { rule, ordinary, call, strip, suggest } from './idiom-common.js';
2
+ export default rule(
3
+ 'Warn about discarded results of copying collection operations.',
4
+ {
5
+ knownArraysOnly: { type: 'boolean' },
6
+ checkCallbacks: { type: 'boolean' },
7
+ },
8
+ (context, m, source, options) => ({
9
+ call(node) {
10
+ if (!ordinary(node, source)) return;
11
+ const name = [
12
+ 'sort',
13
+ 'reverse',
14
+ 'unique',
15
+ 'copy',
16
+ ...(options.checkCallbacks === false ? [] : ['filter']),
17
+ ].find((n) => call(node, n, m));
18
+ if (!name) return;
19
+ const args = call(node, name, m);
20
+ if (
21
+ args.some((n) => n.kind === '...') ||
22
+ args.filter((n) => n.kind !== 'parameters').length !== (name === 'filter' ? 2 : 1)
23
+ )
24
+ return;
25
+ const keywords = args.filter((n) => n.kind === 'parameters').flatMap((n) => n.children);
26
+ if (
27
+ keywords.some((n) => n.kind !== '=' || ['by', 'lt', 'order'].includes(n.children[0]?.value))
28
+ )
29
+ return;
30
+ const block = source.getParent(node),
31
+ fn = source.getParent(block);
32
+ if (
33
+ block?.kind !== 'block' ||
34
+ !['function', '->', 'do'].includes(fn?.kind) ||
35
+ fn.children.at(-1) !== block ||
36
+ block.children.at(-1) === node
37
+ )
38
+ return;
39
+ const input = strip(args[name === 'filter' ? 1 : 0]);
40
+ if (options.knownArraysOnly && !['vect', 'vcat', 'hcat', 'typed_vcat'].includes(input.kind))
41
+ return;
42
+ suggest(
43
+ context,
44
+ node,
45
+ `The result of ${name} is discarded; review whether mutation or the returned copy was intended.`,
46
+ );
47
+ },
48
+ }),
49
+ false,
50
+ );
@@ -0,0 +1,25 @@
1
+ import { defineRule } from './common.js';
2
+ import { getScopeManager } from '../scope.js';
3
+ export default defineRule({
4
+ meta: {
5
+ type: 'problem',
6
+ schema: [],
7
+ docs: { description: 'Disallow repeated names in one signature.' },
8
+ messages: { duplicate: 'Duplicate argument "{{name}}".' },
9
+ },
10
+ create(context) {
11
+ return {
12
+ JuliaFile() {
13
+ for (const fn of getScopeManager(context.sourceCode).functions) {
14
+ const seen = new Set();
15
+ for (const d of fn.arguments) {
16
+ if (/^_+$/u.test(d.name)) continue;
17
+ if (seen.has(d.name))
18
+ context.report({ node: d.node, messageId: 'duplicate', data: { name: d.name } });
19
+ seen.add(d.name);
20
+ }
21
+ }
22
+ },
23
+ };
24
+ },
25
+ });
@@ -0,0 +1,57 @@
1
+ import { defineRule, isBareName, isOpaque, normalizedTreeKey } from './common.js';
2
+ import { isInfixCall, isPrefixOperatorCall } from '../parser/flags.js';
3
+ const comparisons = new Set([
4
+ '==',
5
+ '!=',
6
+ '===',
7
+ '!==',
8
+ '<',
9
+ '<=',
10
+ '>',
11
+ '>=',
12
+ 'in',
13
+ '∈',
14
+ '∉',
15
+ 'isa',
16
+ ]);
17
+ function eligible(n) {
18
+ if (isBareName(n) || (!n.interior && ['Bool', 'Integer', 'Float', 'Char'].includes(n.kind)))
19
+ return true;
20
+ if (n.kind === 'parens' && n.children.length === 1) return eligible(n.children[0]);
21
+ if (['&&', '||'].includes(n.kind)) return n.children.every(eligible);
22
+ if (isPrefixOperatorCall(n) && n.children[0]?.value === '!')
23
+ return n.children.slice(1).every(eligible);
24
+ if (isInfixCall(n) && n.children.length === 3 && comparisons.has(n.children[1].value))
25
+ return eligible(n.children[0]) && eligible(n.children[2]);
26
+ if (n.kind === 'comparison')
27
+ return n.children.every((c, i) => (i % 2 ? comparisons.has(c.value) : eligible(c)));
28
+ return false;
29
+ }
30
+ export default defineRule({
31
+ meta: {
32
+ type: 'problem',
33
+ schema: [],
34
+ docs: { description: 'Report repeated conditions within one if/elseif chain.' },
35
+ messages: { issue: 'Condition repeats an earlier condition in this if chain.' },
36
+ },
37
+ create(context) {
38
+ return {
39
+ if(node) {
40
+ if (!node.interior || isOpaque(node, context.sourceCode)) return;
41
+ const seen = new Set();
42
+ for (
43
+ let branch = node;
44
+ branch;
45
+ branch = branch.children[2]?.kind === 'elseif' ? branch.children[2] : null
46
+ ) {
47
+ const condition = branch.children[0];
48
+ // An unknown evaluation can mutate earlier operands: stop this chain.
49
+ if (!eligible(condition)) break;
50
+ const key = normalizedTreeKey(condition);
51
+ if (seen.has(key)) context.report({ node: condition, messageId: 'issue' });
52
+ seen.add(key);
53
+ }
54
+ },
55
+ };
56
+ },
57
+ });
@@ -0,0 +1,95 @@
1
+ import { check, ordinary, strip, call, binary, text } from './semantic-common.js';
2
+
3
+ // Decode a bounded subset of Julia strings. Unsupported byte escapes and triple
4
+ // strings are skipped rather than interpreted using JavaScript string semantics.
5
+ function key(node, source) {
6
+ node = strip(node);
7
+ if (node.kind === 'quote' && node.children.length === 1 && node.children[0].kind === 'Identifier')
8
+ return ['Symbol', node.children[0].value];
9
+ if (node.kind !== 'string' || node.children.some((c) => c.kind !== 'String')) return null;
10
+ const raw = text(source, node);
11
+ if (!raw.startsWith('"') || raw.startsWith('"""')) return null;
12
+ let value = '',
13
+ body = raw.slice(1, -1);
14
+ const simple = {
15
+ n: '\n',
16
+ r: '\r',
17
+ t: '\t',
18
+ b: '\b',
19
+ f: '\f',
20
+ v: '\v',
21
+ a: '\x07',
22
+ e: '\x1b',
23
+ '\\': '\\',
24
+ '"': '"',
25
+ $: '$',
26
+ };
27
+ for (let i = 0; i < body.length; i++) {
28
+ if (body[i] !== '\\') {
29
+ value += body[i];
30
+ continue;
31
+ }
32
+ const c = body[++i];
33
+ if (Object.hasOwn(simple, c)) {
34
+ value += simple[c];
35
+ continue;
36
+ }
37
+ const pattern =
38
+ c === 'u'
39
+ ? /^[0-9a-fA-F]{1,4}/u
40
+ : c === 'U'
41
+ ? /^[0-9a-fA-F]{1,8}/u
42
+ : c === 'x'
43
+ ? /^[0-9a-fA-F]{1,2}/u
44
+ : null;
45
+ if (!pattern) return null;
46
+ const match = body.slice(i + 1).match(pattern);
47
+ if (!match) return null;
48
+ const cp = parseInt(match[0], 16);
49
+ if (cp > 0x10ffff || (cp >= 0xd800 && cp <= 0xdfff) || (c === 'x' && cp > 127)) return null;
50
+ value += String.fromCodePoint(cp);
51
+ i += match[0].length;
52
+ }
53
+ return ['String', value];
54
+ }
55
+ export default check(
56
+ 'Report duplicate homogeneous literal keys in builtin Dict constructors.',
57
+ (context, m, source) => ({
58
+ call(node) {
59
+ if (!ordinary(node, source)) return;
60
+ const args = call(node, 'Dict', m);
61
+ if (!args?.length) return;
62
+ const head = strip(node.children[0]);
63
+ const keys = [];
64
+ for (const arg of args) {
65
+ const pair = binary(strip(arg));
66
+ if (!pair || !m.isUnshadowedBuiltin(pair[1], '=>')) return;
67
+ const k = key(pair[0], source);
68
+ if (!k || (keys.length && k[0] !== keys[0].key[0])) return;
69
+ keys.push({ key: k, node: pair[0] });
70
+ }
71
+ if (
72
+ head.kind === 'curly' &&
73
+ (head.children.length !== 3 ||
74
+ !m.isUnshadowedBuiltin(strip(head.children[1]), keys[0].key[0]))
75
+ )
76
+ return;
77
+ const seen = new Set();
78
+ for (const {
79
+ key: [, value],
80
+ node: at,
81
+ } of keys) {
82
+ if (seen.has(value))
83
+ context.report({
84
+ node: at,
85
+ messageId: 'diagnostic',
86
+ data: {
87
+ message:
88
+ 'This literal dictionary key repeats an earlier key; the later value overwrites it.',
89
+ },
90
+ });
91
+ seen.add(value);
92
+ }
93
+ },
94
+ }),
95
+ );
@@ -0,0 +1,40 @@
1
+ import { defineRule, isBareName, isOpaque, isSignature } from './common.js';
2
+ import { isPrefixCall } from '../parser/flags.js';
3
+ export default defineRule({
4
+ meta: {
5
+ type: 'problem',
6
+ schema: [],
7
+ docs: { description: 'Disallow repeated explicit keyword names in a call.' },
8
+ messages: { duplicate: 'Duplicate keyword argument "{{name}}".' },
9
+ },
10
+ create(context) {
11
+ return {
12
+ call(node) {
13
+ if (
14
+ !isPrefixCall(node) ||
15
+ isOpaque(node, context.sourceCode) ||
16
+ isSignature(node, context.sourceCode)
17
+ )
18
+ return;
19
+ const args = node.children
20
+ .slice(1)
21
+ .flatMap((n) => (n.kind === 'parameters' && n.interior ? n.children : [n]));
22
+ if (args.some((n) => n.kind === '...')) return;
23
+ const seen = new Set();
24
+ for (const arg of args) {
25
+ if (
26
+ arg.kind !== '=' ||
27
+ !arg.interior ||
28
+ arg.children.length !== 2 ||
29
+ !isBareName(arg.children[0])
30
+ )
31
+ continue;
32
+ const name = arg.children[0];
33
+ if (seen.has(name.value))
34
+ context.report({ node: name, messageId: 'duplicate', data: { name: name.value } });
35
+ seen.add(name.value);
36
+ }
37
+ },
38
+ };
39
+ },
40
+ });