Skip to content

Commit

Permalink
fix(ngdoc): replace logic of custom parse tags by signature with ts t…
Browse files Browse the repository at this point in the history
…ag infos array #469 (#471)
  • Loading branch information
why520crazy committed Apr 13, 2023
1 parent 6b86d17 commit 518cfc7
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 9 deletions.
1 change: 1 addition & 0 deletions packages/a-lib/dialog/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
export * from './dialog.module';
export * from './dialog.service';
export * from './my-http-interceptor';
10 changes: 10 additions & 0 deletions packages/a-lib/dialog/my-http-interceptor.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { HttpEvent, HttpHandler, HttpInterceptor, HttpRequest } from '@angular/common/http';
import { Injectable } from '@angular/core';
import { Observable, of } from 'rxjs';

@Injectable()
export class MyHttpInterceptor implements HttpInterceptor {
intercept(req: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
return of(null);
}
}
50 changes: 50 additions & 0 deletions packages/ngdoc/src/ng-parser.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -493,6 +493,56 @@ describe('ng-parser', () => {
}
] as NgMethodDoc[]);
});

it('should generate service api when implements interface and param contains newline', () => {
const sourceText = `
interface HttpInterceptor {
/**
* Identifies and handles a given HTTP request.
* @param req The outgoing request object to handle.
* @param next The next interceptor in the chain, or the backend
* if no interceptors remain in the chain.
* @returns An observable of the event stream.
*/
intercept(req: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>>;
}
@Injectable()
export class MyHttpInterceptor implements HttpInterceptor {
intercept(req: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
return of(null);
}
}
`;
const { ngDocParser } = createTestNgDocParser('dialog', {
'/dialog/dialog.service.ts': sourceText
});
const docs = ngDocParser.parse('/dialog/*');

expect(docs[0].methods).toEqual([
{
name: 'intercept',
parameters: [
{
name: 'req',
description: '',
type: 'HttpRequest<any>'
},
{
name: 'next',
description: '',
type: 'HttpHandler'
}
],
returnValue: {
type: 'Observable<HttpEvent<any>>',
description: ''
},
description: 'Identifies and handles a given HTTP request.'
}
]);
});
});

describe('interface', () => {
Expand Down
19 changes: 10 additions & 9 deletions packages/ngdoc/src/parser/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -249,17 +249,18 @@ export function getDocTagsBySignature(symbol: ts.Signature): MethodDocTagResult
if (item.name !== 'param') {
result[item.name] = item;
} else {
let paramName: string;
let paramText: string;
const regexpResult = (ts.displayPartsToString(item.text) || '').match(/(^\S+)\s?(.*)$/);
if (regexpResult) {
paramText = regexpResult[2] || '';
paramName = regexpResult[1];
result[item.name] = result[item.name] || {};
if (item.text && item.text?.length > 0) {
const paramName = item.text[0].kind === 'parameterName' ? item.text[0].text : '';
const paramText = item.text.length > 2 ? item.text[2].text : '';

result[item.name][paramName] = {
name: paramName,
text: paramText
};
} else {
throw new Error(`[${item.name}-${item.text}]param comment format error`);
throw new Error(`[${item.name}] param comment format error`);
}
result[item.name] = result[item.name] || {};
result[item.name][paramName] = { ...item, text: paramText };
}
return result;
}, {});
Expand Down

0 comments on commit 518cfc7

Please sign in to comment.