Skip to content

Dumping add quote to string even it is not necessary #470

Closed
@joshuaavalon

Description

@joshuaavalon

As title.

Test Code

const jsYaml = require("js-yaml");
const fs = require("fs");
const yamlStr = jsYaml.dump({ url: "https://github.com/nodeca/js-yaml" });
fs.writeFileSync("foo.yml", yamlStr, { encoding: "utf8" });

Expected Result

url: https://github.com/nodeca/js-yaml

Actual Result

url: 'https://github.com/nodeca/js-yaml'

Problem

plain = plain && isPlainSafe(char);

function isPlainSafe(c) {
// Uses a subset of nb-char - c-flow-indicator - ":" - "#"
// where nb-char ::= c-printable - b-char - c-byte-order-mark.
return isPrintable(c) && c !== 0xFEFF
// - c-flow-indicator
&& c !== CHAR_COMMA
&& c !== CHAR_LEFT_SQUARE_BRACKET
&& c !== CHAR_RIGHT_SQUARE_BRACKET
&& c !== CHAR_LEFT_CURLY_BRACKET
&& c !== CHAR_RIGHT_CURLY_BRACKET
// - ":" - "#"
&& c !== CHAR_COLON
&& c !== CHAR_SHARP;
}

All of these characters ( except0xFEFF ) are safe inside string. Only colon cannot be followed by space or placed at the end of line.

Activity

diberry

diberry commented on Feb 26, 2019

@diberry
Contributor

This is the same as #466. Perhaps we can have a new setting to handle the quoting issue?

puzrin

puzrin commented on Feb 26, 2019

@puzrin
Member

@joshuaavalon "unquoting" quality is limited only by authors time. If anyone has time to improve it - PR is welcome.

diberry

diberry commented on Feb 26, 2019

@diberry
Contributor

@puzrin I'll make a PR this weekend, if no one does it before then.

diberry

diberry commented on Mar 2, 2019

@diberry
Contributor
diberry

diberry commented on Mar 3, 2019

@diberry
Contributor

@puzrin Are these tests all valid/correct?

`test.only('should not unnecessaryly apply quotes', function () {

var expected = 'url: https://github.com/nodeca/js-yaml\n';
var actual = yaml.dump({ url: 'https://github.com/nodeca/js-yaml' });

assert.strictEqual(actual, expected);
});
test.only('should not unnecessaryly apply quotes - ', function () {

var expected = 'url: https://github.com/nodeca/js-yaml\n';

var obj = {};
obj['url']='https://github.com/nodeca/js-yaml';

var actual = yaml.dump(obj);

assert.strictEqual(actual, expected);
});

test.only('should not unnecessaryly apply quotes - space then /\n at end of value', function () {

var expected = 'url: 'https://github.com/nodeca/js-yaml '\n';

var obj = {};
obj['url']='https://github.com/nodeca/js-yaml ';

var actual = yaml.dump(obj);

assert.strictEqual(actual, expected);
});
test.only('should not unnecessaryly apply quotes - space after colon', function () {

var expected = 'url: 'https: //github.com/nodeca/js-yaml'\n';
var actual = yaml.dump({ url: 'https: //github.com/nodeca/js-yaml' });

assert.strictEqual(actual, expected);
});
`

puzrin

puzrin commented on Mar 3, 2019

@puzrin
Member

Could you format you message and add proper lang type for highlight?

added 4 commits that reference this issue on Apr 15, 2019

extended tests for nodeca#470, including bugfix for the dumper: now a…

extended tests for nodeca#470, including bugfix for the dumper: now a…

added 2 commits that reference this issue on Apr 16, 2019

Revert nodeca#470/nodeca#473 for dumper: too many situations where th…

kchen-shanghai

kchen-shanghai commented on Jul 2, 2020

@kchen-shanghai

Having a same problem here. Since only colon and hash characters are limited in flow scalars, is a PR still welcome by now?

Giszmo

Giszmo commented on Jul 22, 2020

@Giszmo

@ZEROPC I would appreciate a fix but but not speaking for the project. Crossing fingers :D

27 remaining items

Loading
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

      Development

      No branches or pull requests

        Participants

        @Giszmo@puzrin@rlidwka@mgiamberardino@joshuaavalon

        Issue actions

          Dumping add quote to string even it is not necessary · Issue #470 · nodeca/js-yaml