Skip to content

Commit 4f4f8d0

Browse files
SChetwyndAlCalzoneCopilot
authored
fix: recognise options that are overwritten unnecessarily (#12)
Co-authored-by: AlCalzone <d.griesel@gmx.net> Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
1 parent 287a614 commit 4f4f8d0

File tree

6 files changed

+659
-16
lines changed

6 files changed

+659
-16
lines changed

esbuild.js

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,20 +2,23 @@
22
// This may require changes when upgrading to esbuild 0.17+
33

44
const { build } = require("esbuild");
5+
const glob = require("glob");
56

67
const baseConfig = {
78
bundle: true,
89
minify: process.env.NODE_ENV === "production",
910
sourcemap: process.env.NODE_ENV !== "production",
1011
};
1112

13+
const testFiles = process.env.NODE_ENV !== "production" ? glob.sync("./src/test/**/*.ts") : [];
14+
1215
const extensionConfig = {
1316
...baseConfig,
1417
platform: "node",
1518
mainFields: ["module", "main"],
1619
format: "cjs",
17-
entryPoints: ["./src/extension.ts"],
18-
outfile: "./out/extension.js",
20+
entryPoints: ["./src/extension.ts", ...testFiles],
21+
outdir: "./out",
1922
external: ["vscode"],
2023
};
2124

src/astUtils.ts

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import * as vscode from "vscode";
22
import {
3+
ArrayASTNode,
34
ASTNode,
45
Location,
56
ObjectASTNode,
@@ -124,3 +125,64 @@ export function getOptionsFromParamDefinition(
124125
});
125126
return options;
126127
}
128+
129+
export function isJSONDifferentToAST(json: unknown, ast: ASTNode): boolean {
130+
const jsonType =
131+
typeof json === "object"
132+
? Array.isArray(json)
133+
? "array"
134+
: "object"
135+
: typeof json;
136+
137+
// If the property is undefined in the JSON but present in the AST it will return here
138+
if (jsonType !== ast.type) {
139+
return true;
140+
}
141+
142+
if (isArrayASTNode(ast) && Array.isArray(json)) {
143+
if (ast.items.length !== json.length) {
144+
return true;
145+
}
146+
147+
for (let i = 0; i < ast.items.length; i++) {
148+
if (isJSONDifferentToAST(json[i], ast.items[i])) {
149+
return true;
150+
}
151+
}
152+
153+
return false;
154+
}
155+
156+
if (isObjectASTNode(ast) && typeof json === "object" && json !== null) {
157+
if (ast.properties.length !== Object.keys(json).length) {
158+
return true;
159+
}
160+
161+
for (const property of ast.properties) {
162+
if (!property.valueNode) {
163+
return true;
164+
}
165+
166+
if (
167+
isJSONDifferentToAST(
168+
(json as Record<string, unknown>)[property.keyNode.value],
169+
property.valueNode,
170+
)
171+
) {
172+
return true;
173+
}
174+
}
175+
176+
return false;
177+
}
178+
179+
return ast.value !== json;
180+
}
181+
182+
export function isArrayASTNode(node: ASTNode): node is ArrayASTNode {
183+
return node.type === "array";
184+
}
185+
186+
export function isObjectASTNode(node: ASTNode): node is ObjectASTNode {
187+
return node.type === "object";
188+
}

src/diagnostics/importOverrideDiagnostics.ts

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { ASTNode, PropertyASTNode } from "vscode-json-languageservice";
22
import {
33
getPropertyNameFromNode,
44
getPropertyValueFromNode,
5+
isJSONDifferentToAST,
56
nodeIsPropertyNameOrValue,
67
rangeFromNode,
78
} from "../astUtils";
@@ -62,14 +63,13 @@ export function generateImportOverrideDiagnostics(
6263
const properties = s
6364
.map((s) => config.getNodeFromSymbol(s))
6465
.filter(nodeIsPropertyNameOrValue)
65-
.map(
66-
(n) =>
67-
[
68-
getPropertyNameFromNode(n),
69-
getPropertyValueFromNode(n),
70-
n,
71-
] as const,
72-
)
66+
.map((n) => {
67+
return [
68+
getPropertyNameFromNode(n),
69+
n.parent.valueNode,
70+
n,
71+
] as const;
72+
})
7373
.filter(([name]) => name in resolvedImport);
7474
return [resolvedImport, properties] as const;
7575
},
@@ -79,11 +79,10 @@ export function generateImportOverrideDiagnostics(
7979
if (!block) continue;
8080
const [imp, properties] = block;
8181

82-
for (const [name, value, propNode] of properties) {
82+
for (const [name, valueNode, propNode] of properties) {
8383
const originalValue = imp[name];
84-
const isUnchanged = value === originalValue;
8584

86-
if (isUnchanged) {
85+
if (valueNode && !isJSONDifferentToAST(originalValue, valueNode)) {
8786
ret.push({
8887
type: DiagnosticType.UnnecessaryImportOverride,
8988
range: rangeFromNode(config.original, propNode.parent),
@@ -92,7 +91,7 @@ export function generateImportOverrideDiagnostics(
9291
ret.push({
9392
type: DiagnosticType.ImportOverride,
9493
range: rangeFromNode(config.original, propNode),
95-
value,
94+
value: getPropertyValueFromNode(propNode),
9695
originalValue,
9796
});
9897
}

0 commit comments

Comments
 (0)