Skip to content

Commit

Permalink
Fix up lint
Browse files Browse the repository at this point in the history
  • Loading branch information
kddnewton committed Nov 12, 2019
1 parent 289f2a6 commit 8c9ad3d
Show file tree
Hide file tree
Showing 6 changed files with 192 additions and 1 deletion.
22 changes: 22 additions & 0 deletions .eslintrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
{
"extends": [
"airbnb-base",
"prettier"
],
"env": {
"jest": true,
"node": true
},
"rules": {
"no-unused-vars": [
"error",
{
"argsIgnorePattern": "^_",
"varsIgnorePattern": "^_"
}
],
"prefer-destructuring": "off",
"prefer-object-spread": "off",
"prefer-spread": "off"
}
}
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
node_modules/
/.eslintcache
/node_modules/
Empty file modified bin/print
100644 → 100755
Empty file.
38 changes: 38 additions & 0 deletions src/parse.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
const parser = require("fast-xml-parser");

const translate = (node, name) => {
if (typeof node !== "object") {
return { type: "leaf", name, attrs: {}, value: node.toString() };
}

const attrs = {};
const value = [];

Object.keys(node).forEach(key => {
if (key.startsWith("@_")) {
attrs[key.slice(2)] = node[key];
} else if (key !== "#text") {
value.push(translate(node[key], key));
}
});

if (Object.prototype.hasOwnProperty.call(node, "#text")) {
return { type: "leaf", name, attrs, value: node["#text"] };
}

return { type: "node", name, attrs, value };
};

const parse = (text, _parsers, _opts) => Object.assign(
{},
translate(parser.parse(text, {
allowBooleanAttributes: true,
attributeNamePrefix : "@_",
ignoreAttributes: false,
parseAttributeValue: true,
textNodeName: "#text"
})),
{ type: "root" }
);

module.exports = parse;
46 changes: 46 additions & 0 deletions src/plugin.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
const parse = require("./parse");
const print = require("./print");

// These functions are necessary or the print with cursor function breaks.
// Eventually we should fill them in with the correct metadata, but the parser
// doesn't provide it at the moment.
const locStart = _node => 0;
const locEnd = _node => 0;

const plugin = {
languages: [
{
name: "XML",
parsers: ["xml"],
extensions: [".xml"]
}
],
parsers: {
xml: {
parse,
astFormat: "xml",
locStart,
locEnd
}
},
printers: {
xml: {
print
}
},
options: {
xmlSelfClosingTags: {
since: "1.19.1",
category: "XML",
type: "boolean",
default: true,
description: "Whether or not to allow self closing XML tags."
}
},
defaultOptions: {
printWidth: 80,
tabWidth: 2
}
};

module.exports = plugin;
84 changes: 84 additions & 0 deletions src/print.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
const { concat, group, hardline, indent, join, line, softline } = require("prettier").doc.builders;

const getFirstNonBlankLine = originalText => originalText.split("\n").find(
text => text.trim().length !== 0
);

const printAttrs = attrs => {
if (Object.keys(attrs).length === 0) {
return "";
}

const parts = [line];

Object.keys(attrs).forEach((key, index) => {
if (index !== 0) {
parts.push(line);
}
parts.push(key, "=", '"', attrs[key], '"');
});

return group(concat([indent(concat(parts)), softline]));
};

const genericPrint = (path, opts, print) => {
const { type, name, attrs, value } = path.getValue();

switch (type) {
case "leaf": {
const parts = [
"<",
name,
printAttrs(attrs)
];

if (!value && opts.xmlSelfClosingTags) {
return group(concat(parts.concat(" />")));
}

return group(concat(parts.concat(
">",
indent(concat([
softline,
value
])),
softline,
"</",
name,
">"
)));
}
case "node":
return group(concat([
"<",
name,
printAttrs(attrs),
">",
indent(concat([
hardline,
join(hardline, path.map(print, "value"))
])),
hardline,
"</",
name,
">"
]));
case "root": {
const parts = [
join(hardline, path.map(print, "value")),
hardline
];

const firstNonBlankLine = getFirstNonBlankLine(opts.originalText);
if (firstNonBlankLine && firstNonBlankLine.startsWith("<?xml")) {
parts.unshift(firstNonBlankLine, hardline);
}

return concat(parts);
}
default:
throw new Error(`Unsupported node encountered: ${type}`);
}
};

module.exports = genericPrint;

0 comments on commit 8c9ad3d

Please sign in to comment.