Skip to content

Commit 3126a4a

Browse files
committed
use pegjs
1 parent ad56372 commit 3126a4a

File tree

5 files changed

+268
-20
lines changed

5 files changed

+268
-20
lines changed

builtin_types_peg_rules.txt

Lines changed: 124 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
1+
BuiltinType
2+
// primitives
3+
= "number" &NoChar
4+
/ "string" &NoChar
5+
/ "boolean" &NoChar
6+
/ "undefined" &NoChar
7+
/ "null" &NoChar
8+
9+
// standard types
10+
/ "Object" &NoChar
11+
/ "Function" &NoChar
12+
/ "Symbol" &NoChar
13+
/ "Error" &NoChar
14+
/ "EvalError" &NoChar
15+
/ "InternalError" &NoChar
16+
/ "RangeError" &NoChar
17+
/ "ReferenceError" &NoChar
18+
/ "SyntaxError" &NoChar
19+
/ "TypeError" &NoChar
20+
/ "URIError" &NoChar
21+
/ "Date" &NoChar
22+
/ "RegExp" &NoChar
23+
/ "Array" &NoChar
24+
/ "Int8Array" &NoChar
25+
/ "Uint8Array" &NoChar
26+
/ "Uint8ClampedArray" &NoChar
27+
/ "Int16Array" &NoChar
28+
/ "Uint16Array" &NoChar
29+
/ "Int32Array" &NoChar
30+
/ "Uint32Array" &NoChar
31+
/ "Float32Array" &NoChar
32+
/ "Float64Array" &NoChar
33+
/ "BigInt64Array" &NoChar
34+
/ "BigUint64Array" &NoChar
35+
/ "Promise" &NoChar
36+
/ "Generator" &NoChar
37+
/ "GeneratorFunction" &NoChar
38+
/ "AsyncFunction" &NoChar
39+
/ "XMLHttpRequest" &NoChar
40+
/ "ArrayBuffer" &NoChar
41+
/ "SharedArrayBuffer" &NoChar
42+
/ "Atomics" &NoChar
43+
/ "DataView" &NoChar
44+
/ "JSON" &NoChar
45+
/ "Map" &NoChar
46+
/ "Set" &NoChar
47+
/ "WeakMap" &NoChar
48+
/ "WeakSet" &NoChar
49+
/ "Reflect" &NoChar
50+
/ "Proxy" &NoChar
51+
52+
// Geolocation
53+
/ "PositionError" &NoChar
54+
/ "PositionOptions" &NoChar
55+
/ "Position" &NoChar
56+
/ "Geolocation" &NoChar
57+
58+
// DOM
59+
/ "Attr" &NoChar
60+
/ "CDATASection" &NoChar
61+
/ "CharacterData" &NoChar
62+
/ "ChildNode" &NoChar
63+
/ "Comment" &NoChar
64+
/ "CustomEvent" &NoChar
65+
/ "Document" &NoChar
66+
/ "DocumentFragment" &NoChar
67+
/ "DocumentType" &NoChar
68+
/ "DOMError" &NoChar
69+
/ "DOMException" &NoChar
70+
/ "DOMImplementation" &NoChar
71+
/ "DOMString" &NoChar
72+
/ "DOMTimeStamp" &NoChar
73+
/ "DOMStringList" &NoChar
74+
/ "DOMTokenList" &NoChar
75+
/ "Element" &NoChar
76+
/ "Event" &NoChar
77+
/ "EventTarget" &NoChar
78+
/ "HTMLCollection" &NoChar
79+
/ "MutationObserver" &NoChar
80+
/ "MutationRecord" &NoChar
81+
/ "NamedNodeMap" &NoChar
82+
/ "Node" &NoChar
83+
/ "NodeFilter" &NoChar
84+
/ "NodeIterator" &NoChar
85+
/ "NodeList" &NoChar
86+
/ "NonDocumentTypeChildNode" &NoChar
87+
/ "ParentNode" &NoChar
88+
/ "ProcessingInstruction" &NoChar
89+
/ "Selection" &NoChar
90+
/ "Range" &NoChar
91+
/ "Text" &NoChar
92+
/ "TextDecoder" &NoChar
93+
/ "TextEncoder" &NoChar
94+
/ "TimeRanges" &NoChar
95+
/ "TreeWalker" &NoChar
96+
/ "URL" &NoChar
97+
/ "Window" &NoChar
98+
/ "Worker" &NoChar
99+
/ "XMLDocument" &NoChar
100+
101+
// HTML Types
102+
/ "HTMLElement" &NoChar
103+
/ "HTMLCanvasElement" &NoChar
104+
/ "HTMLImageElement" &NoChar
105+
/ "HTMLVideoElement" &NoChar
106+
107+
// PointerEvent
108+
/ "PointerEvent" &NoChar
109+
110+
// TouchEvent
111+
/ "TouchEvent" &NoChar
112+
113+
// MouseEvent
114+
/ "MouseEvent" &NoChar
115+
116+
// TypeScript types
117+
/ "*" &NoChar
118+
// "?" will be catched by a special rule in type_rewrite_peg_rules
119+
/ "any" &NoChar
120+
/ "void" &NoChar
121+
/ "Partial" &NoChar
122+
123+
// Other special types
124+
/ "Class" &NoChar

