Skip to content

linter: Potential false flag for oxc(const-comparisons) #19352

@MrSimmmons

Description

@MrSimmmons

What version of Oxlint are you using?

1.47.0

What command did you run?

oxlint

What does your .oxlintrc.json config file look like?

{
	"$schema": "./node_modules/oxlint/configuration_schema.json",
	"rules": {
		"no-unused-vars": [
			"warn",
			{
				"vars": "all",
				"args": "none",
				"varsIgnorePattern": "^_",
				"argsIgnorePattern": "^_",
				"caughtErrorsIgnorePattern": "^_"
			}
		]
	},
	"env": {
		"builtin": true,
		"node": true
	},
	"ignorePatterns": [
		"**/node_modules/**",
		"**/build/**",
		"**/dist/**",
		"**/*.d.ts",
		"**/*.md"
	]
}

What happened?

Im not sure if this should be a lint or a format type issue, but its flagged on a lint rule so I'm making a lint bug.

When using the || or ?? operators in combination with mathematical operators inside logic gates, the falsey case of the operator is used as const in the second half of the if statement rather than the combined type of the truthy & falsy input.

eg:

let numOrUndefined: number | undefined = 6;

if (numOrUndefined || 0 > 0) {
  console.log('       ^---^'); // This comparison will always evaluate to false help: Because `0` will never be greater than oxc(const-comparisons)
}

if (numOrUndefined ?? 0 > 0) {
  console.log('       ^---^');
}

if ((numOrUndefined ?? 0) > 0) {
  console.log('No lint flag');
}

Wrapping the logic in brackets stops the flag (which is where I'm not sure if this is a lint or a format issue) stops the line from being flagged, I'm guessing because the 'return type' of the brackets is just number rather than explicitly 0.

Feel free to close if this is expected behaviour, it just felt off that I had to wrap in brackets to "fix" it when I ran into it.

If it is a issue with the linter, adding a --fix case could wrap the numOrUndefined ?? 0 in brackets to "fix" it. But that does feel like its getting to the formatter side of things.

Metadata

Metadata

Assignees

Labels

Type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions