Skip to content

Type rewrite further improvement #11

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 28 additions & 9 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ if (!fs.existsSync(moduleRootAbsolute)) {
}

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

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

function getModuleInfo(moduleId, parser) {
if (!moduleInfos[moduleId]) {
const moduleInfo = moduleInfos[moduleId] = {
namedExports: {}
};
if (!fileNodes[moduleId]) {
const absolutePath = path.join(process.cwd(), moduleRoot, moduleId + '.js');
if (!fs.existsSync(absolutePath)) {
Expand All @@ -37,6 +34,9 @@ function getModuleInfo(moduleId, parser) {
const file = fs.readFileSync(absolutePath, 'UTF-8');
fileNodes[moduleId] = parser.astBuilder.build(file, absolutePath);
}
const moduleInfo = moduleInfos[moduleId] = {
namedExports: {}
};
const node = fileNodes[moduleId];
if (node.program && node.program.body) {
const classDeclarations = {};
Expand Down Expand Up @@ -79,8 +79,12 @@ exports.astNodeVisitor = {
const nodes = node.program.body;
for (let i = 0, ii = nodes.length; i < ii; ++i) {
let node = nodes[i];
let leadingComments = node.leadingComments;
if (node.type === 'ExportNamedDeclaration' && node.declaration) {
node = node.declaration;
if (node.leadingComments) {
leadingComments = node.leadingComments;
}
}
if (node.type === 'ImportDeclaration') {
node.specifiers.forEach(specifier => {
Expand All @@ -98,6 +102,21 @@ exports.astNodeVisitor = {
default:
}
});
} else if (node.type === 'VariableDeclaration') {
for (const declaration of node.declarations) {
let declarationComments = leadingComments;
if (declaration.leadingComments) {
declarationComments = declaration.leadingComments;
}
if (declarationComments && declarationComments.length > 0) {
const comment = declarationComments[declarationComments.length - 1].value;
if (/@enum/.test(comment)) {
identifiers[declaration.id.name] = {
value: path.basename(currentSourceName)
};
}
}
}
} else if (node.type === 'ClassDeclaration') {
if (node.id && node.id.name) {
identifiers[node.id.name] = {
Expand Down Expand Up @@ -190,9 +209,9 @@ exports.astNodeVisitor = {
}

// Treat `@typedef`s like named exports
const typedefMatch = comment.value.replace(/\s*\*\s*/g, ' ').match(typedefRegEx);
if (typedefMatch) {
identifiers[typedefMatch[1]] = {
const typedefMatches = comment.value.replace(/\s*\*\s*/g, ' ').matchAll(typedefRegEx);
for (const match of typedefMatches) {
identifiers[match[1]] = {
value: path.basename(currentSourceName)
};
}
Expand All @@ -201,10 +220,10 @@ exports.astNodeVisitor = {
node.comments.forEach(comment => {
// Replace local types with the full `module:` path
Object.keys(identifiers).forEach(key => {
const eventRegex = new RegExp(`@(event |fires )${key}(\\s*)`, 'g');
const eventRegex = new RegExp(`@(event |fires )${key}([^A-Za-z])`, 'g');
replace(eventRegex);

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

function replace(regex) {
Expand Down