Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

nimpretty creates invalid code with nested if inside parenthesis #23574

Closed
Alogani opened this issue May 5, 2024 · 1 comment
Closed

nimpretty creates invalid code with nested if inside parenthesis #23574

Alogani opened this issue May 5, 2024 · 1 comment

Comments

@Alogani
Copy link

Alogani commented May 5, 2024

Description

Hello,

I encoutered a construct where nimpretty fails.

Nim Version

nim: 2.0.4
nimpretty: 0.2

Example 1: invalid formatting

Current Output

Here is the code as formatted by nimpretty:
This code is invalid

proc merge*(procArgs: ProcArgs, modifier: ProcArgsModifier): ProcArgs =
    ProcArgs(
        prefixCmd: if modifier.prefixCmd.isSome: modifier.prefixCmd.get(
                ) else: procArgs.prefixCmd,
        options: procArgs.options + modifier.toAdd - modifier.toRemove,
        input: if modifier.input.isSome: modifier.input.get(
                ) else: procArgs.input,
        output: if modifier.output.isSome: modifier.output.get(
                ) else: procArgs.output,
        outputErr: if modifier.outputErr.isSome: modifier.outputErr.get(
                ) else: procArgs.outputErr,
        env: (if modifier.env.isSome:
        if modifier.envModifier.isSome():
            mergeEnv(modifier.env.get(), modifier.envModifier.get())
        else:
            modifier.env.get()
    else:
        if modifier.envModifier.isSome():
            mergeEnv(procArgs.env, modifier.envModifier.get())
        else:
            procArgs.env
    ),
        workingDir: if modifier.workingDir.isSome: modifier.workingDir.get(
                ) else: procArgs.workingDir,
        processName: if modifier.processName.isSome: modifier.processName.get(
                ) else: procArgs.processName,
        logFn: if modifier.logFn.isSome: modifier.logFn.get(
                ) else: procArgs.logFn,
        onErrorFn: if modifier.onErrorFn.isSome: modifier.onErrorFn.get(
                ) else: procArgs.onErrorFn,
    )

Expected Output

Here is the valid code before formatting:

proc merge*(a, b: ProcArgsModifier): ProcArgsModifier =
    ProcArgsModifier(
        prefixCmd: if b.prefixCmd.isSome: b.prefixCmd else: a.prefixCmd,
        toAdd: a.toAdd + b.toAdd, # options to remove take priority
        toRemove: (a.toRemove - b.toAdd) + b.toRemove,
        input: if b.input.isSome: b.input else: a.input,
        output: if b.output.isSome: b.output else: a.output,
        outputErr: if b.outputErr.isSome: b.outputErr else: a.outputErr,
        env: (if b.env.isSome:
            if a.env.isSome:
                some(mergeEnv(a.env.get(), b.env.get()))
            else:
                b.env
        else:
            a.env),
            envModifier: (if b.envModifier.isSome:
            if a.envModifier.isSome:
                some(mergeEnv(a.envModifier.get(), b.envModifier.get()))
            else:
                b.envModifier
        else:
            a.envModifier),
        workingDir: if b.workingDir.isSome: b.workingDir else: a.workingDir,
        processName: if b.processName.isSome: b.processName else: a.processName,
        logFn: if b.logFn.isSome: b.logFn else: a.logFn,
        onErrorFn: if b.onErrorFn.isSome: b.onErrorFn else: a.onErrorFn,
    )

Example2: Strange (but valid) formatting

I also encouters a formatting that visually seems wrong (but compiles)

Actual Output2

Valid but looks wrong

    result = ProcResult(
        fullCmd: self.fullCmd,
        cmd: self.cmd,
        input:
        (
            if self.captureStreams.input != nil:
            await self.captureStreams.input
        else:
            ""),
        output:
        (
            if self.captureStreams.output != nil:
            await self.captureStreams.output
        else:
            ""),
        outputErr:
        (
            if self.captureStreams.outputErr != nil:
            await self.captureStreams.outputErr
        else:
            ""),
        exitCode: exitCode,
        success: exitCode == 0,
        procArgs: self.procArgs,
    )

Expected Output2

    result = ProcResult(
        fullCmd: self.fullCmd,
        cmd: self.cmd,
        input:
        (
            if self.captureStreams.input != nil:
                await self.captureStreams.input
            else:
                ""),
        output:
        (
            if self.captureStreams.output != nil:
                await self.captureStreams.output
            else:
                ""),
        outputErr:
        (
            if self.captureStreams.outputErr != nil:
            await self.captureStreams.outputErr
        else:
            ""),
        exitCode: exitCode,
        success: exitCode == 0,
        procArgs: self.procArgs,
    )

Workaround for example2

This compiles and looks better :

    result = ProcResult(
        fullCmd: self.fullCmd,
        cmd: self.cmd,
        input:
        (if self.captureStreams.input != nil:
            await self.captureStreams.input
        else:
            ""),
        output:
        (if self.captureStreams.output != nil:
            await self.captureStreams.output
        else:
            ""),
        outputErr:
        (if self.captureStreams.outputErr != nil:
            await self.captureStreams.outputErr
        else:
            ""),
        exitCode: exitCode,
        success: exitCode == 0,
        procArgs: self.procArgs,
    )

Possible Solution

No response

Additional Information

And I put this here: Thanks for the great tools ! :-)

@Alogani Alogani changed the title nimpretty creates invalid code in some cireconstances nimpretty creates invalid code in some circonstances May 5, 2024
@Alogani Alogani changed the title nimpretty creates invalid code in some circonstances nimpretty creates invalid code with nested if inside parenthesis May 5, 2024
@Alogani
Copy link
Author

Alogani commented May 6, 2024

This is a duplicate of #18498

@Alogani Alogani closed this as not planned Won't fix, can't repro, duplicate, stale May 6, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants