Skip to content

Commit

Permalink
fix(yaml): update parsers (#5027)
Browse files Browse the repository at this point in the history
- upgrade to `yaml@1.0.0-rc.8` and `yaml-unist-parser@1.0.0-rc.4`
- refactor some logic since the AST has slightly changed (prettier/yaml-unist-parser#82)
- unmatched aliases are now errors since it may introduce invalid AST from `yaml`
- rewrite the document separator (`...`/`---`) logic, this fixes some cases where it can use `---` but we printed `...`
- removed some unnecessary duplicate trailing newline
- trailing comments on `document` (`... #comment`) and `documentHead` (`--- #comment`) are preserved (i.e. they won't be moved somewhere)
  • Loading branch information
ikatyang committed Sep 1, 2018
1 parent 1657420 commit db2bc36
Show file tree
Hide file tree
Showing 17 changed files with 341 additions and 256 deletions.
4 changes: 2 additions & 2 deletions package.json
Expand Up @@ -62,8 +62,8 @@
"unicode-regex": "1.0.1",
"unified": "6.1.6",
"vnopts": "1.0.2",
"yaml": "ikatyang/yaml#a765c1ee16d6b8a5e715564645f2b85f7e04828b",
"yaml-unist-parser": "ikatyang/yaml-unist-parser#cd4f73325b3fc02a6d17842d0d9cee0dfc729c9b"
"yaml": "1.0.0-rc.8",
"yaml-unist-parser": "1.0.0-rc.4"
},
"devDependencies": {
"@babel/cli": "7.0.0-beta.55",
Expand Down
Expand Up @@ -19,7 +19,9 @@ module.exports = ({ types: t }) => ({
t.isMemberExpression(callee, { computed: false }) &&
(t.isArrayExpression(callee.object) ||
(t.isIdentifier(callee.object) &&
/^[A-Z_]+$/.test(callee.object.name))) &&
(/^[A-Z_]+$/.test(callee.object.name) ||
// https://github.com/eemeli/yaml/blob/1005d01/src/Anchors.js#L45
callee.object.name === "names"))) &&
t.isIdentifier(callee.property, { name: "includes" })
) {
callee.property.name = "indexOf";
Expand Down
2 changes: 1 addition & 1 deletion scripts/build/build.js
Expand Up @@ -98,7 +98,7 @@ async function run(params) {
await execa("rm", ["-rf", ".cache"]);
}

const bundleCache = new Cache(".cache/", "v5");
const bundleCache = new Cache(".cache/", "v6");
await bundleCache.load();

console.log(chalk.inverse(" Building packages "));
Expand Down
27 changes: 9 additions & 18 deletions src/language-yaml/parser-yaml.js
Expand Up @@ -2,45 +2,36 @@

const createError = require("../common/parser-create-error");
const { hasPragma } = require("./pragma");
const { createNull, defineShortcut, mapNode } = require("./utils");
const { defineShortcut, mapNode } = require("./utils");

function defineShortcuts(node) {
switch (node.type) {
case "document":
defineShortcut(node, "head", () => node.children[0]);
defineShortcut(node, "body", () => node.children[1]);
break;
case "documentBody":
case "sequenceItem":
case "flowSequenceItem":
case "mappingKey":
case "mappingValue":
defineShortcut(node, "node", () => node.children[0]);
defineShortcut(node, "content", () => node.children[0]);
break;
case "mappingItem":
case "flowMappingItem":
defineShortcut(node, "key", () => node.children[0]);
defineShortcut(node, "value", () => node.children[1]);
break;
}
return node;
}

function parse(text) {
try {
const root = mapNode(require("yaml-unist-parser").parse(text), node => {
// replace explicit empty MappingKey/MappingValue with implicit one
if (
(node.type === "mappingKey" || node.type === "mappingValue") &&
node.children[0].type === "null" &&
node.leadingComments.length === 0 &&
node.trailingComments.length === 0 &&
node.endComments.length === 0
) {
return createNull();
}

defineShortcuts(node);
return node;
});
const root = mapNode(
require("yaml-unist-parser").parse(text),
defineShortcuts
);

/**
* suppress `comment not printed` error
Expand All @@ -53,7 +44,7 @@ function parse(text) {
return root;
} catch (error) {
// istanbul ignore next
throw error && error.name === "YAMLSyntaxError"
throw error && error.position
? createError(error.message, error.position)
: error;
}
Expand Down

0 comments on commit db2bc36

Please sign in to comment.