Skip to content

Commit f247302

Browse files
authored
Merge pull request #11 from KlausBenndorf/fix-local-types-conversion
Type rewrite further improvement
2 parents e8d92bf + 6d8f002 commit f247302

File tree

1 file changed

+28
-9
lines changed

1 file changed

+28
-9
lines changed

index.js

Lines changed: 28 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ if (!fs.existsSync(moduleRootAbsolute)) {
1717
}
1818

1919
const importRegEx = /import\(["']([^"']*)["']\)\.([^ \.\|\}><,\)=#\n]*)([ \.\|\}><,\)=#\n])/g;
20-
const typedefRegEx = /@typedef \{[^\}]*\} (\S+)/;
20+
const typedefRegEx = /@typedef \{[^\}]*\} (\S+)/g;
2121
const noClassdescRegEx = /@(typedef|module|type)/;
2222
const slashRegEx = /\\/g;
2323

@@ -26,9 +26,6 @@ const fileNodes = {};
2626

2727
function getModuleInfo(moduleId, parser) {
2828
if (!moduleInfos[moduleId]) {
29-
const moduleInfo = moduleInfos[moduleId] = {
30-
namedExports: {}
31-
};
3229
if (!fileNodes[moduleId]) {
3330
const absolutePath = path.join(process.cwd(), moduleRoot, moduleId + '.js');
3431
if (!fs.existsSync(absolutePath)) {
@@ -37,6 +34,9 @@ function getModuleInfo(moduleId, parser) {
3734
const file = fs.readFileSync(absolutePath, 'UTF-8');
3835
fileNodes[moduleId] = parser.astBuilder.build(file, absolutePath);
3936
}
37+
const moduleInfo = moduleInfos[moduleId] = {
38+
namedExports: {}
39+
};
4040
const node = fileNodes[moduleId];
4141
if (node.program && node.program.body) {
4242
const classDeclarations = {};
@@ -79,8 +79,12 @@ exports.astNodeVisitor = {
7979
const nodes = node.program.body;
8080
for (let i = 0, ii = nodes.length; i < ii; ++i) {
8181
let node = nodes[i];
82+
let leadingComments = node.leadingComments;
8283
if (node.type === 'ExportNamedDeclaration' && node.declaration) {
8384
node = node.declaration;
85+
if (node.leadingComments) {
86+
leadingComments = node.leadingComments;
87+
}
8488
}
8589
if (node.type === 'ImportDeclaration') {
8690
node.specifiers.forEach(specifier => {
@@ -98,6 +102,21 @@ exports.astNodeVisitor = {
98102
default:
99103
}
100104
});
105+
} else if (node.type === 'VariableDeclaration') {
106+
for (const declaration of node.declarations) {
107+
let declarationComments = leadingComments;
108+
if (declaration.leadingComments) {
109+
declarationComments = declaration.leadingComments;
110+
}
111+
if (declarationComments && declarationComments.length > 0) {
112+
const comment = declarationComments[declarationComments.length - 1].value;
113+
if (/@enum/.test(comment)) {
114+
identifiers[declaration.id.name] = {
115+
value: path.basename(currentSourceName)
116+
};
117+
}
118+
}
119+
}
101120
} else if (node.type === 'ClassDeclaration') {
102121
if (node.id && node.id.name) {
103122
identifiers[node.id.name] = {
@@ -190,9 +209,9 @@ exports.astNodeVisitor = {
190209
}
191210

192211
// Treat `@typedef`s like named exports
193-
const typedefMatch = comment.value.replace(/\s*\*\s*/g, ' ').match(typedefRegEx);
194-
if (typedefMatch) {
195-
identifiers[typedefMatch[1]] = {
212+
const typedefMatches = comment.value.replace(/\s*\*\s*/g, ' ').matchAll(typedefRegEx);
213+
for (const match of typedefMatches) {
214+
identifiers[match[1]] = {
196215
value: path.basename(currentSourceName)
197216
};
198217
}
@@ -201,10 +220,10 @@ exports.astNodeVisitor = {
201220
node.comments.forEach(comment => {
202221
// Replace local types with the full `module:` path
203222
Object.keys(identifiers).forEach(key => {
204-
const eventRegex = new RegExp(`@(event |fires )${key}(\\s*)`, 'g');
223+
const eventRegex = new RegExp(`@(event |fires )${key}([^A-Za-z])`, 'g');
205224
replace(eventRegex);
206225

207-
const typeRegex = new RegExp(`@(.*[{<|,]\\s*[!?]?)${key}(=?\\s*[}>|,])`, 'g');
226+
const typeRegex = new RegExp(`@(.*[{<|,(!?:]\\s*)${key}([^A-Za-z].*?\}|\})`, 'g');
208227
replace(typeRegex);
209228

210229
function replace(regex) {

0 commit comments

Comments
 (0)