@@ -2,6 +2,7 @@ const path = require('path');
22const fs = require ( 'fs' ) ;
33const env = require ( 'jsdoc/env' ) ;
44const addInherited = require ( 'jsdoc/augment' ) . addInherited ;
5+ const peg = require ( "pegjs" ) ;
56
67const config = env . conf . typescript ;
78if ( ! config ) {
@@ -24,6 +25,28 @@ const slashRegEx = /\\/g;
2425const moduleInfos = { } ;
2526const 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 ( / \. j s $ / , '' ) ;
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+
2750function getModuleInfo ( moduleId , parser ) {
2851 if ( ! moduleInfos [ moduleId ] ) {
2952 if ( ! fileNodes [ 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 ( / \. j s $ / , '' ) ;
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 }
0 commit comments