@gulu9527/code-trust 0.1.0 → 0.2.0
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/README.md +21 -8
- package/action.yml +1 -1
- package/dist/cli/index.js +848 -1
- package/dist/cli/index.js.map +1 -1
- package/dist/index.js +848 -1
- package/dist/index.js.map +1 -1
- package/package.json +1 -1
package/dist/index.js
CHANGED
|
@@ -1605,6 +1605,840 @@ function isInsidePromiseChain(_node, parent) {
|
|
|
1605
1605
|
return false;
|
|
1606
1606
|
}
|
|
1607
1607
|
|
|
1608
|
+
// src/rules/builtin/any-type-abuse.ts
|
|
1609
|
+
var anyTypeAbuseRule = {
|
|
1610
|
+
id: "logic/any-type-abuse",
|
|
1611
|
+
category: "logic",
|
|
1612
|
+
severity: "medium",
|
|
1613
|
+
title: "Excessive any type usage",
|
|
1614
|
+
description: "AI-generated code often uses `any` type to bypass TypeScript type checking, reducing type safety.",
|
|
1615
|
+
check(context) {
|
|
1616
|
+
const issues = [];
|
|
1617
|
+
if (!context.filePath.match(/\.tsx?$/)) return issues;
|
|
1618
|
+
let ast;
|
|
1619
|
+
try {
|
|
1620
|
+
const parsed = parseCode(context.fileContent, context.filePath);
|
|
1621
|
+
ast = parsed.ast;
|
|
1622
|
+
} catch {
|
|
1623
|
+
return issues;
|
|
1624
|
+
}
|
|
1625
|
+
const lines = context.fileContent.split("\n");
|
|
1626
|
+
walkAST(ast, (node) => {
|
|
1627
|
+
if (node.type !== AST_NODE_TYPES.TSAnyKeyword) return;
|
|
1628
|
+
const line = node.loc?.start.line ?? 0;
|
|
1629
|
+
if (line === 0) return;
|
|
1630
|
+
const lineContent = lines[line - 1] ?? "";
|
|
1631
|
+
const trimmed = lineContent.trim();
|
|
1632
|
+
if (trimmed.startsWith("//") || trimmed.startsWith("*") || trimmed.startsWith("/*")) return;
|
|
1633
|
+
const parent = node.parent;
|
|
1634
|
+
if (parent?.type === AST_NODE_TYPES.TSTypeAssertion || parent?.type === AST_NODE_TYPES.TSAsExpression) {
|
|
1635
|
+
let ancestor = parent;
|
|
1636
|
+
while (ancestor) {
|
|
1637
|
+
if (ancestor.type === AST_NODE_TYPES.CatchClause) return;
|
|
1638
|
+
ancestor = ancestor.parent;
|
|
1639
|
+
}
|
|
1640
|
+
}
|
|
1641
|
+
issues.push({
|
|
1642
|
+
ruleId: "logic/any-type-abuse",
|
|
1643
|
+
severity: "medium",
|
|
1644
|
+
category: "logic",
|
|
1645
|
+
file: context.filePath,
|
|
1646
|
+
startLine: line,
|
|
1647
|
+
endLine: line,
|
|
1648
|
+
message: t(
|
|
1649
|
+
`Usage of "any" type reduces type safety.`,
|
|
1650
|
+
`\u4F7F\u7528 "any" \u7C7B\u578B\u964D\u4F4E\u4E86\u7C7B\u578B\u5B89\u5168\u6027\u3002`
|
|
1651
|
+
),
|
|
1652
|
+
suggestion: t(
|
|
1653
|
+
`Replace "any" with a specific type or "unknown" for safer type narrowing.`,
|
|
1654
|
+
`\u5C06 "any" \u66FF\u6362\u4E3A\u5177\u4F53\u7C7B\u578B\u6216\u4F7F\u7528 "unknown" \u8FDB\u884C\u66F4\u5B89\u5168\u7684\u7C7B\u578B\u6536\u7A84\u3002`
|
|
1655
|
+
)
|
|
1656
|
+
});
|
|
1657
|
+
});
|
|
1658
|
+
return issues;
|
|
1659
|
+
}
|
|
1660
|
+
};
|
|
1661
|
+
|
|
1662
|
+
// src/rules/builtin/type-coercion.ts
|
|
1663
|
+
var typeCoercionRule = {
|
|
1664
|
+
id: "logic/type-coercion",
|
|
1665
|
+
category: "logic",
|
|
1666
|
+
severity: "medium",
|
|
1667
|
+
title: "Loose equality with type coercion",
|
|
1668
|
+
description: "AI-generated code often uses == instead of ===, leading to implicit type coercion bugs.",
|
|
1669
|
+
check(context) {
|
|
1670
|
+
const issues = [];
|
|
1671
|
+
const lines = context.fileContent.split("\n");
|
|
1672
|
+
let inBlockComment = false;
|
|
1673
|
+
for (let i = 0; i < lines.length; i++) {
|
|
1674
|
+
const line = lines[i];
|
|
1675
|
+
const trimmed = line.trim();
|
|
1676
|
+
if (inBlockComment) {
|
|
1677
|
+
if (trimmed.includes("*/")) inBlockComment = false;
|
|
1678
|
+
continue;
|
|
1679
|
+
}
|
|
1680
|
+
if (trimmed.startsWith("/*")) {
|
|
1681
|
+
if (!trimmed.includes("*/")) inBlockComment = true;
|
|
1682
|
+
continue;
|
|
1683
|
+
}
|
|
1684
|
+
if (trimmed.startsWith("//")) continue;
|
|
1685
|
+
const cleaned = line.replace(/(['"`])(?:(?!\1|\\).|\\.)*\1/g, '""').replace(/\/\/.*$/, "");
|
|
1686
|
+
const looseEqRegex = /[^!=<>]==[^=]|[^!]==[^=]|!=[^=]/g;
|
|
1687
|
+
let match;
|
|
1688
|
+
while ((match = looseEqRegex.exec(cleaned)) !== null) {
|
|
1689
|
+
const pos = match.index;
|
|
1690
|
+
const snippet = cleaned.substring(Math.max(0, pos), pos + match[0].length);
|
|
1691
|
+
if (snippet.includes("===") || snippet.includes("!==")) continue;
|
|
1692
|
+
const isNotEqual = snippet.includes("!=");
|
|
1693
|
+
const operator = isNotEqual ? "!=" : "==";
|
|
1694
|
+
const strict = isNotEqual ? "!==" : "===";
|
|
1695
|
+
const afterOp = cleaned.substring(pos + match[0].length - 1).trim();
|
|
1696
|
+
if (afterOp.startsWith("null") || afterOp.startsWith("undefined")) continue;
|
|
1697
|
+
const beforeOp = cleaned.substring(0, pos + 1).trim();
|
|
1698
|
+
if (beforeOp.endsWith("null") || beforeOp.endsWith("undefined")) continue;
|
|
1699
|
+
issues.push({
|
|
1700
|
+
ruleId: "logic/type-coercion",
|
|
1701
|
+
severity: "medium",
|
|
1702
|
+
category: "logic",
|
|
1703
|
+
file: context.filePath,
|
|
1704
|
+
startLine: i + 1,
|
|
1705
|
+
endLine: i + 1,
|
|
1706
|
+
message: t(
|
|
1707
|
+
`Loose equality "${operator}" can cause implicit type coercion.`,
|
|
1708
|
+
`\u5BBD\u677E\u7B49\u4E8E "${operator}" \u4F1A\u5BFC\u81F4\u9690\u5F0F\u7C7B\u578B\u8F6C\u6362\u3002`
|
|
1709
|
+
),
|
|
1710
|
+
suggestion: t(
|
|
1711
|
+
`Use strict equality "${strict}" instead of "${operator}".`,
|
|
1712
|
+
`\u4F7F\u7528\u4E25\u683C\u7B49\u4E8E "${strict}" \u4EE3\u66FF "${operator}"\u3002`
|
|
1713
|
+
)
|
|
1714
|
+
});
|
|
1715
|
+
}
|
|
1716
|
+
}
|
|
1717
|
+
return issues;
|
|
1718
|
+
}
|
|
1719
|
+
};
|
|
1720
|
+
|
|
1721
|
+
// src/rules/builtin/magic-number.ts
|
|
1722
|
+
var ALLOWED_NUMBERS = /* @__PURE__ */ new Set([
|
|
1723
|
+
-1,
|
|
1724
|
+
0,
|
|
1725
|
+
1,
|
|
1726
|
+
2,
|
|
1727
|
+
10,
|
|
1728
|
+
100
|
|
1729
|
+
]);
|
|
1730
|
+
var magicNumberRule = {
|
|
1731
|
+
id: "logic/magic-number",
|
|
1732
|
+
category: "logic",
|
|
1733
|
+
severity: "low",
|
|
1734
|
+
title: "Magic number",
|
|
1735
|
+
description: "AI-generated code often uses unexplained numeric literals instead of named constants.",
|
|
1736
|
+
check(context) {
|
|
1737
|
+
const issues = [];
|
|
1738
|
+
const lines = context.fileContent.split("\n");
|
|
1739
|
+
let inBlockComment = false;
|
|
1740
|
+
for (let i = 0; i < lines.length; i++) {
|
|
1741
|
+
const line = lines[i];
|
|
1742
|
+
const trimmed = line.trim();
|
|
1743
|
+
if (inBlockComment) {
|
|
1744
|
+
if (trimmed.includes("*/")) inBlockComment = false;
|
|
1745
|
+
continue;
|
|
1746
|
+
}
|
|
1747
|
+
if (trimmed.startsWith("/*")) {
|
|
1748
|
+
if (!trimmed.includes("*/")) inBlockComment = true;
|
|
1749
|
+
continue;
|
|
1750
|
+
}
|
|
1751
|
+
if (trimmed.startsWith("//")) continue;
|
|
1752
|
+
if (/^\s*(export\s+)?(const|let|var|readonly)\s+[A-Z_][A-Z0-9_]*\s*[=:]/.test(line)) continue;
|
|
1753
|
+
if (/^\s*(export\s+)?enum\s/.test(line)) continue;
|
|
1754
|
+
if (trimmed.startsWith("import ")) continue;
|
|
1755
|
+
if (/^\s*return\s+[0-9]+\s*;?\s*$/.test(line)) continue;
|
|
1756
|
+
const cleaned = line.replace(/(['"`])(?:(?!\1|\\).|\\.)*\1/g, '""').replace(/\/\/.*$/, "");
|
|
1757
|
+
const numRegex = /(?<![.\w])(-?\d+\.?\d*(?:e[+-]?\d+)?)\b/gi;
|
|
1758
|
+
let match;
|
|
1759
|
+
while ((match = numRegex.exec(cleaned)) !== null) {
|
|
1760
|
+
const value = parseFloat(match[1]);
|
|
1761
|
+
if (ALLOWED_NUMBERS.has(value)) continue;
|
|
1762
|
+
if (isNaN(value)) continue;
|
|
1763
|
+
const beforeChar = cleaned[match.index - 1] || "";
|
|
1764
|
+
if (beforeChar === "[") continue;
|
|
1765
|
+
if (beforeChar === "<" || beforeChar === ",") continue;
|
|
1766
|
+
issues.push({
|
|
1767
|
+
ruleId: "logic/magic-number",
|
|
1768
|
+
severity: "low",
|
|
1769
|
+
category: "logic",
|
|
1770
|
+
file: context.filePath,
|
|
1771
|
+
startLine: i + 1,
|
|
1772
|
+
endLine: i + 1,
|
|
1773
|
+
message: t(
|
|
1774
|
+
`Magic number ${match[1]} should be extracted to a named constant.`,
|
|
1775
|
+
`\u9B54\u672F\u6570\u5B57 ${match[1]} \u5E94\u63D0\u53D6\u4E3A\u547D\u540D\u5E38\u91CF\u3002`
|
|
1776
|
+
),
|
|
1777
|
+
suggestion: t(
|
|
1778
|
+
`Define a descriptive constant, e.g., const MAX_RETRIES = ${match[1]};`,
|
|
1779
|
+
`\u5B9A\u4E49\u4E00\u4E2A\u63CF\u8FF0\u6027\u5E38\u91CF\uFF0C\u4F8B\u5982 const MAX_RETRIES = ${match[1]};`
|
|
1780
|
+
)
|
|
1781
|
+
});
|
|
1782
|
+
}
|
|
1783
|
+
}
|
|
1784
|
+
return issues;
|
|
1785
|
+
}
|
|
1786
|
+
};
|
|
1787
|
+
|
|
1788
|
+
// src/rules/builtin/nested-ternary.ts
|
|
1789
|
+
var nestedTernaryRule = {
|
|
1790
|
+
id: "logic/no-nested-ternary",
|
|
1791
|
+
category: "logic",
|
|
1792
|
+
severity: "medium",
|
|
1793
|
+
title: "Nested ternary expression",
|
|
1794
|
+
description: "AI-generated code often produces nested ternary expressions that are hard to read.",
|
|
1795
|
+
check(context) {
|
|
1796
|
+
const issues = [];
|
|
1797
|
+
let ast;
|
|
1798
|
+
try {
|
|
1799
|
+
const parsed = parseCode(context.fileContent, context.filePath);
|
|
1800
|
+
ast = parsed.ast;
|
|
1801
|
+
} catch {
|
|
1802
|
+
return issues;
|
|
1803
|
+
}
|
|
1804
|
+
const reportedLines = /* @__PURE__ */ new Set();
|
|
1805
|
+
walkAST(ast, (node) => {
|
|
1806
|
+
if (node.type !== AST_NODE_TYPES.ConditionalExpression) return;
|
|
1807
|
+
const conditional = node;
|
|
1808
|
+
const hasNestedTernary = conditional.consequent.type === AST_NODE_TYPES.ConditionalExpression || conditional.alternate.type === AST_NODE_TYPES.ConditionalExpression;
|
|
1809
|
+
if (!hasNestedTernary) return;
|
|
1810
|
+
const line = node.loc?.start.line ?? 0;
|
|
1811
|
+
if (line === 0 || reportedLines.has(line)) return;
|
|
1812
|
+
reportedLines.add(line);
|
|
1813
|
+
let depth = 1;
|
|
1814
|
+
let current = node;
|
|
1815
|
+
while (current.type === AST_NODE_TYPES.ConditionalExpression) {
|
|
1816
|
+
const cond = current;
|
|
1817
|
+
if (cond.consequent.type === AST_NODE_TYPES.ConditionalExpression) {
|
|
1818
|
+
depth++;
|
|
1819
|
+
current = cond.consequent;
|
|
1820
|
+
} else if (cond.alternate.type === AST_NODE_TYPES.ConditionalExpression) {
|
|
1821
|
+
depth++;
|
|
1822
|
+
current = cond.alternate;
|
|
1823
|
+
} else {
|
|
1824
|
+
break;
|
|
1825
|
+
}
|
|
1826
|
+
}
|
|
1827
|
+
const endLine = node.loc?.end.line ?? line;
|
|
1828
|
+
issues.push({
|
|
1829
|
+
ruleId: "logic/no-nested-ternary",
|
|
1830
|
+
severity: "medium",
|
|
1831
|
+
category: "logic",
|
|
1832
|
+
file: context.filePath,
|
|
1833
|
+
startLine: line,
|
|
1834
|
+
endLine,
|
|
1835
|
+
message: t(
|
|
1836
|
+
`Nested ternary expression (depth: ${depth}) reduces readability.`,
|
|
1837
|
+
`\u5D4C\u5957\u4E09\u5143\u8868\u8FBE\u5F0F\uFF08\u6DF1\u5EA6: ${depth}\uFF09\u964D\u4F4E\u4E86\u53EF\u8BFB\u6027\u3002`
|
|
1838
|
+
),
|
|
1839
|
+
suggestion: t(
|
|
1840
|
+
`Refactor into if-else statements or use a lookup object/map.`,
|
|
1841
|
+
`\u91CD\u6784\u4E3A if-else \u8BED\u53E5\u6216\u4F7F\u7528\u67E5\u627E\u5BF9\u8C61/\u6620\u5C04\u3002`
|
|
1842
|
+
)
|
|
1843
|
+
});
|
|
1844
|
+
});
|
|
1845
|
+
return issues;
|
|
1846
|
+
}
|
|
1847
|
+
};
|
|
1848
|
+
|
|
1849
|
+
// src/rules/builtin/duplicate-string.ts
|
|
1850
|
+
var MIN_STRING_LENGTH = 6;
|
|
1851
|
+
var MIN_OCCURRENCES = 3;
|
|
1852
|
+
var duplicateStringRule = {
|
|
1853
|
+
id: "logic/duplicate-string",
|
|
1854
|
+
category: "logic",
|
|
1855
|
+
severity: "low",
|
|
1856
|
+
title: "Duplicate string literal",
|
|
1857
|
+
description: "AI-generated code often repeats the same string literal instead of extracting it into a constant.",
|
|
1858
|
+
check(context) {
|
|
1859
|
+
const issues = [];
|
|
1860
|
+
const lines = context.fileContent.split("\n");
|
|
1861
|
+
const stringMap = /* @__PURE__ */ new Map();
|
|
1862
|
+
let inBlockComment = false;
|
|
1863
|
+
for (let i = 0; i < lines.length; i++) {
|
|
1864
|
+
const line = lines[i];
|
|
1865
|
+
const trimmed = line.trim();
|
|
1866
|
+
if (inBlockComment) {
|
|
1867
|
+
if (trimmed.includes("*/")) inBlockComment = false;
|
|
1868
|
+
continue;
|
|
1869
|
+
}
|
|
1870
|
+
if (trimmed.startsWith("/*")) {
|
|
1871
|
+
if (!trimmed.includes("*/")) inBlockComment = true;
|
|
1872
|
+
continue;
|
|
1873
|
+
}
|
|
1874
|
+
if (trimmed.startsWith("//")) continue;
|
|
1875
|
+
if (trimmed.startsWith("import ")) continue;
|
|
1876
|
+
const cleaned = line.replace(/\/\/.*$/, "");
|
|
1877
|
+
const stringRegex = /(['"])([^'"\\](?:(?!\1|\\).|\\.)*)\1/g;
|
|
1878
|
+
let match;
|
|
1879
|
+
while ((match = stringRegex.exec(cleaned)) !== null) {
|
|
1880
|
+
const value = match[2];
|
|
1881
|
+
if (value.length < MIN_STRING_LENGTH) continue;
|
|
1882
|
+
if (value.includes("${")) continue;
|
|
1883
|
+
if (value.startsWith("http") || value.startsWith("/")) continue;
|
|
1884
|
+
if (value.startsWith("test") || value.startsWith("mock")) continue;
|
|
1885
|
+
if (!stringMap.has(value)) {
|
|
1886
|
+
stringMap.set(value, []);
|
|
1887
|
+
}
|
|
1888
|
+
stringMap.get(value).push(i + 1);
|
|
1889
|
+
}
|
|
1890
|
+
}
|
|
1891
|
+
for (const [value, locations] of stringMap) {
|
|
1892
|
+
if (locations.length < MIN_OCCURRENCES) continue;
|
|
1893
|
+
const firstLine = locations[0];
|
|
1894
|
+
const displayValue = value.length > 30 ? value.substring(0, 30) + "..." : value;
|
|
1895
|
+
issues.push({
|
|
1896
|
+
ruleId: "logic/duplicate-string",
|
|
1897
|
+
severity: "low",
|
|
1898
|
+
category: "logic",
|
|
1899
|
+
file: context.filePath,
|
|
1900
|
+
startLine: firstLine,
|
|
1901
|
+
endLine: firstLine,
|
|
1902
|
+
message: t(
|
|
1903
|
+
`String "${displayValue}" is repeated ${locations.length} times.`,
|
|
1904
|
+
`\u5B57\u7B26\u4E32 "${displayValue}" \u91CD\u590D\u51FA\u73B0\u4E86 ${locations.length} \u6B21\u3002`
|
|
1905
|
+
),
|
|
1906
|
+
suggestion: t(
|
|
1907
|
+
`Extract to a named constant to improve maintainability.`,
|
|
1908
|
+
`\u63D0\u53D6\u4E3A\u547D\u540D\u5E38\u91CF\u4EE5\u63D0\u9AD8\u53EF\u7EF4\u62A4\u6027\u3002`
|
|
1909
|
+
)
|
|
1910
|
+
});
|
|
1911
|
+
}
|
|
1912
|
+
return issues;
|
|
1913
|
+
}
|
|
1914
|
+
};
|
|
1915
|
+
|
|
1916
|
+
// src/rules/builtin/no-debugger.ts
|
|
1917
|
+
var noDebuggerRule = {
|
|
1918
|
+
id: "security/no-debugger",
|
|
1919
|
+
category: "security",
|
|
1920
|
+
severity: "high",
|
|
1921
|
+
title: "Debugger statement",
|
|
1922
|
+
description: "Debugger statements should never be committed to production code.",
|
|
1923
|
+
check(context) {
|
|
1924
|
+
const issues = [];
|
|
1925
|
+
const lines = context.fileContent.split("\n");
|
|
1926
|
+
let inBlockComment = false;
|
|
1927
|
+
for (let i = 0; i < lines.length; i++) {
|
|
1928
|
+
const line = lines[i];
|
|
1929
|
+
const trimmed = line.trim();
|
|
1930
|
+
if (inBlockComment) {
|
|
1931
|
+
if (trimmed.includes("*/")) inBlockComment = false;
|
|
1932
|
+
continue;
|
|
1933
|
+
}
|
|
1934
|
+
if (trimmed.startsWith("/*")) {
|
|
1935
|
+
if (!trimmed.includes("*/")) inBlockComment = true;
|
|
1936
|
+
continue;
|
|
1937
|
+
}
|
|
1938
|
+
if (trimmed.startsWith("//")) continue;
|
|
1939
|
+
const cleaned = line.replace(/(['"`])(?:(?!\1|\\).|\\.)*\1/g, '""').replace(/\/\/.*$/, "");
|
|
1940
|
+
if (/\bdebugger\b/.test(cleaned)) {
|
|
1941
|
+
issues.push({
|
|
1942
|
+
ruleId: "security/no-debugger",
|
|
1943
|
+
severity: "high",
|
|
1944
|
+
category: "security",
|
|
1945
|
+
file: context.filePath,
|
|
1946
|
+
startLine: i + 1,
|
|
1947
|
+
endLine: i + 1,
|
|
1948
|
+
message: t(
|
|
1949
|
+
`Debugger statement found. Remove before committing.`,
|
|
1950
|
+
`\u53D1\u73B0 debugger \u8BED\u53E5\u3002\u63D0\u4EA4\u524D\u8BF7\u79FB\u9664\u3002`
|
|
1951
|
+
),
|
|
1952
|
+
suggestion: t(
|
|
1953
|
+
`Remove the debugger statement.`,
|
|
1954
|
+
`\u79FB\u9664 debugger \u8BED\u53E5\u3002`
|
|
1955
|
+
)
|
|
1956
|
+
});
|
|
1957
|
+
}
|
|
1958
|
+
}
|
|
1959
|
+
return issues;
|
|
1960
|
+
}
|
|
1961
|
+
};
|
|
1962
|
+
|
|
1963
|
+
// src/rules/builtin/no-non-null-assertion.ts
|
|
1964
|
+
var noNonNullAssertionRule = {
|
|
1965
|
+
id: "logic/no-non-null-assertion",
|
|
1966
|
+
category: "logic",
|
|
1967
|
+
severity: "medium",
|
|
1968
|
+
title: "Non-null assertion operator",
|
|
1969
|
+
description: "AI-generated code often uses the ! operator to bypass TypeScript null checks, risking runtime crashes.",
|
|
1970
|
+
check(context) {
|
|
1971
|
+
const issues = [];
|
|
1972
|
+
if (!context.filePath.match(/\.tsx?$/)) return issues;
|
|
1973
|
+
let ast;
|
|
1974
|
+
try {
|
|
1975
|
+
const parsed = parseCode(context.fileContent, context.filePath);
|
|
1976
|
+
ast = parsed.ast;
|
|
1977
|
+
} catch {
|
|
1978
|
+
return issues;
|
|
1979
|
+
}
|
|
1980
|
+
walkAST(ast, (node) => {
|
|
1981
|
+
if (node.type !== AST_NODE_TYPES.TSNonNullExpression) return;
|
|
1982
|
+
const line = node.loc?.start.line ?? 0;
|
|
1983
|
+
if (line === 0) return;
|
|
1984
|
+
issues.push({
|
|
1985
|
+
ruleId: "logic/no-non-null-assertion",
|
|
1986
|
+
severity: "medium",
|
|
1987
|
+
category: "logic",
|
|
1988
|
+
file: context.filePath,
|
|
1989
|
+
startLine: line,
|
|
1990
|
+
endLine: line,
|
|
1991
|
+
message: t(
|
|
1992
|
+
`Non-null assertion (!) used \u2014 value could be null/undefined at runtime.`,
|
|
1993
|
+
`\u4F7F\u7528\u4E86\u975E\u7A7A\u65AD\u8A00 (!) \u2014 \u503C\u5728\u8FD0\u884C\u65F6\u53EF\u80FD\u4E3A null/undefined\u3002`
|
|
1994
|
+
),
|
|
1995
|
+
suggestion: t(
|
|
1996
|
+
`Use optional chaining (?.), nullish coalescing (??), or add a proper null check.`,
|
|
1997
|
+
`\u4F7F\u7528\u53EF\u9009\u94FE (?.)\u3001\u7A7A\u503C\u5408\u5E76 (??) \u6216\u6DFB\u52A0\u9002\u5F53\u7684\u7A7A\u503C\u68C0\u67E5\u3002`
|
|
1998
|
+
)
|
|
1999
|
+
});
|
|
2000
|
+
});
|
|
2001
|
+
return issues;
|
|
2002
|
+
}
|
|
2003
|
+
};
|
|
2004
|
+
|
|
2005
|
+
// src/rules/builtin/no-self-compare.ts
|
|
2006
|
+
var noSelfCompareRule = {
|
|
2007
|
+
id: "logic/no-self-compare",
|
|
2008
|
+
category: "logic",
|
|
2009
|
+
severity: "medium",
|
|
2010
|
+
title: "Self-comparison",
|
|
2011
|
+
description: "Self-comparison (x === x) is always true (or always false for !==). Use Number.isNaN() for NaN checks.",
|
|
2012
|
+
check(context) {
|
|
2013
|
+
const issues = [];
|
|
2014
|
+
let ast;
|
|
2015
|
+
try {
|
|
2016
|
+
const parsed = parseCode(context.fileContent, context.filePath);
|
|
2017
|
+
ast = parsed.ast;
|
|
2018
|
+
} catch {
|
|
2019
|
+
return issues;
|
|
2020
|
+
}
|
|
2021
|
+
walkAST(ast, (node) => {
|
|
2022
|
+
if (node.type !== AST_NODE_TYPES.BinaryExpression) return;
|
|
2023
|
+
const binExpr = node;
|
|
2024
|
+
const op = binExpr.operator;
|
|
2025
|
+
if (!["===", "!==", "==", "!="].includes(op)) return;
|
|
2026
|
+
const left = serializeNode(binExpr.left);
|
|
2027
|
+
const right = serializeNode(binExpr.right);
|
|
2028
|
+
if (left === null || right === null || left !== right) return;
|
|
2029
|
+
const line = node.loc?.start.line ?? 0;
|
|
2030
|
+
if (line === 0) return;
|
|
2031
|
+
issues.push({
|
|
2032
|
+
ruleId: "logic/no-self-compare",
|
|
2033
|
+
severity: "medium",
|
|
2034
|
+
category: "logic",
|
|
2035
|
+
file: context.filePath,
|
|
2036
|
+
startLine: line,
|
|
2037
|
+
endLine: line,
|
|
2038
|
+
message: t(
|
|
2039
|
+
`Self-comparison "${left} ${op} ${right}" is always ${op.includes("!") ? "false" : "true"}.`,
|
|
2040
|
+
`\u81EA\u6BD4\u8F83 "${left} ${op} ${right}" \u59CB\u7EC8\u4E3A ${op.includes("!") ? "false" : "true"}\u3002`
|
|
2041
|
+
),
|
|
2042
|
+
suggestion: t(
|
|
2043
|
+
`If checking for NaN, use Number.isNaN(${left}) instead.`,
|
|
2044
|
+
`\u5982\u9700\u68C0\u67E5 NaN\uFF0C\u8BF7\u4F7F\u7528 Number.isNaN(${left})\u3002`
|
|
2045
|
+
)
|
|
2046
|
+
});
|
|
2047
|
+
});
|
|
2048
|
+
return issues;
|
|
2049
|
+
}
|
|
2050
|
+
};
|
|
2051
|
+
function serializeNode(node) {
|
|
2052
|
+
if (node.type === AST_NODE_TYPES.Identifier) {
|
|
2053
|
+
return node.name;
|
|
2054
|
+
}
|
|
2055
|
+
if (node.type === AST_NODE_TYPES.MemberExpression && !node.computed) {
|
|
2056
|
+
const obj = serializeNode(node.object);
|
|
2057
|
+
const prop = node.property.name;
|
|
2058
|
+
if (obj && prop) return `${obj}.${prop}`;
|
|
2059
|
+
}
|
|
2060
|
+
return null;
|
|
2061
|
+
}
|
|
2062
|
+
|
|
2063
|
+
// src/rules/builtin/no-return-assign.ts
|
|
2064
|
+
var noReturnAssignRule = {
|
|
2065
|
+
id: "logic/no-return-assign",
|
|
2066
|
+
category: "logic",
|
|
2067
|
+
severity: "medium",
|
|
2068
|
+
title: "Assignment in return statement",
|
|
2069
|
+
description: "AI-generated code sometimes uses assignment (=) instead of comparison (===) in return statements.",
|
|
2070
|
+
check(context) {
|
|
2071
|
+
const issues = [];
|
|
2072
|
+
let ast;
|
|
2073
|
+
try {
|
|
2074
|
+
const parsed = parseCode(context.fileContent, context.filePath);
|
|
2075
|
+
ast = parsed.ast;
|
|
2076
|
+
} catch {
|
|
2077
|
+
return issues;
|
|
2078
|
+
}
|
|
2079
|
+
walkAST(ast, (node) => {
|
|
2080
|
+
if (node.type !== AST_NODE_TYPES.ReturnStatement) return;
|
|
2081
|
+
const returnStmt = node;
|
|
2082
|
+
if (!returnStmt.argument) return;
|
|
2083
|
+
if (returnStmt.argument.type === AST_NODE_TYPES.AssignmentExpression) {
|
|
2084
|
+
const assignExpr = returnStmt.argument;
|
|
2085
|
+
if (assignExpr.operator !== "=") return;
|
|
2086
|
+
const line = node.loc?.start.line ?? 0;
|
|
2087
|
+
if (line === 0) return;
|
|
2088
|
+
issues.push({
|
|
2089
|
+
ruleId: "logic/no-return-assign",
|
|
2090
|
+
severity: "medium",
|
|
2091
|
+
category: "logic",
|
|
2092
|
+
file: context.filePath,
|
|
2093
|
+
startLine: line,
|
|
2094
|
+
endLine: line,
|
|
2095
|
+
message: t(
|
|
2096
|
+
`Assignment in return statement \u2014 did you mean to use === instead of =?`,
|
|
2097
|
+
`return \u8BED\u53E5\u4E2D\u4F7F\u7528\u4E86\u8D4B\u503C \u2014 \u662F\u5426\u5E94\u8BE5\u4F7F\u7528 === \u800C\u975E =\uFF1F`
|
|
2098
|
+
),
|
|
2099
|
+
suggestion: t(
|
|
2100
|
+
`If comparison was intended, use === instead of =. If assignment is intentional, extract it to a separate line.`,
|
|
2101
|
+
`\u5982\u679C\u610F\u56FE\u662F\u6BD4\u8F83\uFF0C\u8BF7\u4F7F\u7528 === \u4EE3\u66FF =\u3002\u5982\u679C\u786E\u5B9E\u9700\u8981\u8D4B\u503C\uFF0C\u8BF7\u63D0\u53D6\u5230\u5355\u72EC\u7684\u884C\u3002`
|
|
2102
|
+
)
|
|
2103
|
+
});
|
|
2104
|
+
}
|
|
2105
|
+
});
|
|
2106
|
+
return issues;
|
|
2107
|
+
}
|
|
2108
|
+
};
|
|
2109
|
+
|
|
2110
|
+
// src/rules/builtin/promise-void.ts
|
|
2111
|
+
var promiseVoidRule = {
|
|
2112
|
+
id: "logic/promise-void",
|
|
2113
|
+
category: "logic",
|
|
2114
|
+
severity: "medium",
|
|
2115
|
+
title: "Floating promise (not awaited or returned)",
|
|
2116
|
+
description: "AI-generated code often calls async functions without await, causing unhandled rejections.",
|
|
2117
|
+
check(context) {
|
|
2118
|
+
const issues = [];
|
|
2119
|
+
let ast;
|
|
2120
|
+
try {
|
|
2121
|
+
const parsed = parseCode(context.fileContent, context.filePath);
|
|
2122
|
+
ast = parsed.ast;
|
|
2123
|
+
} catch {
|
|
2124
|
+
return issues;
|
|
2125
|
+
}
|
|
2126
|
+
const asyncFnNames = /* @__PURE__ */ new Set();
|
|
2127
|
+
walkAST(ast, (node) => {
|
|
2128
|
+
if (node.type === AST_NODE_TYPES.FunctionDeclaration && node.async && node.id) {
|
|
2129
|
+
asyncFnNames.add(node.id.name);
|
|
2130
|
+
}
|
|
2131
|
+
if (node.type === AST_NODE_TYPES.VariableDeclarator && node.id.type === AST_NODE_TYPES.Identifier) {
|
|
2132
|
+
const init = node.init;
|
|
2133
|
+
if (init && (init.type === AST_NODE_TYPES.ArrowFunctionExpression || init.type === AST_NODE_TYPES.FunctionExpression) && init.async) {
|
|
2134
|
+
asyncFnNames.add(
|
|
2135
|
+
node.id.name
|
|
2136
|
+
);
|
|
2137
|
+
}
|
|
2138
|
+
}
|
|
2139
|
+
});
|
|
2140
|
+
const commonAsyncPatterns = [
|
|
2141
|
+
/^fetch$/,
|
|
2142
|
+
/^save/,
|
|
2143
|
+
/^load/,
|
|
2144
|
+
/^send/,
|
|
2145
|
+
/^delete/,
|
|
2146
|
+
/^update/,
|
|
2147
|
+
/^create/,
|
|
2148
|
+
/^connect/,
|
|
2149
|
+
/^disconnect/,
|
|
2150
|
+
/^init/
|
|
2151
|
+
];
|
|
2152
|
+
walkAST(ast, (node) => {
|
|
2153
|
+
if (node.type !== AST_NODE_TYPES.ExpressionStatement) return;
|
|
2154
|
+
const expr = node.expression;
|
|
2155
|
+
if (expr.type !== AST_NODE_TYPES.CallExpression) return;
|
|
2156
|
+
const callExpr = expr;
|
|
2157
|
+
const fnName = getCallName2(callExpr);
|
|
2158
|
+
if (!fnName) return;
|
|
2159
|
+
const isKnownAsync = asyncFnNames.has(fnName);
|
|
2160
|
+
const matchesPattern = commonAsyncPatterns.some((p) => p.test(fnName));
|
|
2161
|
+
const endsWithAsync = fnName.endsWith("Async") || fnName.endsWith("async");
|
|
2162
|
+
if (!isKnownAsync && !matchesPattern && !endsWithAsync) return;
|
|
2163
|
+
const line = node.loc?.start.line ?? 0;
|
|
2164
|
+
if (line === 0) return;
|
|
2165
|
+
issues.push({
|
|
2166
|
+
ruleId: "logic/promise-void",
|
|
2167
|
+
severity: "medium",
|
|
2168
|
+
category: "logic",
|
|
2169
|
+
file: context.filePath,
|
|
2170
|
+
startLine: line,
|
|
2171
|
+
endLine: node.loc?.end.line ?? line,
|
|
2172
|
+
message: t(
|
|
2173
|
+
`Call to "${fnName}()" appears to be a floating promise (not awaited or returned).`,
|
|
2174
|
+
`\u8C03\u7528 "${fnName}()" \u7591\u4F3C\u6D6E\u52A8 Promise\uFF08\u672A await \u6216 return\uFF09\u3002`
|
|
2175
|
+
),
|
|
2176
|
+
suggestion: t(
|
|
2177
|
+
`Add "await" before the call, assign the result, or use "void ${fnName}()" to explicitly discard.`,
|
|
2178
|
+
`\u5728\u8C03\u7528\u524D\u6DFB\u52A0 "await"\uFF0C\u8D4B\u503C\u7ED9\u53D8\u91CF\uFF0C\u6216\u4F7F\u7528 "void ${fnName}()" \u663E\u5F0F\u4E22\u5F03\u3002`
|
|
2179
|
+
)
|
|
2180
|
+
});
|
|
2181
|
+
});
|
|
2182
|
+
return issues;
|
|
2183
|
+
}
|
|
2184
|
+
};
|
|
2185
|
+
function getCallName2(callExpr) {
|
|
2186
|
+
const callee = callExpr.callee;
|
|
2187
|
+
if (callee.type === AST_NODE_TYPES.Identifier) {
|
|
2188
|
+
return callee.name;
|
|
2189
|
+
}
|
|
2190
|
+
if (callee.type === AST_NODE_TYPES.MemberExpression && !callee.computed && callee.property.type === AST_NODE_TYPES.Identifier) {
|
|
2191
|
+
return callee.property.name;
|
|
2192
|
+
}
|
|
2193
|
+
return null;
|
|
2194
|
+
}
|
|
2195
|
+
|
|
2196
|
+
// src/rules/builtin/no-reassign-param.ts
|
|
2197
|
+
var noReassignParamRule = {
|
|
2198
|
+
id: "logic/no-reassign-param",
|
|
2199
|
+
category: "logic",
|
|
2200
|
+
severity: "low",
|
|
2201
|
+
title: "Parameter reassignment",
|
|
2202
|
+
description: "AI-generated code often reassigns function parameters, creating confusing side-effect patterns.",
|
|
2203
|
+
check(context) {
|
|
2204
|
+
const issues = [];
|
|
2205
|
+
let ast;
|
|
2206
|
+
try {
|
|
2207
|
+
const parsed = parseCode(context.fileContent, context.filePath);
|
|
2208
|
+
ast = parsed.ast;
|
|
2209
|
+
} catch {
|
|
2210
|
+
return issues;
|
|
2211
|
+
}
|
|
2212
|
+
walkAST(ast, (node) => {
|
|
2213
|
+
const isFn = node.type === AST_NODE_TYPES.FunctionDeclaration || node.type === AST_NODE_TYPES.FunctionExpression || node.type === AST_NODE_TYPES.ArrowFunctionExpression || node.type === AST_NODE_TYPES.MethodDefinition;
|
|
2214
|
+
if (!isFn) return;
|
|
2215
|
+
let params = [];
|
|
2216
|
+
if (node.type === AST_NODE_TYPES.MethodDefinition) {
|
|
2217
|
+
const method = node;
|
|
2218
|
+
if (method.value && "params" in method.value) {
|
|
2219
|
+
params = method.value.params;
|
|
2220
|
+
}
|
|
2221
|
+
} else {
|
|
2222
|
+
params = node.params;
|
|
2223
|
+
}
|
|
2224
|
+
const paramNames = /* @__PURE__ */ new Set();
|
|
2225
|
+
for (const param of params) {
|
|
2226
|
+
if (param.type === AST_NODE_TYPES.Identifier) {
|
|
2227
|
+
paramNames.add(param.name);
|
|
2228
|
+
}
|
|
2229
|
+
if (param.type === AST_NODE_TYPES.AssignmentPattern) {
|
|
2230
|
+
const left = param.left;
|
|
2231
|
+
if (left.type === AST_NODE_TYPES.Identifier) {
|
|
2232
|
+
paramNames.add(left.name);
|
|
2233
|
+
}
|
|
2234
|
+
}
|
|
2235
|
+
}
|
|
2236
|
+
if (paramNames.size === 0) return;
|
|
2237
|
+
let body = null;
|
|
2238
|
+
if (node.type === AST_NODE_TYPES.MethodDefinition) {
|
|
2239
|
+
body = node.value;
|
|
2240
|
+
} else {
|
|
2241
|
+
body = node;
|
|
2242
|
+
}
|
|
2243
|
+
if (!body || !("body" in body)) return;
|
|
2244
|
+
const fnBody = body.body;
|
|
2245
|
+
if (!fnBody) return;
|
|
2246
|
+
const reportedParams = /* @__PURE__ */ new Set();
|
|
2247
|
+
walkAST(fnBody, (innerNode) => {
|
|
2248
|
+
if (innerNode.type !== AST_NODE_TYPES.AssignmentExpression) return;
|
|
2249
|
+
const assignExpr = innerNode;
|
|
2250
|
+
if (assignExpr.left.type !== AST_NODE_TYPES.Identifier) return;
|
|
2251
|
+
const name = assignExpr.left.name;
|
|
2252
|
+
if (!paramNames.has(name) || reportedParams.has(name)) return;
|
|
2253
|
+
reportedParams.add(name);
|
|
2254
|
+
const line = innerNode.loc?.start.line ?? 0;
|
|
2255
|
+
if (line === 0) return;
|
|
2256
|
+
issues.push({
|
|
2257
|
+
ruleId: "logic/no-reassign-param",
|
|
2258
|
+
severity: "low",
|
|
2259
|
+
category: "logic",
|
|
2260
|
+
file: context.filePath,
|
|
2261
|
+
startLine: line,
|
|
2262
|
+
endLine: line,
|
|
2263
|
+
message: t(
|
|
2264
|
+
`Parameter "${name}" is reassigned. This can cause confusion and lose the original value.`,
|
|
2265
|
+
`\u53C2\u6570 "${name}" \u88AB\u91CD\u65B0\u8D4B\u503C\u3002\u8FD9\u53EF\u80FD\u9020\u6210\u6DF7\u6DC6\u5E76\u4E22\u5931\u539F\u59CB\u503C\u3002`
|
|
2266
|
+
),
|
|
2267
|
+
suggestion: t(
|
|
2268
|
+
`Use a local variable instead: const local${name.charAt(0).toUpperCase() + name.slice(1)} = ...`,
|
|
2269
|
+
`\u4F7F\u7528\u5C40\u90E8\u53D8\u91CF\u4EE3\u66FF\uFF1Aconst local${name.charAt(0).toUpperCase() + name.slice(1)} = ...`
|
|
2270
|
+
)
|
|
2271
|
+
});
|
|
2272
|
+
});
|
|
2273
|
+
});
|
|
2274
|
+
return issues;
|
|
2275
|
+
}
|
|
2276
|
+
};
|
|
2277
|
+
|
|
2278
|
+
// src/rules/builtin/no-async-without-await.ts
|
|
2279
|
+
var noAsyncWithoutAwaitRule = {
|
|
2280
|
+
id: "logic/no-async-without-await",
|
|
2281
|
+
category: "logic",
|
|
2282
|
+
severity: "low",
|
|
2283
|
+
title: "Async function without await",
|
|
2284
|
+
description: "AI-generated code often marks functions as async without using await, adding unnecessary Promise wrapping.",
|
|
2285
|
+
check(context) {
|
|
2286
|
+
const issues = [];
|
|
2287
|
+
let ast;
|
|
2288
|
+
try {
|
|
2289
|
+
const parsed = parseCode(context.fileContent, context.filePath);
|
|
2290
|
+
ast = parsed.ast;
|
|
2291
|
+
} catch {
|
|
2292
|
+
return issues;
|
|
2293
|
+
}
|
|
2294
|
+
walkAST(ast, (node) => {
|
|
2295
|
+
const isAsyncFn = (node.type === AST_NODE_TYPES.FunctionDeclaration || node.type === AST_NODE_TYPES.FunctionExpression || node.type === AST_NODE_TYPES.ArrowFunctionExpression) && node.async;
|
|
2296
|
+
if (!isAsyncFn) return;
|
|
2297
|
+
const fnNode = node;
|
|
2298
|
+
const body = fnNode.body;
|
|
2299
|
+
if (!body) return;
|
|
2300
|
+
let hasAwait = false;
|
|
2301
|
+
walkAST(body, (innerNode) => {
|
|
2302
|
+
if (innerNode !== body && (innerNode.type === AST_NODE_TYPES.FunctionDeclaration || innerNode.type === AST_NODE_TYPES.FunctionExpression || innerNode.type === AST_NODE_TYPES.ArrowFunctionExpression) && innerNode.async) {
|
|
2303
|
+
return false;
|
|
2304
|
+
}
|
|
2305
|
+
if (innerNode.type === AST_NODE_TYPES.AwaitExpression) {
|
|
2306
|
+
hasAwait = true;
|
|
2307
|
+
return false;
|
|
2308
|
+
}
|
|
2309
|
+
if (innerNode.type === AST_NODE_TYPES.ForOfStatement && innerNode.await) {
|
|
2310
|
+
hasAwait = true;
|
|
2311
|
+
return false;
|
|
2312
|
+
}
|
|
2313
|
+
return;
|
|
2314
|
+
});
|
|
2315
|
+
if (hasAwait) return false;
|
|
2316
|
+
const line = node.loc?.start.line ?? 0;
|
|
2317
|
+
if (line === 0) return;
|
|
2318
|
+
let fnName = "<anonymous>";
|
|
2319
|
+
if (fnNode.type === AST_NODE_TYPES.FunctionDeclaration && fnNode.id) {
|
|
2320
|
+
fnName = fnNode.id.name;
|
|
2321
|
+
}
|
|
2322
|
+
issues.push({
|
|
2323
|
+
ruleId: "logic/no-async-without-await",
|
|
2324
|
+
severity: "low",
|
|
2325
|
+
category: "logic",
|
|
2326
|
+
file: context.filePath,
|
|
2327
|
+
startLine: line,
|
|
2328
|
+
endLine: node.loc?.end.line ?? line,
|
|
2329
|
+
message: t(
|
|
2330
|
+
`Async function "${fnName}" does not use await.`,
|
|
2331
|
+
`\u5F02\u6B65\u51FD\u6570 "${fnName}" \u5185\u90E8\u6CA1\u6709\u4F7F\u7528 await\u3002`
|
|
2332
|
+
),
|
|
2333
|
+
suggestion: t(
|
|
2334
|
+
`Remove the async keyword if this function doesn't need to be asynchronous.`,
|
|
2335
|
+
`\u5982\u679C\u6B64\u51FD\u6570\u4E0D\u9700\u8981\u5F02\u6B65\u884C\u4E3A\uFF0C\u8BF7\u79FB\u9664 async \u5173\u952E\u5B57\u3002`
|
|
2336
|
+
)
|
|
2337
|
+
});
|
|
2338
|
+
return false;
|
|
2339
|
+
});
|
|
2340
|
+
return issues;
|
|
2341
|
+
}
|
|
2342
|
+
};
|
|
2343
|
+
|
|
2344
|
+
// src/rules/builtin/no-useless-constructor.ts
|
|
2345
|
+
var noUselessConstructorRule = {
|
|
2346
|
+
id: "logic/no-useless-constructor",
|
|
2347
|
+
category: "logic",
|
|
2348
|
+
severity: "low",
|
|
2349
|
+
title: "Useless constructor",
|
|
2350
|
+
description: "AI-generated code often produces constructors that only call super() or are completely empty.",
|
|
2351
|
+
check(context) {
|
|
2352
|
+
const issues = [];
|
|
2353
|
+
let ast;
|
|
2354
|
+
try {
|
|
2355
|
+
const parsed = parseCode(context.fileContent, context.filePath);
|
|
2356
|
+
ast = parsed.ast;
|
|
2357
|
+
} catch {
|
|
2358
|
+
return issues;
|
|
2359
|
+
}
|
|
2360
|
+
walkAST(ast, (node) => {
|
|
2361
|
+
if (node.type !== AST_NODE_TYPES.ClassDeclaration && node.type !== AST_NODE_TYPES.ClassExpression) return;
|
|
2362
|
+
const classNode = node;
|
|
2363
|
+
const hasSuper = classNode.superClass !== null && classNode.superClass !== void 0;
|
|
2364
|
+
const constructor = classNode.body.body.find(
|
|
2365
|
+
(member) => member.type === AST_NODE_TYPES.MethodDefinition && member.kind === "constructor"
|
|
2366
|
+
);
|
|
2367
|
+
if (!constructor) return;
|
|
2368
|
+
const ctorValue = constructor.value;
|
|
2369
|
+
if (!ctorValue.body) return;
|
|
2370
|
+
const bodyStatements = ctorValue.body.body;
|
|
2371
|
+
if (bodyStatements.length === 0) {
|
|
2372
|
+
const line = constructor.loc?.start.line ?? 0;
|
|
2373
|
+
if (line === 0) return;
|
|
2374
|
+
issues.push({
|
|
2375
|
+
ruleId: "logic/no-useless-constructor",
|
|
2376
|
+
severity: "low",
|
|
2377
|
+
category: "logic",
|
|
2378
|
+
file: context.filePath,
|
|
2379
|
+
startLine: line,
|
|
2380
|
+
endLine: constructor.loc?.end.line ?? line,
|
|
2381
|
+
message: t(
|
|
2382
|
+
`Empty constructor is unnecessary.`,
|
|
2383
|
+
`\u7A7A\u6784\u9020\u51FD\u6570\u662F\u4E0D\u5FC5\u8981\u7684\u3002`
|
|
2384
|
+
),
|
|
2385
|
+
suggestion: t(
|
|
2386
|
+
`Remove the empty constructor \u2014 JavaScript provides a default one.`,
|
|
2387
|
+
`\u79FB\u9664\u7A7A\u6784\u9020\u51FD\u6570 \u2014 JavaScript \u4F1A\u81EA\u52A8\u63D0\u4F9B\u9ED8\u8BA4\u6784\u9020\u51FD\u6570\u3002`
|
|
2388
|
+
)
|
|
2389
|
+
});
|
|
2390
|
+
return;
|
|
2391
|
+
}
|
|
2392
|
+
if (hasSuper && bodyStatements.length === 1) {
|
|
2393
|
+
const stmt = bodyStatements[0];
|
|
2394
|
+
if (stmt.type !== AST_NODE_TYPES.ExpressionStatement) return;
|
|
2395
|
+
const expr = stmt.expression;
|
|
2396
|
+
if (expr.type !== AST_NODE_TYPES.CallExpression) return;
|
|
2397
|
+
if (expr.callee.type !== AST_NODE_TYPES.Super) return;
|
|
2398
|
+
const ctorParams = ctorValue.params;
|
|
2399
|
+
const superArgs = expr.arguments;
|
|
2400
|
+
if (ctorParams.length === superArgs.length) {
|
|
2401
|
+
let allMatch = true;
|
|
2402
|
+
for (let i = 0; i < ctorParams.length; i++) {
|
|
2403
|
+
const param = ctorParams[i];
|
|
2404
|
+
const arg = superArgs[i];
|
|
2405
|
+
if (param.type === AST_NODE_TYPES.Identifier && arg.type === AST_NODE_TYPES.Identifier && param.name === arg.name) {
|
|
2406
|
+
continue;
|
|
2407
|
+
}
|
|
2408
|
+
if (param.type === AST_NODE_TYPES.TSParameterProperty && param.parameter.type === AST_NODE_TYPES.Identifier) {
|
|
2409
|
+
allMatch = false;
|
|
2410
|
+
break;
|
|
2411
|
+
}
|
|
2412
|
+
allMatch = false;
|
|
2413
|
+
break;
|
|
2414
|
+
}
|
|
2415
|
+
if (allMatch) {
|
|
2416
|
+
const line = constructor.loc?.start.line ?? 0;
|
|
2417
|
+
if (line === 0) return;
|
|
2418
|
+
issues.push({
|
|
2419
|
+
ruleId: "logic/no-useless-constructor",
|
|
2420
|
+
severity: "low",
|
|
2421
|
+
category: "logic",
|
|
2422
|
+
file: context.filePath,
|
|
2423
|
+
startLine: line,
|
|
2424
|
+
endLine: constructor.loc?.end.line ?? line,
|
|
2425
|
+
message: t(
|
|
2426
|
+
`Constructor only calls super() with the same arguments \u2014 it is unnecessary.`,
|
|
2427
|
+
`\u6784\u9020\u51FD\u6570\u4EC5\u8C03\u7528 super() \u5E76\u4F20\u9012\u76F8\u540C\u53C2\u6570 \u2014 \u8FD9\u662F\u4E0D\u5FC5\u8981\u7684\u3002`
|
|
2428
|
+
),
|
|
2429
|
+
suggestion: t(
|
|
2430
|
+
`Remove the constructor \u2014 JavaScript automatically calls super() with the same arguments.`,
|
|
2431
|
+
`\u79FB\u9664\u6784\u9020\u51FD\u6570 \u2014 JavaScript \u4F1A\u81EA\u52A8\u7528\u76F8\u540C\u53C2\u6570\u8C03\u7528 super()\u3002`
|
|
2432
|
+
)
|
|
2433
|
+
});
|
|
2434
|
+
}
|
|
2435
|
+
}
|
|
2436
|
+
}
|
|
2437
|
+
});
|
|
2438
|
+
return issues;
|
|
2439
|
+
}
|
|
2440
|
+
};
|
|
2441
|
+
|
|
1608
2442
|
// src/rules/engine.ts
|
|
1609
2443
|
var BUILTIN_RULES = [
|
|
1610
2444
|
unnecessaryTryCatchRule,
|
|
@@ -1619,7 +2453,20 @@ var BUILTIN_RULES = [
|
|
|
1619
2453
|
consoleInCodeRule,
|
|
1620
2454
|
phantomImportRule,
|
|
1621
2455
|
unusedImportRule,
|
|
1622
|
-
missingAwaitRule
|
|
2456
|
+
missingAwaitRule,
|
|
2457
|
+
anyTypeAbuseRule,
|
|
2458
|
+
typeCoercionRule,
|
|
2459
|
+
magicNumberRule,
|
|
2460
|
+
nestedTernaryRule,
|
|
2461
|
+
duplicateStringRule,
|
|
2462
|
+
noDebuggerRule,
|
|
2463
|
+
noNonNullAssertionRule,
|
|
2464
|
+
noSelfCompareRule,
|
|
2465
|
+
noReturnAssignRule,
|
|
2466
|
+
promiseVoidRule,
|
|
2467
|
+
noReassignParamRule,
|
|
2468
|
+
noAsyncWithoutAwaitRule,
|
|
2469
|
+
noUselessConstructorRule
|
|
1623
2470
|
];
|
|
1624
2471
|
var RuleEngine = class {
|
|
1625
2472
|
rules;
|