opencode-code-simplifier 1.3.0 → 1.5.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 +15 -24
- package/dist/plugin.js +136 -54
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -4,16 +4,7 @@ Code simplifier plugin for [OpenCode](https://opencode.ai). Simplifies and refin
|
|
|
4
4
|
|
|
5
5
|
## Install
|
|
6
6
|
|
|
7
|
-
|
|
8
|
-
npm install opencode-code-simplifier --save-dev
|
|
9
|
-
```
|
|
10
|
-
|
|
11
|
-
This will automatically:
|
|
12
|
-
1. Copy `skills/code-simplifier/SKILL.md` to `.opencode/skills/`
|
|
13
|
-
2. Copy `rules/code-simplifier.md` to `.opencode/rules/`
|
|
14
|
-
3. Copy `commands/simplify.md` to `.opencode/commands/`
|
|
15
|
-
|
|
16
|
-
Then add to your `opencode.json`:
|
|
7
|
+
Add to your `opencode.json`:
|
|
17
8
|
|
|
18
9
|
```json
|
|
19
10
|
{
|
|
@@ -21,31 +12,31 @@ Then add to your `opencode.json`:
|
|
|
21
12
|
}
|
|
22
13
|
```
|
|
23
14
|
|
|
24
|
-
Restart OpenCode
|
|
25
|
-
|
|
26
|
-
## Features
|
|
27
|
-
|
|
28
|
-
- Reduces nesting and complexity
|
|
29
|
-
- Removes debug artifacts (unconditional console.log, debugger, commented code)
|
|
30
|
-
- Removes obvious comments
|
|
31
|
-
- Preserves conditional logging (if debug, if isDev, etc.)
|
|
32
|
-
- Converts nested ternaries to if/else chains
|
|
33
|
-
- Applies language-specific best practices
|
|
15
|
+
Restart OpenCode. The plugin will auto-create `skills/code-simplifier/SKILL.md` and `rules/code-simplifier.md` on first load.
|
|
34
16
|
|
|
35
17
|
## Usage
|
|
36
18
|
|
|
37
|
-
|
|
19
|
+
Ask OpenCode to simplify code using the tool:
|
|
38
20
|
|
|
39
21
|
```
|
|
40
|
-
/
|
|
22
|
+
Use simplify-code tool on path/to/file.js
|
|
41
23
|
```
|
|
42
24
|
|
|
43
|
-
|
|
25
|
+
Or just describe what you want:
|
|
44
26
|
|
|
45
27
|
```
|
|
46
|
-
|
|
28
|
+
Simplify the code in src/utils.js
|
|
47
29
|
```
|
|
48
30
|
|
|
31
|
+
## Features
|
|
32
|
+
|
|
33
|
+
- Reduces nesting and complexity
|
|
34
|
+
- Removes debug artifacts (unconditional console.log, debugger, commented code)
|
|
35
|
+
- Removes obvious comments
|
|
36
|
+
- Preserves conditional logging (if debug, if isDev, etc.)
|
|
37
|
+
- Converts nested ternaries to if/else chains
|
|
38
|
+
- Applies language-specific best practices
|
|
39
|
+
|
|
49
40
|
## What Gets Removed
|
|
50
41
|
|
|
51
42
|
| Type | Action |
|
package/dist/plugin.js
CHANGED
|
@@ -12334,8 +12334,8 @@ function tool(input) {
|
|
|
12334
12334
|
}
|
|
12335
12335
|
tool.schema = exports_external;
|
|
12336
12336
|
// plugin.ts
|
|
12337
|
-
import { existsSync, mkdirSync, writeFileSync } from "fs";
|
|
12338
|
-
import { join } from "path";
|
|
12337
|
+
import { existsSync, mkdirSync, writeFileSync, readFileSync } from "fs";
|
|
12338
|
+
import { join, extname } from "path";
|
|
12339
12339
|
import { homedir } from "os";
|
|
12340
12340
|
var SKILL_CONTENT = `---
|
|
12341
12341
|
name: code-simplifier
|
|
@@ -12399,7 +12399,7 @@ Simplify code for clarity while preserving exact functionality.
|
|
|
12399
12399
|
|
|
12400
12400
|
## Core Rules
|
|
12401
12401
|
|
|
12402
|
-
1. **Preserve Functionality** - Never change what
|
|
12402
|
+
1. **Preserve Functionality** - Never change what it does, only how.
|
|
12403
12403
|
|
|
12404
12404
|
2. **Enhance Clarity**:
|
|
12405
12405
|
- Reduce nesting and complexity
|
|
@@ -12439,31 +12439,113 @@ Simplify code for clarity while preserving exact functionality.
|
|
|
12439
12439
|
Skip when:
|
|
12440
12440
|
- User wants exact structure preserved
|
|
12441
12441
|
- Code is already clean`;
|
|
12442
|
-
var
|
|
12443
|
-
|
|
12444
|
-
|
|
12445
|
-
|
|
12446
|
-
|
|
12447
|
-
|
|
12448
|
-
|
|
12449
|
-
|
|
12450
|
-
|
|
12451
|
-
|
|
12452
|
-
|
|
12453
|
-
|
|
12454
|
-
|
|
12455
|
-
|
|
12456
|
-
|
|
12457
|
-
|
|
12458
|
-
|
|
12459
|
-
|
|
12460
|
-
|
|
12442
|
+
var CODE_EXTENSIONS = new Set([
|
|
12443
|
+
".js",
|
|
12444
|
+
".jsx",
|
|
12445
|
+
".ts",
|
|
12446
|
+
".tsx",
|
|
12447
|
+
".mjs",
|
|
12448
|
+
".cjs",
|
|
12449
|
+
".py",
|
|
12450
|
+
".go",
|
|
12451
|
+
".rs",
|
|
12452
|
+
".java",
|
|
12453
|
+
".rb",
|
|
12454
|
+
".c",
|
|
12455
|
+
".cpp",
|
|
12456
|
+
".h",
|
|
12457
|
+
".hpp",
|
|
12458
|
+
".cs",
|
|
12459
|
+
".php",
|
|
12460
|
+
".swift",
|
|
12461
|
+
".kt",
|
|
12462
|
+
".lua",
|
|
12463
|
+
".zig"
|
|
12464
|
+
]);
|
|
12465
|
+
var SKIP_PATTERNS = [
|
|
12466
|
+
"node_modules",
|
|
12467
|
+
".git",
|
|
12468
|
+
"dist",
|
|
12469
|
+
"build",
|
|
12470
|
+
".next",
|
|
12471
|
+
"vendor",
|
|
12472
|
+
"__pycache__",
|
|
12473
|
+
".cache",
|
|
12474
|
+
"target"
|
|
12475
|
+
];
|
|
12476
|
+
var NESTING_THRESHOLD = 3;
|
|
12477
|
+
var REPEATED_THRESHOLD = 3;
|
|
12478
|
+
function isCodeFile(filePath) {
|
|
12479
|
+
const ext = extname(filePath).toLowerCase();
|
|
12480
|
+
if (!CODE_EXTENSIONS.has(ext))
|
|
12481
|
+
return false;
|
|
12482
|
+
return !SKIP_PATTERNS.some((p) => filePath.includes(`/${p}/`) || filePath.includes(`\\${p}\\`));
|
|
12483
|
+
}
|
|
12484
|
+
function getIndentLevel(line) {
|
|
12485
|
+
let spaces = 0;
|
|
12486
|
+
for (const ch of line) {
|
|
12487
|
+
if (ch === " ")
|
|
12488
|
+
spaces++;
|
|
12489
|
+
else if (ch === "\t")
|
|
12490
|
+
spaces += 4;
|
|
12491
|
+
else
|
|
12492
|
+
break;
|
|
12493
|
+
}
|
|
12494
|
+
return Math.floor(spaces / 2);
|
|
12495
|
+
}
|
|
12496
|
+
function checkNestingDepth(code) {
|
|
12497
|
+
let maxDepth = 0;
|
|
12498
|
+
for (const line of code.split(`
|
|
12499
|
+
`)) {
|
|
12500
|
+
if (line.trim() === "")
|
|
12501
|
+
continue;
|
|
12502
|
+
const depth = getIndentLevel(line);
|
|
12503
|
+
if (depth > maxDepth)
|
|
12504
|
+
maxDepth = depth;
|
|
12505
|
+
}
|
|
12506
|
+
if (maxDepth >= NESTING_THRESHOLD) {
|
|
12507
|
+
return {
|
|
12508
|
+
rule: "nesting-depth",
|
|
12509
|
+
detail: `Max nesting depth: ${maxDepth} (threshold: ${NESTING_THRESHOLD})`
|
|
12510
|
+
};
|
|
12511
|
+
}
|
|
12512
|
+
return null;
|
|
12513
|
+
}
|
|
12514
|
+
function checkRepeatedExpressions(code) {
|
|
12515
|
+
const issues = [];
|
|
12516
|
+
const calls = new Map;
|
|
12517
|
+
const pattern = /(\w+(?:\.\w+)*(?:\[.*?\])*(?:\(.*?\))*)/g;
|
|
12518
|
+
let match;
|
|
12519
|
+
while ((match = pattern.exec(code)) !== null) {
|
|
12520
|
+
const expr = match[1];
|
|
12521
|
+
if (expr.length < 5)
|
|
12522
|
+
continue;
|
|
12523
|
+
const base = expr.replace(/\[.*?\]/g, "[]");
|
|
12524
|
+
calls.set(base, (calls.get(base) ?? 0) + 1);
|
|
12525
|
+
}
|
|
12526
|
+
for (const [expr, count] of calls) {
|
|
12527
|
+
if (count >= REPEATED_THRESHOLD) {
|
|
12528
|
+
issues.push({
|
|
12529
|
+
rule: "repeated-expression",
|
|
12530
|
+
detail: `Repeated expression "${expr}" appears ${count} times`
|
|
12531
|
+
});
|
|
12532
|
+
}
|
|
12533
|
+
}
|
|
12534
|
+
return issues;
|
|
12535
|
+
}
|
|
12536
|
+
function analyzeCode(code) {
|
|
12537
|
+
const issues = [];
|
|
12538
|
+
const nesting = checkNestingDepth(code);
|
|
12539
|
+
if (nesting)
|
|
12540
|
+
issues.push(nesting);
|
|
12541
|
+
issues.push(...checkRepeatedExpressions(code));
|
|
12542
|
+
return issues;
|
|
12543
|
+
}
|
|
12461
12544
|
function ensureFilesExist() {
|
|
12462
12545
|
const configDir = join(homedir(), ".config", "opencode");
|
|
12463
12546
|
const files = [
|
|
12464
12547
|
{ path: "skills/code-simplifier/SKILL.md", content: SKILL_CONTENT },
|
|
12465
|
-
{ path: "rules/code-simplifier.md", content: RULES_CONTENT }
|
|
12466
|
-
{ path: "commands/simplify.md", content: COMMAND_CONTENT }
|
|
12548
|
+
{ path: "rules/code-simplifier.md", content: RULES_CONTENT }
|
|
12467
12549
|
];
|
|
12468
12550
|
for (const file2 of files) {
|
|
12469
12551
|
const fullPath = join(configDir, file2.path);
|
|
@@ -12474,40 +12556,40 @@ function ensureFilesExist() {
|
|
|
12474
12556
|
}
|
|
12475
12557
|
}
|
|
12476
12558
|
}
|
|
12559
|
+
function readFileSafe(filePath) {
|
|
12560
|
+
try {
|
|
12561
|
+
return readFileSync(filePath, "utf-8");
|
|
12562
|
+
} catch {
|
|
12563
|
+
return null;
|
|
12564
|
+
}
|
|
12565
|
+
}
|
|
12477
12566
|
var CodeSimplifier = async ({ client }) => {
|
|
12478
12567
|
ensureFilesExist();
|
|
12479
|
-
await client.app.log({
|
|
12480
|
-
body: {
|
|
12481
|
-
service: "code-simplifier",
|
|
12482
|
-
level: "info",
|
|
12483
|
-
message: "Code Simplifier loaded. Use /simplify or the simplify-code tool."
|
|
12484
|
-
}
|
|
12485
|
-
});
|
|
12486
12568
|
return {
|
|
12487
|
-
|
|
12488
|
-
if (
|
|
12489
|
-
|
|
12490
|
-
|
|
12491
|
-
|
|
12492
|
-
|
|
12493
|
-
|
|
12494
|
-
|
|
12495
|
-
|
|
12496
|
-
|
|
12497
|
-
|
|
12498
|
-
|
|
12499
|
-
|
|
12500
|
-
|
|
12501
|
-
|
|
12502
|
-
|
|
12503
|
-
|
|
12504
|
-
|
|
12505
|
-
|
|
12506
|
-
|
|
12507
|
-
|
|
12508
|
-
|
|
12569
|
+
async "tool.execute.after"(input, output) {
|
|
12570
|
+
if (input.tool !== "write" && input.tool !== "edit")
|
|
12571
|
+
return;
|
|
12572
|
+
const filePath = input.args?.file ?? input.args?.filePath ?? input.args?.path;
|
|
12573
|
+
if (!filePath || !isCodeFile(filePath))
|
|
12574
|
+
return;
|
|
12575
|
+
const code = readFileSafe(filePath);
|
|
12576
|
+
if (!code)
|
|
12577
|
+
return;
|
|
12578
|
+
const issues = analyzeCode(code);
|
|
12579
|
+
if (issues.length === 0)
|
|
12580
|
+
return;
|
|
12581
|
+
const fileName = filePath.split("/").pop() ?? filePath;
|
|
12582
|
+
const lines = issues.map((i) => ` - ${i.detail}`).join(`
|
|
12583
|
+
`);
|
|
12584
|
+
await client.event.emit({
|
|
12585
|
+
type: "tui.toast.show",
|
|
12586
|
+
properties: {
|
|
12587
|
+
message: `${fileName} has potential issues:
|
|
12588
|
+
${lines}
|
|
12589
|
+
Use "simplify-code" to clean up.`,
|
|
12590
|
+
variant: "warning"
|
|
12509
12591
|
}
|
|
12510
|
-
}
|
|
12592
|
+
});
|
|
12511
12593
|
},
|
|
12512
12594
|
tool: {
|
|
12513
12595
|
"simplify-code": tool({
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "opencode-code-simplifier",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.5.0",
|
|
4
4
|
"description": "Code simplifier plugin for OpenCode - simplifies and refines code for clarity while preserving functionality",
|
|
5
5
|
"main": "dist/plugin.js",
|
|
6
6
|
"types": "dist/plugin.d.ts",
|