fix: recognise options that are overwritten unnecessarily#12
Conversation
Add test files to build when not in production Correct import of Mocha and Glob in test suite
Add new util function to compare JSON to AST. Use isJSONDifferentToAST function to compare changes after edit. ✅ Closes: 7811
|
The changes I've made to get the tests working might be out of scope for the issue which I'm trying to resolve, so I've structured my commits so that if required I can drop the changes to the test and the new tests and only include the change which resolves the issue. Or I could extract them into a new PR. |
AlCalzone
left a comment
There was a problem hiding this comment.
Just a few nitpicks, otherwise nothing to add
There was a problem hiding this comment.
Pull Request Overview
This PR adds functionality to detect when options or properties imported via JSON AST are overridden unnecessarily, integrates the new comparison util into the diagnostics, and fixes test suite imports and build inclusion.
- Introduce
isJSONDifferentToASTand use it inimportOverrideDiagnosticsto distinguish real overrides from identical ones - Update test suite imports (Mocha, glob) and add tests for both scenarios
- Modify
esbuild.jsto include tests in development builds
Reviewed Changes
Copilot reviewed 6 out of 6 changed files in this pull request and generated 5 comments.
Show a summary per file
| File | Description |
|---|---|
| src/astUtils.ts | Added isJSONDifferentToAST helper and AST checks |
| src/diagnostics/importOverrideDiagnostics.ts | Use new util to flag unnecessary overrides |
| src/test/suite/index.ts | Switch to default imports for glob and Mocha |
| src/test/suite/diagnostics/importOverrideDiagnostics.test.ts | Expanded tests; fixed import paths |
| src/test/suite/astUtils.test.ts | Added comprehensive AST <-> JSON comparison tests |
| esbuild.js | Include test files in non-production builds |
Comments suppressed due to low confidence (3)
src/test/suite/astUtils.test.ts:120
- [nitpick] This test description duplicates an earlier one but tests the opposite case (JSON longer than AST). Please update it to accurately reflect the scenario.
test("returns true if ast array is longer than json array", () => {
esbuild.js:20
- [nitpick] Bundling test files into the extension build can bloat output. Consider using a separate build step or directory for tests.
entryPoints: ["./src/extension.ts", ...testFiles],
src/test/suite/index.ts:1
- [nitpick] Switching to default imports requires
esModuleInterop(orallowSyntheticDefaultImports) in tsconfig. Please verify your compiler settings to avoid runtime import errors.
import glob from "glob";
| .map((n) => { | ||
| return [ | ||
| getPropertyNameFromNode(n), | ||
| n.parent.valueNode, |
There was a problem hiding this comment.
Using n.parent.valueNode returns the AST node rather than the JSON value. Consider using getPropertyValueFromNode(n) to pass the actual JSON value into the diagnostics.
| n.parent.valueNode, | |
| getPropertyValueFromNode(n), |
There was a problem hiding this comment.
Hmm I missed this change. The change itself is fine, so ignore Copilot's review here. Given that the array now contains the AST node and not its value, the variable in line 82 should be renamed to valueNode.
There was a problem hiding this comment.
I'm wondering if the array could be reduced to just the property name , and the ASTNode.
Line 94 could be changed to: value: getPropertyValueFromNode(propNode),.
And line 85 to if (!isJSONDifferentToAST(originalValue, propNode.parent.valueNode))
There was a problem hiding this comment.
Hmm that wouldn't change the other type issue with the value possibly being undefined. Let's just stick to renaming then and making sure we pass the actual value to the diagnostic and not the AST node (see other comment).
There was a problem hiding this comment.
I've changed the variable name as suggested
| const isUnchanged = value === originalValue; | ||
|
|
||
| if (isUnchanged) { | ||
| if (value && !isJSONDifferentToAST(originalValue, value)) { |
There was a problem hiding this comment.
[nitpick] The value check is always truthy for an ASTNode. You can remove the redundant value && guard to simplify the condition.
| if (value && !isJSONDifferentToAST(originalValue, value)) { | |
| if (!isJSONDifferentToAST(originalValue, value)) { |
There was a problem hiding this comment.
This nitpick seems correct, please change.
There was a problem hiding this comment.
Typescript complains when I make this change, value is set to n.parent.valueNode, and n.parent is typed as an ASTNode which doesn't always have a valueNode even if logically it must have one.
I could change the isJSONDifferentToAST to accept undefined as the second param, which would remove the need to check if the value is defined here.
There was a problem hiding this comment.
Ok, let's keep this one as is then.
There was a problem hiding this comment.
@SChetwynd Sorry, there are a few more things I missed. I already committed the spelling fixes, so make sure to pull first.
The comments are referring to the Copilot review comments above, in case the context is not clear.
| .map((n) => { | ||
| return [ | ||
| getPropertyNameFromNode(n), | ||
| n.parent.valueNode, |
There was a problem hiding this comment.
Hmm I missed this change. The change itself is fine, so ignore Copilot's review here. Given that the array now contains the AST node and not its value, the variable in line 82 should be renamed to valueNode.
| const isUnchanged = value === originalValue; | ||
|
|
||
| if (isUnchanged) { | ||
| if (value && !isJSONDifferentToAST(originalValue, value)) { |
There was a problem hiding this comment.
This nitpick seems correct, please change.
change return value when import is overwritten, but not unnecessarily
Changes:
isJSONDifferentToAST.isJSONDifferentToASTfunction to compare changes after edit.MochaandGlobin test suitefixes: zwave-js/zwave-js#7811
Options have been overwritten and one of the properties is different, only indicates that the property is overwritten:
Options have been overwritten and all of the properties are the same, shows as an error:
(^^ I created these examples by changing the
master_template, those changes are not included in the PR)