Skip to content

Commit 53ca85e

Browse files
committed
TypeScript
1 parent c1e2d09 commit 53ca85e

File tree

18 files changed

+912
-672
lines changed

18 files changed

+912
-672
lines changed

bin/print

Lines changed: 0 additions & 10 deletions
This file was deleted.

bin/print.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
#!./node_modules/.bin/ts-node
2+
3+
import fs from "fs";
4+
import prettier from "prettier";
5+
6+
import plugin from "../src/plugin";
7+
import { XMLOptions } from "../src/types";
8+
9+
const code = fs.existsSync(process.argv[2])
10+
? fs.readFileSync(process.argv[2], "utf-8")
11+
: process.argv.slice(2).join(" ").replace(/\\n/g, "\n");
12+
13+
const options: Partial<XMLOptions> = {
14+
parser: "xml",
15+
plugins: [plugin],
16+
xmlWhitespaceSensitivity: "ignore"
17+
};
18+
19+
console.log(prettier.format(code, options));

package.json

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,14 @@
2323
"prettier": ">=1.10"
2424
},
2525
"devDependencies": {
26+
"@types/jest": "^26.0.23",
27+
"@types/prettier": "^2.2.3",
2628
"eslint": "^7.0.0",
2729
"eslint-config-prettier": "^8.0.0",
28-
"jest": "^27.0.1"
30+
"jest": "^27.0.1",
31+
"ts-jest": "^27.0.2",
32+
"ts-node": "^10.0.0",
33+
"typescript": "^4.3.2"
2934
},
3035
"eslintConfig": {
3136
"extends": [
@@ -51,7 +56,8 @@
5156
}
5257
},
5358
"jest": {
54-
"testRegex": ".test.js$"
59+
"preset": "ts-jest",
60+
"testRegex": ".test.ts$"
5561
},
5662
"prettier": {
5763
"trailingComma": "none"

src/embed.js renamed to src/embed.ts

Lines changed: 22 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,9 @@
1-
const { builders, utils } = require("prettier/doc");
1+
import { ContentCtx, ElementCstNode } from "@xml-tools/parser";
2+
import type { Doc, FastPath, ParserOptions, Printer } from "prettier";
3+
import { builders, utils } from "prettier/doc";
4+
5+
import { XMLAST } from "./types";
6+
27
const {
38
concat,
49
dedentToRoot,
@@ -10,12 +15,11 @@ const {
1015
literalline,
1116
softline
1217
} = builders;
13-
const { mapDoc, stripTrailingHardline } = utils;
1418

1519
// Replace the string content newlines within a doc tree with literallines so
1620
// that all of the indentation lines up appropriately
17-
const replaceNewlines = (doc) =>
18-
mapDoc(doc, (currentDoc) =>
21+
function replaceNewlines(doc: Doc) {
22+
return utils.mapDoc(doc, (currentDoc) =>
1923
typeof currentDoc === "string" && currentDoc.includes("\n")
2024
? concat(
2125
currentDoc
@@ -24,14 +28,15 @@ const replaceNewlines = (doc) =>
2428
)
2529
: currentDoc
2630
);
31+
}
2732

2833
// Get the start and end element tags from the current node on the tree
29-
const getElementTags = (path, print) => {
30-
const node = path.getValue();
34+
function getElementTags(path: FastPath<XMLAST>, print: (path: FastPath<XMLAST>) => Doc) {
35+
const node = path.getValue() as ElementCstNode;
3136
const { OPEN, Name, attribute, START_CLOSE, SLASH_OPEN, END_NAME, END } =
3237
node.children;
3338

34-
const parts = [OPEN[0].image, Name[0].image];
39+
const parts: Doc[] = [OPEN[0].image, Name[0].image];
3540

3641
if (attribute) {
3742
parts.push(
@@ -51,7 +56,7 @@ const getElementTags = (path, print) => {
5156

5257
// Get the name of the parser that is represented by the given element node,
5358
// return null if a matching parser cannot be found
54-
const getParser = (node, opts) => {
59+
function getParser(node: ElementCstNode, opts: ParserOptions<XMLAST>) {
5560
const { Name, attribute } = node.children;
5661
const parser = Name[0].image.toLowerCase();
5762

@@ -81,6 +86,7 @@ const getParser = (node, opts) => {
8186
if (
8287
opts.plugins.some(
8388
(plugin) =>
89+
typeof plugin !== "string" &&
8490
plugin.parsers &&
8591
Object.prototype.hasOwnProperty.call(plugin.parsers, parser)
8692
)
@@ -89,26 +95,27 @@ const getParser = (node, opts) => {
8995
}
9096

9197
return null;
92-
};
98+
}
9399

94100
// Get the source string that will be passed into the embedded parser from the
95101
// content of the inside of the element node
96-
const getSource = (content) =>
97-
content.chardata
102+
function getSource(content: ContentCtx) {
103+
return content.chardata
98104
.map((node) => {
99105
const { SEA_WS, TEXT } = node.children;
100106
const [{ image }] = SEA_WS || TEXT;
101107

102108
return {
103-
offset: node.location.startOffset,
109+
offset: node.location!.startOffset,
104110
printed: image
105111
};
106112
})
107113
.sort(({ offset }) => offset)
108114
.map(({ printed }) => printed)
109115
.join("");
116+
}
110117

111-
const embed = (path, print, textToDoc, opts) => {
118+
const embed: Printer<XMLAST>["embed"] = (path, print, textToDoc, opts) => {
112119
const node = path.getValue();
113120

114121
// If the node isn't an element node, then skip
@@ -140,7 +147,7 @@ const embed = (path, print, textToDoc, opts) => {
140147
literalline,
141148
dedentToRoot(
142149
replaceNewlines(
143-
stripTrailingHardline(textToDoc(getSource(content), { parser }))
150+
utils.stripTrailingHardline(textToDoc(getSource(content), { parser }))
144151
)
145152
),
146153
hardline,
@@ -149,4 +156,4 @@ const embed = (path, print, textToDoc, opts) => {
149156
);
150157
};
151158

152-
module.exports = embed;
159+
export default embed;

src/languages.js

Lines changed: 0 additions & 129 deletions
This file was deleted.

src/parser.js

Lines changed: 0 additions & 34 deletions
This file was deleted.

src/parser.ts

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import type { Parser } from "prettier";
2+
import { parse as xmlToolsParse } from "@xml-tools/parser";
3+
4+
const parser: Parser = {
5+
parse(text) {
6+
const { lexErrors, parseErrors, cst } = xmlToolsParse(text);
7+
8+
if (lexErrors.length > 0 || parseErrors.length > 0) {
9+
throw Error("Error parsing XML");
10+
}
11+
12+
return cst;
13+
},
14+
astFormat: "xml",
15+
locStart(node) {
16+
if (node.location) {
17+
return node.location.startOffset;
18+
}
19+
return node.startOffset;
20+
},
21+
locEnd(node) {
22+
if (node.location) {
23+
return node.location.endOffset;
24+
}
25+
return node.endOffset;
26+
}
27+
};
28+
29+
export default parser;

0 commit comments

Comments
 (0)