-
-
Notifications
You must be signed in to change notification settings - Fork 33
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
192 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,2 @@ | ||
node_modules/ | ||
/.eslintcache | ||
/node_modules/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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: { | ||
} | ||
}, | ||
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |