From b4586249b0b9a58152a5541e507baa293e29cfe0 Mon Sep 17 00:00:00 2001 From: Mariusz Miszuta Date: Mon, 6 Jul 2020 09:49:59 +0200 Subject: [PATCH] fixed issue where if enum name starts with letter I it gets cut off from name in generated interface --- src/cli.ts | 41 ++++++++++++++++++++++++++++++++++++++++- 1 file changed, 40 insertions(+), 1 deletion(-) diff --git a/src/cli.ts b/src/cli.ts index 3be089a..326e5a9 100644 --- a/src/cli.ts +++ b/src/cli.ts @@ -183,6 +183,36 @@ function getReference(commentedNodePath: ASTPath) { + const enums: String[] = []; + ast.find(jscodeshift.ExpressionStatement).forEach(path => { + if (!path.node.comments) { + return; + } + if (path.node.expression.type === 'AssignmentExpression') { + path.node.comments.forEach(comment => { + if (/@enum/.test(comment.value)) { + enums.push(comment.value); + } + }); + } + }); + const enumNames: RegExp[] = enums + .map(en => { + const temp = en.split('@name').pop(); + if (!temp) { + return undefined; + } + const name = temp.split('\n').shift(); + if (!name) { + return undefined; + } + return new RegExp(name.trim()); + }) + .filter( + (i): i is RegExp => { + return i instanceof RegExp; + }, + ); ast.find(jscodeshift.FunctionDeclaration).forEach(path => { if (!path.node.comments) { return; @@ -195,7 +225,16 @@ function constructorsToInterfaces(ast: Collection) { path.node.comments = interfaceComments; path.node.comments.forEach(comment => { comment.value = comment.value.replace(/^([\s\*]+@interface\s+)I/gm, '$1'); - comment.value = comment.value.replace(/^([\s\*]+@property\s+\{.*?\.)I([^.]+\})/gm, '$1$2'); + const commentValueLines = comment.value.split('\n'); + commentValueLines.forEach((line, i) => { + if (!enumNames.some(name => name.test(line))) { + commentValueLines[i] = line.replace( + /^([\s\*]+@property\s+\{.*?\.)I([^.]+\})/gm, + '$1$2', + ); + } + }); + comment.value = commentValueLines.join('\n'); }); } else { // Otherwise this is a service