index.js

Lines changed: 29 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ const path = require('path');
22
const fs = require('fs');
33
const env = require('jsdoc/env');
44
const addInherited = require('jsdoc/augment').addInherited;
5+
const peg = require("pegjs");
56

67
const config = env.conf.typescript;
78
if (!config) {
@@ -24,6 +25,28 @@ const slashRegEx = /\\/g;
2425
const moduleInfos = {};
2526
const fileNodes = {};
2627

28+
const pegRules = fs.readFileSync(path.join(__dirname, "./type_rewrite_peg_rules.txt"), 'utf8');
29+
const pegBuiltinRules = fs.readFileSync(path.join(__dirname, "./builtin_types_peg_rules.txt"), 'utf8');
30+
31+
function buildTypeRewriteRules(identifiers, parser, currentSourceName) {
32+
const keys = Object.keys(identifiers).sort().reverse();
33+
let rules = 'RewriteType\n';
34+
let first = true;
35+
for (const key of keys) {
36+
const identifier = identifiers[key];
37+
const absolutePath = path.resolve(path.dirname(currentSourceName), identifier.value);
38+
const moduleId = path.relative(path.join(process.cwd(), moduleRoot), absolutePath).replace(/\.js$/, '');
39+
if (getModuleInfo(moduleId, parser)) {
40+
const exportName = identifier.defaultImport ? getDefaultExportName(moduleId, parser) : key;
41+
const delimiter = identifier.defaultImport ? '~' : getDelimiter(moduleId, exportName, parser);
42+
const replacement = `module:${moduleId.replace(slashRegEx, '/')}${exportName ? delimiter + exportName : ''}`;
43+
rules += ` ${first ? '=' : '/'} "${key}" &NoChar { return "${replacement}" }\n`;
44+
first = false;
45+
}
46+
}
47+
return pegRules + '\n' + pegBuiltinRules + '\n' + rules;
48+
}
49+
2750
function getModuleInfo(moduleId, parser) {
2851
if (!moduleInfos[moduleId]) {
2952
const moduleInfo = moduleInfos[moduleId] = {
@@ -198,30 +221,16 @@ exports.astNodeVisitor = {
198221
}
199222
});
200223

201-
node.comments.forEach(comment => {
224+
if (Object.keys(identifiers).length > 0) {
202225
// Replace local types with the full `module:` path
203-
Object.keys(identifiers).forEach(key => {
204-
const eventRegex = new RegExp(`@(event |fires )${key}(\\s*)`, 'g');
205-
replace(eventRegex);
206226

207-
const typeRegex = new RegExp(`@(.*[{<|,]\\s*[!?]?)${key}(=?\\s*[}>|,])`, 'g');
208-
replace(typeRegex);
227+
const rules = buildTypeRewriteRules(identifiers, parser, currentSourceName);
228+
const rewriter = peg.generate(rules);
209229

210-
function replace(regex) {
211-
if (regex.test(comment.value)) {
212-
const identifier = identifiers[key];
213-
const absolutePath = path.resolve(path.dirname(currentSourceName), identifier.value);
214-
const moduleId = path.relative(path.join(process.cwd(), moduleRoot), absolutePath).replace(/\.js$/, '');
215-
if (getModuleInfo(moduleId, parser)) {
216-
const exportName = identifier.defaultImport ? getDefaultExportName(moduleId, parser) : key;
217-
const delimiter = identifier.defaultImport ? '~' : getDelimiter(moduleId, exportName, parser);
218-
let replacement = `module:${moduleId.replace(slashRegEx, '/')}${exportName ? delimiter + exportName : ''}`;
219-
comment.value = comment.value.replace(regex, '@$1' + replacement + '$2');
220-
}
221-
}
222-
}
230+
node.comments.forEach(comment => {
231+
comment.value = rewriter.parse(comment.value);
223232
});
224-
});
233+
}
225234
}
226235
}
227236
}

package-lock.json

Lines changed: 13 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,5 +17,8 @@
1717
"repository": {
1818
"type": "git",
1919
"url": "git://github.com/openlayers/jsdoc-plugin-typescript.git"
20+
},
21+
"dependencies": {
22+
"pegjs": "^0.10.0"
2023
}
2124
}

