periderm-cli 0.1.57 → 0.1.59
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/dist/scanner/checks.js +93 -0
- package/package.json +1 -1
package/dist/scanner/checks.js
CHANGED
|
@@ -982,6 +982,97 @@ const heavyLibImport = (source, file) => {
|
|
|
982
982
|
return out;
|
|
983
983
|
};
|
|
984
984
|
// ───────────────────────────────────────────────────────────────────────
|
|
985
|
+
// 29. Missing Error Property Typing Issue
|
|
986
|
+
// ───────────────────────────────────────────────────────────────────────
|
|
987
|
+
const missingErrorTyping = (source, file, ast) => {
|
|
988
|
+
const out = [];
|
|
989
|
+
if (!ast)
|
|
990
|
+
return out;
|
|
991
|
+
traverse(ast, {
|
|
992
|
+
CatchClause(p) {
|
|
993
|
+
if (!p.node.param || p.node.param.type !== "Identifier")
|
|
994
|
+
return;
|
|
995
|
+
const errorName = p.node.param.name;
|
|
996
|
+
const catchBodySrc = source.slice(p.node.body.start ?? 0, p.node.body.end ?? 0);
|
|
997
|
+
const propAccessRegex = new RegExp(`\\b${errorName}\\.(message|code|name|stack)\\b`);
|
|
998
|
+
if (propAccessRegex.test(catchBodySrc)) {
|
|
999
|
+
// Check if there is a type narrowing guard
|
|
1000
|
+
const hasGuard = new RegExp(`instanceof\\s+Error|typeof\\s+${errorName}\\s*===?\\s*['"]object['"]|${errorName}\\s+is\\s+|as\\s+Error`).test(catchBodySrc) || new RegExp(`\\b${errorName}\\s+instanceof\\b`).test(catchBodySrc) || /Error\s*\(/.test(catchBodySrc) || new RegExp(`isError\\s*\\(\\s*${errorName}\\s*\\)`).test(catchBodySrc);
|
|
1001
|
+
if (!hasGuard) {
|
|
1002
|
+
out.push(mkFinding({
|
|
1003
|
+
id: "missing-error-typing",
|
|
1004
|
+
category: "Data Integrity",
|
|
1005
|
+
severity: "medium",
|
|
1006
|
+
file, line: p.node.loc?.start.line ?? 1,
|
|
1007
|
+
message: `Error property accessed without type narrowing.`,
|
|
1008
|
+
why: "In TypeScript, caught errors are of type `unknown` or `any`. Accessing properties like `.message` directly can crash if the thrown value is a string or null.",
|
|
1009
|
+
fix: "Check if the error is an instance of Error (e.g. `if (e instanceof Error)`) before accessing its properties, or safely cast it.",
|
|
1010
|
+
aiPrompt: `In ${file} near line ${p.node.loc?.start.line ?? 1}, the catch block accesses properties on the caught error without type narrowing. Wrap the property access in an \`if (${errorName} instanceof Error)\` check.`,
|
|
1011
|
+
}));
|
|
1012
|
+
}
|
|
1013
|
+
}
|
|
1014
|
+
}
|
|
1015
|
+
});
|
|
1016
|
+
return out;
|
|
1017
|
+
};
|
|
1018
|
+
// ───────────────────────────────────────────────────────────────────────
|
|
1019
|
+
// 30. Unused Variables/Imports
|
|
1020
|
+
// ───────────────────────────────────────────────────────────────────────
|
|
1021
|
+
const unusedVariablesImports = (source, file, ast) => {
|
|
1022
|
+
const out = [];
|
|
1023
|
+
if (!ast || !/\.(tsx|jsx|ts|js|mjs|cjs)$/.test(file))
|
|
1024
|
+
return out;
|
|
1025
|
+
if (/\.d\.ts$/.test(file))
|
|
1026
|
+
return out;
|
|
1027
|
+
traverse(ast, {
|
|
1028
|
+
Program(p) {
|
|
1029
|
+
for (const [name, binding] of Object.entries(p.scope.bindings)) {
|
|
1030
|
+
if (!binding.referenced && binding.constant && binding.kind !== "param") {
|
|
1031
|
+
if (name.startsWith("_"))
|
|
1032
|
+
continue;
|
|
1033
|
+
if (name === "React")
|
|
1034
|
+
continue; // Ignore React import if unused
|
|
1035
|
+
// Ignore exports
|
|
1036
|
+
if (binding.path.parentPath?.isExportDeclaration() || binding.path.parentPath?.isExportDefaultDeclaration())
|
|
1037
|
+
continue;
|
|
1038
|
+
if (binding.path.isExportDeclaration() || binding.path.isExportDefaultDeclaration())
|
|
1039
|
+
continue;
|
|
1040
|
+
const typeLabel = binding.kind === "module" ? "import" : "variable";
|
|
1041
|
+
out.push(mkFinding({
|
|
1042
|
+
id: `unused-${typeLabel}`,
|
|
1043
|
+
category: "Polish & Embarrassment Risks",
|
|
1044
|
+
severity: "low",
|
|
1045
|
+
file, line: binding.identifier.loc?.start.line ?? 1,
|
|
1046
|
+
message: `Unused ${typeLabel}: '${name}'.`,
|
|
1047
|
+
why: "Unused variables and imports clutter the codebase, increase bundle size, and make code harder to read.",
|
|
1048
|
+
fix: `Remove the unused ${typeLabel}.`,
|
|
1049
|
+
aiPrompt: `In ${file} near line ${binding.identifier.loc?.start.line ?? 1}, remove the unused ${typeLabel} '${name}'.`,
|
|
1050
|
+
}));
|
|
1051
|
+
}
|
|
1052
|
+
}
|
|
1053
|
+
},
|
|
1054
|
+
Function(p) {
|
|
1055
|
+
for (const [name, binding] of Object.entries(p.scope.bindings)) {
|
|
1056
|
+
if (!binding.referenced && binding.kind !== "param") {
|
|
1057
|
+
if (name.startsWith("_"))
|
|
1058
|
+
continue;
|
|
1059
|
+
out.push(mkFinding({
|
|
1060
|
+
id: "unused-variable",
|
|
1061
|
+
category: "Polish & Embarrassment Risks",
|
|
1062
|
+
severity: "low",
|
|
1063
|
+
file, line: binding.identifier.loc?.start.line ?? 1,
|
|
1064
|
+
message: `Unused variable: '${name}'.`,
|
|
1065
|
+
why: "Unused variables clutter the codebase and make code harder to read.",
|
|
1066
|
+
fix: "Remove the unused variable.",
|
|
1067
|
+
aiPrompt: `In ${file} near line ${binding.identifier.loc?.start.line ?? 1}, remove the unused variable '${name}'.`,
|
|
1068
|
+
}));
|
|
1069
|
+
}
|
|
1070
|
+
}
|
|
1071
|
+
}
|
|
1072
|
+
});
|
|
1073
|
+
return out;
|
|
1074
|
+
};
|
|
1075
|
+
// ───────────────────────────────────────────────────────────────────────
|
|
985
1076
|
// Registry
|
|
986
1077
|
// ───────────────────────────────────────────────────────────────────────
|
|
987
1078
|
const CHECKS = [
|
|
@@ -1014,6 +1105,8 @@ const CHECKS = [
|
|
|
1014
1105
|
sqlStringConcat,
|
|
1015
1106
|
mysqlNoUtf8mb4,
|
|
1016
1107
|
heavyLibImport,
|
|
1108
|
+
missingErrorTyping,
|
|
1109
|
+
unusedVariablesImports,
|
|
1017
1110
|
];
|
|
1018
1111
|
export const PER_FILE_CHECK_COUNT = CHECKS.length;
|
|
1019
1112
|
export function runChecks(source, file, ast) {
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "periderm-cli",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.59",
|
|
4
4
|
"description": "Periderm CLI: A pre-launch checklist to catch user-losing bugs, legal liabilities, revenue leaks, runaway cloud bills and so many more before you ship.",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"preferGlobal": true,
|