Skip to content

Commit 3c52df0

Browse files
committed
feat: support esm project to parse documentation
1 parent 092a96e commit 3c52df0

File tree

12 files changed

+619
-284
lines changed

12 files changed

+619
-284
lines changed

.eslintignore

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

.eslintrc

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

.mocharc.js

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,5 @@ process.env.NODE_ENV = "test";
33
module.exports = {
44
recursive: true,
55
reporter: "dot",
6-
spec: [
7-
"packages/**/*.spec.js"
8-
]
6+
spec: ["packages/**/*.spec.js"]
97
};

eslint.config.mjs

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
import typescriptEslint from "@typescript-eslint/eslint-plugin";
2+
import typescriptParser from "@typescript-eslint/parser";
3+
import pluginPrettierRecommended from "eslint-plugin-prettier/recommended";
4+
import globals from "globals";
5+
import {join} from "node:path";
6+
7+
export default [
8+
{
9+
ignores: [
10+
"**/coverage",
11+
"**/lib",
12+
"**/dist",
13+
"processes.config.js",
14+
"**/snapshots",
15+
"**/templates",
16+
"**/docs/**",
17+
"**/docs-references/**"
18+
]
19+
},
20+
{
21+
files: ["**/*.{js,mjs,cjs,jsx,mjsx,ts,tsx,mtsx}"],
22+
languageOptions: {
23+
ecmaVersion: "latest",
24+
sourceType: "module",
25+
parser: typescriptParser,
26+
parserOptions: {
27+
ecmaVersion: "latest",
28+
sourceType: "module",
29+
tsconfigRootDir: join(import.meta.dirname, "tsconfig.eslint.json")
30+
},
31+
globals: {
32+
...globals.node
33+
}
34+
},
35+
plugins: {
36+
"@typescript-eslint": typescriptEslint
37+
},
38+
rules: {
39+
"@typescript-eslint/ban-ts-comment": 0,
40+
"@typescript-eslint/camelcase": 0,
41+
"@typescript-eslint/no-inferrable-types": 0,
42+
"@typescript-eslint/explicit-function-return-type": 0,
43+
"@typescript-eslint/explicit-module-boundary-types": 0,
44+
"@typescript-eslint/no-unused-vars": 0,
45+
"@typescript-eslint/no-explicit-any": 0,
46+
"@typescript-eslint/no-non-null-assertion": 0
47+
}
48+
},
49+
// {
50+
// files: ["**/*.spec.{js,mjs,cjs,jsx,mjsx,ts,tsx,mtsx}"], // or any other pattern
51+
// ignores: ["docs/**", "docs-references/**"],
52+
// plugins: {
53+
// vitest
54+
// },
55+
// rules: {
56+
// ...vitest.configs.recommended.rules, // you can also use vitest.configs.all.rules to enable all rules
57+
// "vitest/consistent-test-it": ["error", {fn: "it", withinDescribe: "it"}],
58+
// "vitest/no-alias-methods": "error",
59+
// "vitest/expect-expect": "off"
60+
// }
61+
// },
62+
pluginPrettierRecommended
63+
];

