Skip to content

Commit 0b0c5ae

Browse files
committed
Drop TypeScript
1 parent 61ce76c commit 0b0c5ae

14 files changed

+98
-779
lines changed

.gitignore

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/.eslintcache
22
/coverage/
3-
/src/languages.ts
3+
/src/languages.js
44
/dist/
55
/node_modules/
66
/test.xml

bin/languages.ts renamed to bin/languages

+10-11
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
1-
import fs from "node:fs";
2-
import path from "node:path";
1+
#!/usr/bin/env node
32

4-
import LinguistLanguages from "linguist-languages";
5-
import prettier, { SupportLanguage } from "prettier";
3+
const fs = require("node:fs");
4+
const path = require("node:path");
5+
6+
const linguistLanguages = require("linguist-languages");
7+
const prettier = require("prettier");
68

79
function getSupportLanguages() {
8-
const supportLanguages: SupportLanguage[] = [];
10+
const supportLanguages = [];
911

10-
for (const language of Object.values(LinguistLanguages)) {
12+
for (const language of Object.values(linguistLanguages)) {
1113
if (language.aceMode === "xml") {
1214
const { type, color, aceMode, languageId, ...config } = language;
1315

@@ -33,15 +35,12 @@ function getSupportLanguages() {
3335
}
3436

3537
const languages = JSON.stringify(getSupportLanguages());
36-
const source = `import { SupportLanguage } from "prettier";
37-
const languages: SupportLanguage[] = ${languages};
38-
export default languages;`;
3938

4039
const packagePath = path.join(path.dirname(__dirname), "package.json");
4140
const packageConfig = JSON.parse(fs.readFileSync(packagePath, "utf8"));
4241
const { plugins, ...prettierConfig } = packageConfig.prettier;
4342

4443
fs.writeFileSync(
45-
"src/languages.ts",
46-
prettier.format(source, { parser: "typescript", ...prettierConfig })
44+
"src/languages.js",
45+
prettier.format(`module.exports = ${languages};`, { parser: "typescript", ...prettierConfig })
4746
);

bin/print.ts renamed to bin/print

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
#!./node_modules/.bin/ts-node
1+
#!/usr/bin/env node
22

33
import fs from "fs";
44
import prettier from "prettier";
@@ -11,7 +11,7 @@ const code = fs.existsSync(process.argv[2])
1111

1212
const options = {
1313
parser: "xml",
14-
plugins: [plugin as any as string],
14+
plugins: [plugin],
1515
xmlWhitespaceSensitivity: "ignore"
1616
};
1717

package.json

+4-21
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,10 @@
22
"name": "@prettier/plugin-xml",
33
"version": "2.2.0",
44
"description": "prettier plugin for XML",
5-
"main": "dist/plugin.js",
5+
"main": "src/plugin.js",
66
"scripts": {
77
"lint": "eslint --cache .",
8-
"prepare": "tsx bin/languages",
9-
"prepublishOnly": "tsc -p tsconfig.build.json",
8+
"prepare": "node bin/languages",
109
"print": "prettier --plugin=.",
1110
"test": "jest"
1211
},
@@ -25,43 +24,27 @@
2524
"prettier": ">=2.4.0"
2625
},
2726
"devDependencies": {
28-
"@types/jest": "^28.1.0",
29-
"@types/node": "^18.0.0",
30-
"@types/prettier": "^2.3.0",
31-
"@typescript-eslint/eslint-plugin": "^5.16.0",
32-
"@typescript-eslint/parser": "^5.16.0",
3327
"eslint": "^8.5.0",
3428
"eslint-config-prettier": "^8.0.0",
3529
"jest": "^28.1.0",
36-
"linguist-languages": "^7.21.0",
37-
"ts-jest": "^28.0.2",
38-
"ts-node": "^10.0.0",
39-
"tsx": "^3.8.2",
40-
"typescript": "^4.3.2"
30+
"linguist-languages": "^7.21.0"
4131
},
4232
"eslintConfig": {
4333
"extends": [
4434
"eslint:recommended",
45-
"plugin:@typescript-eslint/recommended",
4635
"prettier"
4736
],
4837
"env": {
4938
"es6": true,
5039
"jest": true,
5140
"node": true
52-
},
53-
"parser": "@typescript-eslint/parser",
54-
"rules": {
55-
"@typescript-eslint/no-explicit-any": "off",
56-
"@typescript-eslint/no-non-null-assertion": "off"
5741
}
5842
},
5943
"eslintIgnore": [
6044
"dist"
6145
],
6246
"jest": {
63-
"preset": "ts-jest",
64-
"testRegex": ".test.ts$"
47+
"testRegex": ".test.js$"
6548
},
6649
"prettier": {
6750
"plugins": [

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

+10-24
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,4 @@
1-
import { builders, utils } from "prettier/doc";
2-
import {
3-
AnyNode,
4-
ContentCtx,
5-
Doc,
6-
ElementCstNode,
7-
Embed,
8-
Options,
9-
Path
10-
} from "./types";
1+
const { builders, utils } = require("prettier/doc");
112

123
const {
134
dedentToRoot,
@@ -22,7 +13,7 @@ const {
2213

2314
// Replace the string content newlines within a doc tree with literallines so
2415
// that all of the indentation lines up appropriately
25-
function replaceNewlines(doc: Doc) {
16+
function replaceNewlines(doc) {
2617
return utils.mapDoc(doc, (currentDoc) =>
2718
typeof currentDoc === "string" && currentDoc.includes("\n")
2819
? currentDoc.split(/(\n)/g).map((v, i) => (i % 2 === 0 ? v : literalline))
@@ -31,16 +22,12 @@ function replaceNewlines(doc: Doc) {
3122
}
3223

3324
// Get the start and end element tags from the current node on the tree
34-
function getElementTags(
35-
path: Path<ElementCstNode>,
36-
opts: Options,
37-
print: (path: Path<AnyNode>) => Doc
38-
) {
25+
function getElementTags(path, opts, print) {
3926
const node = path.getValue();
4027
const { OPEN, Name, attribute, START_CLOSE, SLASH_OPEN, END_NAME, END } =
4128
node.children;
4229

43-
const parts: Doc[] = [OPEN[0].image, Name[0].image];
30+
const parts = [OPEN[0].image, Name[0].image];
4431

4532
if (attribute) {
4633
parts.push(
@@ -60,7 +47,7 @@ function getElementTags(
6047

6148
// Get the name of the parser that is represented by the given element node,
6249
// return null if a matching parser cannot be found
63-
function getParser(node: ElementCstNode, opts: Options) {
50+
function getParser(node, opts) {
6451
const { Name, attribute } = node.children;
6552
const parser = Name[0].image.toLowerCase();
6653

@@ -103,14 +90,14 @@ function getParser(node: ElementCstNode, opts: Options) {
10390

10491
// Get the source string that will be passed into the embedded parser from the
10592
// content of the inside of the element node
106-
function getSource(content: ContentCtx) {
93+
function getSource(content) {
10794
return content.chardata
10895
.map((node) => {
10996
const { SEA_WS, TEXT } = node.children;
11097
const [{ image }] = SEA_WS || TEXT;
11198

11299
return {
113-
offset: node.location!.startOffset,
100+
offset: node.location.startOffset,
114101
printed: image
115102
};
116103
})
@@ -119,7 +106,7 @@ function getSource(content: ContentCtx) {
119106
.join("");
120107
}
121108

122-
const embed: Embed = (path, print, textToDoc, opts) => {
109+
function embed(path, print, textToDoc, opts) {
123110
const node = path.getValue();
124111

125112
// If the node isn't an element node, then skip
@@ -143,8 +130,7 @@ const embed: Embed = (path, print, textToDoc, opts) => {
143130

144131
// Get the open and close tags of this element, then return the properly
145132
// formatted content enclosed within them
146-
const nodePath = path as Path<typeof node>;
147-
const { openTag, closeTag } = getElementTags(nodePath, opts, print);
133+
const { openTag, closeTag } = getElementTags(path, opts, print);
148134

149135
return group([
150136
openTag,
@@ -161,4 +147,4 @@ const embed: Embed = (path, print, textToDoc, opts) => {
161147
]);
162148
};
163149

164-
export default embed;
150+
module.exports = embed;
+9-17
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,13 @@
1-
import { parse as xmlToolsParse } from "@xml-tools/parser";
2-
import type { Parser } from "./types";
3-
4-
type LocatedError = Error & {
5-
loc: {
6-
start: { line: number; column: number };
7-
end: { line: number; column: number };
8-
};
9-
};
1+
const { parse: xmlToolsParse } = require("@xml-tools/parser");
102

11-
const parser: Parser = {
3+
const parser = {
124
parse(text) {
135
const { lexErrors, parseErrors, cst } = xmlToolsParse(text);
146

157
// If there are any lexical errors, throw the first of them as an error.
168
if (lexErrors.length > 0) {
179
const lexError = lexErrors[0];
18-
const error = new Error(lexError.message) as LocatedError;
10+
const error = new Error(lexError.message);
1911

2012
error.loc = {
2113
start: { line: lexError.line, column: lexError.column },
@@ -28,12 +20,12 @@ const parser: Parser = {
2820
// If there are any parse errors, throw the first of them as an error.
2921
if (parseErrors.length > 0) {
3022
const parseError = parseErrors[0];
31-
const error = new Error(parseError.message) as LocatedError;
23+
const error = new Error(parseError.message);
3224

3325
const { token } = parseError;
3426
error.loc = {
35-
start: { line: token.startLine!, column: token.startColumn! },
36-
end: { line: token.endLine!, column: token.endColumn! }
27+
start: { line: token.startLine, column: token.startColumn },
28+
end: { line: token.endLine, column: token.endColumn }
3729
};
3830

3931
throw error;
@@ -44,11 +36,11 @@ const parser: Parser = {
4436
},
4537
astFormat: "xml",
4638
locStart(node) {
47-
return node.location!.startOffset;
39+
return node.location.startOffset;
4840
},
4941
locEnd(node) {
50-
return node.location!.endOffset!;
42+
return node.location.endOffset;
5143
}
5244
};
5345

54-
export default parser;
46+
module.exports = parser;

src/plugin.ts renamed to src/plugin.js

+5-6
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
1-
import type { Plugin } from "./types";
2-
import languages from "./languages";
3-
import parser from "./parser";
4-
import printer from "./printer";
1+
const languages = require("./languages");
2+
const parser = require("./parser");
3+
const printer = require("./printer");
54

6-
const plugin: Plugin = {
5+
const plugin = {
76
languages,
87
parsers: {
98
xml: parser
@@ -43,4 +42,4 @@ const plugin: Plugin = {
4342
}
4443
};
4544

46-
export = plugin;
45+
module.exports = plugin;

0 commit comments

Comments
 (0)