Skip to content

fix: recognise options that are overwritten unnecessarily#12

Merged
AlCalzone merged 6 commits into
zwave-js:mainfrom
SChetwynd:fix/7811
Jul 9, 2025
Merged

fix: recognise options that are overwritten unnecessarily#12
AlCalzone merged 6 commits into
zwave-js:mainfrom
SChetwynd:fix/7811

Conversation

@SChetwynd

@SChetwynd SChetwynd commented Jul 5, 2025

Copy link
Copy Markdown
Contributor

Changes:

  • Add new util function to compare JSON to AST isJSONDifferentToAST.
  • Use isJSONDifferentToAST function to compare changes after edit.
  • Add test files to build when not in production
  • Correct import of Mocha and Glob in test suite

fixes: zwave-js/zwave-js#7811

Options have been overwritten and one of the properties is different, only indicates that the property is overwritten:

image

Options have been overwritten and all of the properties are the same, shows as an error:

image

(^^ I created these examples by changing the master_template, those changes are not included in the PR)

SChetwynd added 3 commits July 4, 2025 21:15
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
@SChetwynd

Copy link
Copy Markdown
Contributor Author

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 AlCalzone left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just a few nitpicks, otherwise nothing to add

Comment thread esbuild.js
Comment thread src/astUtils.ts Outdated
Comment thread src/astUtils.ts
Comment thread src/astUtils.ts
@AlCalzone AlCalzone changed the title fix: Recognise options that are overwritten unnecessarily (#7811) fix: recognise options that are overwritten unnecessarily Jul 8, 2025
@AlCalzone
AlCalzone requested a review from Copilot July 8, 2025 14:02

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 isJSONDifferentToAST and use it in importOverrideDiagnostics to distinguish real overrides from identical ones
  • Update test suite imports (Mocha, glob) and add tests for both scenarios
  • Modify esbuild.js to 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 (or allowSyntheticDefaultImports) in tsconfig. Please verify your compiler settings to avoid runtime import errors.
import glob from "glob";

Comment thread src/test/suite/diagnostics/importOverrideDiagnostics.test.ts Outdated
Comment thread src/test/suite/diagnostics/importOverrideDiagnostics.test.ts Outdated
Comment thread src/test/suite/diagnostics/importOverrideDiagnostics.test.ts Outdated
.map((n) => {
return [
getPropertyNameFromNode(n),
n.parent.valueNode,

Copilot AI Jul 8, 2025

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Suggested change
n.parent.valueNode,
getPropertyValueFromNode(n),

Copilot uses AI. Check for mistakes.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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))

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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).

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've changed the variable name as suggested

const isUnchanged = value === originalValue;

if (isUnchanged) {
if (value && !isJSONDifferentToAST(originalValue, value)) {

Copilot AI Jul 8, 2025

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[nitpick] The value check is always truthy for an ASTNode. You can remove the redundant value && guard to simplify the condition.

Suggested change
if (value && !isJSONDifferentToAST(originalValue, value)) {
if (!isJSONDifferentToAST(originalValue, value)) {

Copilot uses AI. Check for mistakes.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This nitpick seems correct, please change.

@SChetwynd SChetwynd Jul 8, 2025

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, let's keep this one as is then.

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>

@AlCalzone AlCalzone left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@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,

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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)) {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This nitpick seems correct, please change.

Comment thread src/diagnostics/importOverrideDiagnostics.ts
change return value when import is overwritten, but not unnecessarily

@AlCalzone AlCalzone left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks a lot!

@AlCalzone
AlCalzone merged commit 4f4f8d0 into zwave-js:main Jul 9, 2025
2 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

VSCode extension: Recognize options that are overwritten unnecessarily

3 participants