type_rewrite_peg_rules.txt

Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
{
2+
function flatten(input) {
3+
return Array.isArray(input) ? input.map(i => flatten(i)).join("") : input;
4+
}
5+
}
6+
7+
All
8+
= all: Lines { return flatten(all); }
9+
10+
Lines
11+
= Line "\n" Lines
12+
/ Line
13+
14+
Line
15+
= _ "*" _ TypeExpecting _ ComplexType ("#" [^\n\r]*)?
16+
/ _ "*" _ CurlyTypeExpecting _ CurlyType [^\n\r]*
17+
/ _ "*" _ !TypeExpecting !CurlyTypeExpecting [^\n\r]*
18+
/ _ [^\n\r]* [^*\n\r]*
19+
20+
CurlyTypeExpecting
21+
= "@param"
22+
/ "@property"
23+
/ "@return" "s"?
24+
/ "@typedef"
25+
/ "@type"
26+
/ "@this"
27+
/ "@enum"
28+
29+
TypeExpecting
30+
= "@fires"
31+
/ "@event"
32+
33+
CurlyType
34+
= "{" _ TypeList _ "}"
35+
36+
TypeList
37+
= ComplexType _b "|" _b TypeList
38+
/ ComplexType
39+
40+
ComplexType
41+
= [?!]? ComplexTypeUnmodified "="?
42+
/ "?" // short curcuit for the builtin type "?"
43+
44+
ComplexTypeUnmodified
45+
= "(" _ TypeList _ ")"
46+
/ Spread
47+
/ GenericType
48+
/ FunctionType
49+
/ SimpleType
50+
/ InlineObjectType
51+
52+
InlineObjectType
53+
= "{" _b InlineObjectPropertyList _b "}"
54+
55+
InlineObjectPropertyList
56+
= InlineObjectProperty _ "," _b InlineObjectPropertyList
57+
/ InlineObjectProperty
58+
59+
InlineObjectProperty
60+
= [A-Za-z][A-Za-z0-9_]* _ ":" _b ComplexType
61+
62+
Spread
63+
= "..." ComplexType
64+
65+
FunctionType
66+
= "function" _ "(" _ (FunctionThis? _b ParameterList)? ")" (":" _b ComplexType)?
67+
68+
FunctionThis
69+
= "this:"
70+
/ "new:"
71+
72+
SimpleType
73+
= RewriteType
74+
/ BuiltinType
75+
/ ConvertedType
76+
/ GenericParameter
77+
78+
NoChar
79+
= [^A-Za-z0-9_]
80+
81+
GenericType
82+
= SimpleType "."? "<" ParameterList ">"
83+
84+
ParameterList
85+
= TypeList "," _b ParameterList
86+
/ TypeList
87+
88+
ConvertedType
89+
= "module:" [a-zA-Z/]+ (("~" / ".") [a-zA-Z]+)? &NoChar
90+
91+
GenericParameter
92+
= [A-Z] &NoChar
93+
94+
_b "break or whitespace"
95+
= _ "\n" _ "*" _
96+
/ _
97+
98+
_ "whitespace"
99+
= [ \t]*

0 commit comments

Comments
 (0)