@alova/wormhole
Version:
More modern openAPI generating solution for alova.js
89 lines (88 loc) • 2.85 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.commentHelper = exports.CommentHelper = void 0;
class CommentHelper {
constructor() {
this.type = 'line';
this.comment = '';
this.transformMap = {
'[deprecated]': '@deprecated',
'[required]': '[required]',
'[title]': (text) => {
const [, nextText = ''] = /\[title\](.*)/.exec(text) ?? [];
return `${nextText.trim()}\n---`;
},
'[cycle]': (text) => {
const [, nextText = ''] = /\[cycle\](.*)/.exec(text) ?? [];
return `${nextText.trim()}\n---`;
},
'[summary]': (text) => {
const [, nextText = ''] = /\[summary\](.*)/.exec(text) ?? [];
return `${nextText.trim()}\n---`;
},
};
}
static load(options) {
return new CommentHelper().load(options);
}
static parse(comment) {
return comment
.split('\n')
.map(span => span.replace(/^\/\*+|\*+\/|\*|\/\//, '').trim())
.filter(span => span);
}
static parseStr(comment) {
return this.parse(comment).join('\n');
}
load(options) {
this.type = options.type;
if (options.comment) {
this.comment = options.comment;
}
return this;
}
add(key, text = '') {
if (this.comment) {
this.comment += '\n';
}
this.comment += this.transformText(`${key} ${text}`)
.split('\n')
.map(span => `${this.preText()} ${span}`)
.join('\n');
return this;
}
end() {
const result = this.comment ? this.startText() + this.normalize() + this.endText() : '';
this.comment = '';
return result;
}
startText() {
return this.type === 'doc' ? '/**\n' : '';
}
preText() {
return this.type === 'doc' ? '*' : '//';
}
endText() {
return this.type === 'doc' ? '\n */\n' : '\n';
}
normalize() {
const comment = CommentHelper.parse(this.comment)
.map(span => `${this.preText()} ${span}`)
.join('\n');
return comment.replace('*/*', '* / *').replace('/*', '/ *').replace('*/', '* /');
}
transformText(text) {
text = text.trim();
if (this.type === 'line') {
return text;
}
const key = Object.keys(this.transformMap).find(key => text.startsWith(key));
if (key) {
const transformer = this.transformMap[key];
return typeof transformer === 'function' ? transformer(text) : (transformer ?? text);
}
return text;
}
}
exports.CommentHelper = CommentHelper;
exports.commentHelper = new CommentHelper();