js-beautify icon indicating copy to clipboard operation
js-beautify copied to clipboard

Ignore directive adds newline

Open jonrosner opened this issue 1 year ago • 1 comments

Description

I have a special JSON syntax allowing interpolations using ${env} notation. Without the directive, beautify would split the interpolation apart across several lines. To mitigate this I add ignore directives around the interpolations before beautification. Unfortunately it still does not provide the expected result.

Input

The code looked like this before beautification:

{
    "hello": /* beautify ignore:start */${world}/* beautify ignore:end */
}

Expected Output

The code should have looked like this after beautification:

{
    "hello": /* beautify ignore:start */${world}/* beautify ignore:end */
}

Actual Output

The code actually looked like this after beautification:

{
    "hello":
    /* beautify ignore:start */${world}/* beautify ignore:end */
}

Steps to Reproduce

const body = `
    {
      "hello": /* beautify ignore:start */${world}/* beautify ignore:end */
    }
`;
const result = beautify(body, { format: 'json' });
console.log(result)

Settings

all default settings

jonrosner avatar Mar 03 '23 11:03 jonrosner

This correct behavior in the current design. The ignore expects to start on it's own line. The ignore annotation is not designed to handle the level of fine-grained control.

PRs to change this are welcome, but will require a broad set of tests to ensure they behave well.

Instead of trying to ignore parts of a line, perhaps try ignoring the whole line:

{
    /* beautify ignore:start */
    "hello": ${world}
    /* beautify ignore:end */
}

bitwiseman avatar Mar 20 '23 17:03 bitwiseman