package.json

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@
1010
"test": "yarn test:lint && yarn test:coverage",
1111
"test:unit": "cross-env NODE_ENV=test mocha",
1212
"test:coverage": "cross-env NODE_ENV=test nyc mocha",
13-
"test:lint": "eslint packages --ext .js",
14-
"test:lint:fix": "eslint packages --ext .js --fix",
13+
"test:lint": "eslint",
14+
"test:lint:fix": "eslint --fix",
1515
"build": "monorepo build",
1616
"sync:packages": "monorepo sync packages",
1717
"release": "semantic-release",
@@ -43,23 +43,27 @@
4343
"lerna": "3.22.0"
4444
},
4545
"devDependencies": {
46+
"@typescript-eslint/eslint-plugin": "8.8.0",
47+
"@typescript-eslint/parser": "8.8.0",
4648
"@tsed/monorepo-utils": "1.11.0",
4749
"chai": "4.2.0",
4850
"chai-as-promised": "7.1.1",
4951
"cross-env": "7.0.2",
50-
"eslint": "^7.10.0",
51-
"eslint-config-prettier": "6.12.0",
52-
"eslint-plugin-prettier": "3.1.4",
53-
"eslint-plugin-mocha": "8.0.0",
52+
"eslint": "9.13.0",
53+
"eslint-config-prettier": "9.1.0",
54+
"eslint-plugin-prettier": "5.2.1",
55+
"eslint-plugin-simple-import-sort": "12.1.1",
56+
"eslint-plugin-vitest": "0.5.4",
5457
"proxyquire": "2.1.3",
5558
"husky": "4.3.0",
5659
"lint-staged": "10.4.0",
5760
"mocha": "8.1.3",
5861
"nyc": "15.1.0",
59-
"prettier": "2.1.2",
62+
"prettier": "3.3.3",
6063
"semantic-release": "17.2.3",
6164
"sinon": "9.0.3",
62-
"sinon-chai": "3.5.0"
65+
"sinon-chai": "3.5.0",
66+
"typescript": "5.6.3"
6367
},
6468
"workspaces": {
6569
"packages": [

packages/ts-doc/src/highlight/index.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
"use strict";
22

33
const {context} = require("../context");
4-
const KEYWORDS = /(\benum\b|\bstatic\b|\bclass\b|\binterface\b|\bprivate\b|\bpublic\b|\bconst\b|\blet\b|\bprotected\b|\bimplements\b|\bconstructor\b|\breadonly\b|\babstract\b|\bimport\b|\bexport\b|\bas\b|\bfrom\b|\bextends\b)/g;
4+
const KEYWORDS =
5+
/(\benum\b|\bstatic\b|\bclass\b|\binterface\b|\bprivate\b|\bpublic\b|\bconst\b|\blet\b|\bprotected\b|\bimplements\b|\bconstructor\b|\breadonly\b|\babstract\b|\bimport\b|\bexport\b|\bas\b|\bfrom\b|\bextends\b)/g;
56
const TYPES = /(\bany\b|\bstring\b|\bboolean\b|\bnumber\b|\bDate\b|\bvoid\b)/g;
67
const SEPARATOR = /(:|;|,|\(|\)|{|}|\[|\]| = )/g;
78

packages/ts-doc/src/models/DocFile.js

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
const {context} = require("../context");
22
const path = require("path");
3-
const fs = require("fs");
3+
const fs = require("fs/promises");
4+
const fsSync = require("fs");
45
const normalizePath = require("normalize-path");
56
const readPkgUp = require("read-pkg-up");
67
const mapExported = new Map();
@@ -13,7 +14,7 @@ class DocFile {
1314
constructor(file) {
1415
this.file = normalizePath(file);
1516
this.symbols = new Map();
16-
this.contents = fs.readFileSync(file).toString();
17+
this.contents = fsSync.readFileSync(file).toString();
1718
}
1819

1920
get path() {
@@ -50,21 +51,21 @@ class DocFile {
5051
};
5152
}
5253

53-
requireModule() {
54+
async importModule() {
5455
const {modulePath} = this.module;
5556
let file = path.join(modulePath, "index.js");
5657

57-
if (fs.existsSync(path.join(modulePath, "package.json"))) {
58-
const pkg = require(path.join(modulePath, "package.json"));
58+
if (fsSync.existsSync(path.join(modulePath, "package.json"))) {
59+
const pkg = JSON.parse(await fs.readFile(path.join(modulePath, "package.json"), {encoding: "utf-8"}));
5960
file = path.join(modulePath, pkg.main);
6061
}
6162

6263
if (mapExported.has(file)) {
6364
return mapExported.get(file);
6465
}
6566

66-
if (fs.existsSync(file)) {
67-
mapExported.set(file, require(file));
67+
if (fsSync.existsSync(file)) {
68+
mapExported.set(file, await import(file));
6869
return mapExported.get(file);
6970
}
7071

packages/ts-doc/src/models/DocSymbol.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,7 @@ class DocSymbol {
159159
});
160160
}
161161

162-
setDocFile(docFile) {
162+
async setDocFile(docFile) {
163163
this.docFile = docFile;
164164

165165
if (this.relativePath.match("decorators") && this.symbolType === "function") {
@@ -178,7 +178,7 @@ class DocSymbol {
178178
if (this.symbolType !== "interface") {
179179
this.private = true;
180180

181-
const exported = this.docFile.requireModule();
181+
const exported = await this.docFile.importModule();
182182

183183
if (exported) {
184184
const symbolPrivate = exported[this.symbolName.trim()];

packages/ts-doc/src/models/DocSymbol.spec.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ const context = require("../context/context");
44
const {expect} = require("../test/tools");
55

66
describe("DocSymbol", () => {
7-
before(() => {
7+
before(async () => {
88
context.github = "https://github.com/repo";
99
context.version = "1.0.0";
1010

@@ -26,7 +26,7 @@ describe("DocSymbol", () => {
2626

2727
this.docSymbol = new DocSymbol();
2828
this.docSymbol.symbolName = "SymbolName";
29-
this.docSymbol.setDocFile({
29+
await this.docSymbol.setDocFile({
3030
file: path.join(process.cwd(), "/packages/common/di/lib/file.d.ts"),
3131
path: path.join(process.cwd(), "/packages/common/di/lib/file.ts"),
3232
srcPath: path.join(process.cwd(), "/packages/common/di/src/file.ts"),
@@ -39,7 +39,7 @@ describe("DocSymbol", () => {
3939
pkgName: "common",
4040
subPkgName: "di"
4141
},
42-
requireModule() {
42+
importModule() {
4343
return {};
4444
}
4545
});

packages/ts-doc/src/parsers/DocParser.js

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -33,14 +33,14 @@ class DocParser {
3333
this.contents = contents;
3434
}
3535

36-
static parse(docFile) {
36+
static async parse(docFile) {
3737
const {symbols} = new DocParser(docFile.contents).parse();
3838

39-
symbols.forEach((symbol) => {
40-
symbol.setDocFile(docFile);
41-
symbol = context.symbols.push(symbol);
42-
docFile.symbols.set(symbol.symbolName, symbol);
43-
});
39+
for (const [, symbol] of symbols) {
40+
await symbol.setDocFile(docFile);
41+
const newSymbol = context.symbols.push(symbol);
42+
docFile.symbols.set(newSymbol.symbolName, newSymbol);
43+
}
4444

4545
return docFile.symbols;
4646
}

0 commit comments

Comments
 (0)