Skip to content
This repository has been archived by the owner on Oct 15, 2024. It is now read-only.

Commit

Permalink
fix: support qmllint with PySide 6.4.1
Browse files Browse the repository at this point in the history
Fix #255
  • Loading branch information
seanwu1105 committed Nov 29, 2022
1 parent 7c0f427 commit dc2f427
Show file tree
Hide file tree
Showing 6 changed files with 88 additions and 9 deletions.
2 changes: 1 addition & 1 deletion .eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
"no-magic-numbers": "off", // must disable the base rule as it can report incorrect errors
"@typescript-eslint/no-magic-numbers": [
"error",
{ "ignore": [0, 1], "ignoreDefaultValues": true }
{ "ignore": [0, 1, -1], "ignoreDefaultValues": true }
],
"@typescript-eslint/explicit-module-boundary-types": "off",
"@typescript-eslint/no-explicit-any": "off",
Expand Down
4 changes: 2 additions & 2 deletions python/tests/test_qmllint.py
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ def test_qmllint_multiline_string_qml():


def lint_qml(filename: str, debug=False):
result = invoke_qmllint_py([get_assets_path(filename)])
result = invoke_qmllint_py(["--json", "-", get_assets_path(filename)])
if debug:
print(f"\nreturncode:")
print(result.returncode)
Expand All @@ -131,7 +131,7 @@ def lint_qml(filename: str, debug=False):

def invoke_qmllint_py(args: list[str]):
return subprocess.run(
["poetry", "run", "python", "qmllint.py", "--json", *args],
["poetry", "run", "python", "qmllint.py", *args],
cwd=SCRIPTS_DIR,
capture_output=True,
)
Expand Down
40 changes: 40 additions & 0 deletions src/qmllint/server/get-version.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import type { CommandArgs, ExecError, StdErrError } from '../../run'
import { run } from '../../run'
import type { ErrorResult, SuccessResult } from '../../types'

export async function getVersion({
qmlLintCommand,
}: GetVersionArgs): Promise<GetVersionResult> {
const runResult = await run({
command: [...qmlLintCommand, '--version'],
})

if (runResult.kind === 'Success') {
const version = runResult.value.split(' ').at(-1)?.trim()
if (typeof version !== 'string' || !checkVersionString(version))
return {
kind: 'ParseError',
message: `Could not parse version: ${version}`,
}
return { kind: 'Success', value: version }
}

return runResult
}

export type GetVersionArgs = {
readonly qmlLintCommand: CommandArgs
}

export type GetVersionResult =
| SuccessResult<Version>
| ErrorResult<'Parse'>
| ExecError
| StdErrError

export type Version = `${number}.${number}.${number}`

function checkVersionString(version: string): version is Version {
const versionRegex = /^\d+\.\d+\.\d+$/
return versionRegex.test(version)
}
33 changes: 32 additions & 1 deletion src/qmllint/server/lint.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,27 @@ import type { CommandArgs, ExecError, StdErrError } from '../../run'
import { run } from '../../run'
import type { ErrorResult, SuccessResult } from '../../types'
import { notNil } from '../../utils'
import type { Version } from './get-version'
import { getVersion } from './get-version'

export async function lint({
qmlLintCommand,
documentPath,
options,
}: LintArgs): Promise<LintResult> {
const getVersionResult = await getVersion({ qmlLintCommand })

if (getVersionResult.kind !== 'Success') return getVersionResult

const jsonOption = getJsonOption(getVersionResult.value)

const optionsWithJson = [
...removeDuplicatedJsonOption(options),
...jsonOption,
]

const runResult = await run({
command: [...qmlLintCommand, ...options, documentPath],
command: [...qmlLintCommand, ...optionsWithJson, documentPath],
})

if (runResult.kind === 'Success')
Expand Down Expand Up @@ -38,6 +51,24 @@ export type LintResult =
| ExecError
| StdErrError

function removeDuplicatedJsonOption(options: CommandArgs) {
let cleanedOptions = options
for (let i = 0; i < options.length; i++)
if (options[i] === '--json' && options[i + 1] === '-')
cleanedOptions = [...options.slice(0, i), ...options.slice(i + 1)]

return cleanedOptions.filter(option => option !== '--json')
}

function getJsonOption(version: Version) {
if (compareVersion(version, '6.4.1') < 0) return ['--json']
return ['--json', '-']
}

function compareVersion(a: Version, b: Version) {
return a.localeCompare(b, undefined, { numeric: true, sensitivity: 'base' })
}

function parseQmlLintRunReturnValue(
value: string,
): ParseQmlLintRunReturnValueResult {
Expand Down
6 changes: 1 addition & 5 deletions src/qmllint/server/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,14 +71,10 @@ async function lintQml({ uri, connection }: LintQmlArgs) {
connection,
})

const options = qmlLintCommandResult.value.options.includes('--json')
? qmlLintCommandResult.value.options
: [...qmlLintCommandResult.value.options, '--json']

const lintResult = await lint({
qmlLintCommand: qmlLintCommandResult.value.command,
documentPath: uriToPathResult.value,
options,
options: qmlLintCommandResult.value.options,
})

if (lintResult.kind === 'Success') {
Expand Down
12 changes: 12 additions & 0 deletions src/test/suite/qmllint/server/lint.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import * as assert from 'node:assert'
import * as sinon from 'sinon'
import * as GetVersion from '../../../../qmllint/server/get-version'
import type {
LintArgs,
LintResult,
Expand All @@ -24,8 +25,19 @@ suite('qmllint/lint', () => {
],
}

const mockGetVersionResult: GetVersion.GetVersionResult = {
kind: 'Success',
value: '6.4.1',
}

let result: LintResult

setup(() =>
sinon.replace(GetVersion, 'getVersion', async () => mockGetVersionResult),
)

teardown(() => sinon.restore())

suite('when linting success', () => {
suite('when lint result is parsable', () => {
setup(async () => {
Expand Down

0 comments on commit dc2f427

Please sign in to comment.