зеркало из https://github.com/microsoft/ts-gyb.git
Prevent throwing an exception for invalid methods when skip flag is set (#137)
This commit is contained in:
Родитель
ee0ba9ac3e
Коммит
6638d4f03f
|
@ -602,6 +602,7 @@ export class ValueParser {
|
|||
if (error instanceof ValueParserError) {
|
||||
if (this.skipInvalidMethods) {
|
||||
this.logger.warnSkippedNode(decrationNode, error.message, error.guide);
|
||||
return;
|
||||
}
|
||||
|
||||
throw error;
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
import { describe, it } from 'mocha';
|
||||
import { expect } from 'chai';
|
||||
import { withTempParser } from './utils';
|
||||
import { withTempParser, withTempSkipParser } from './utils';
|
||||
import { ValueParserError } from '../src/parser/ValueParserError';
|
||||
import { BasicTypeValue, ValueTypeKind } from '../src/types';
|
||||
|
||||
|
@ -122,4 +122,50 @@ describe('Parser', () => {
|
|||
expect(() => parser.parse()).to.throw(ValueParserError).with.property('message', 'it has multiple parameters');
|
||||
});
|
||||
});
|
||||
|
||||
it('Multiple parameters with skip flag', () => {
|
||||
const sourceCode = `
|
||||
/**
|
||||
* @shouldExport true
|
||||
*/
|
||||
interface MockedInterface {
|
||||
/**
|
||||
* This documentation should be skipped
|
||||
*/
|
||||
multipleParamsMethod(foo: string, bar: number);
|
||||
/**
|
||||
* This is an example documentation for the member
|
||||
*/
|
||||
mockedMember: string;
|
||||
/**
|
||||
* This is an example documentation for the method
|
||||
*/
|
||||
mockedMethod(): void;
|
||||
}
|
||||
`;
|
||||
|
||||
withTempSkipParser(sourceCode, (parser) => {
|
||||
const modules = parser.parse();
|
||||
expect(modules).to.deep.equal([
|
||||
{
|
||||
name: 'MockedInterface',
|
||||
exportedInterfaceBases: [],
|
||||
documentation: '',
|
||||
customTags: {},
|
||||
members: [{
|
||||
name: 'mockedMember',
|
||||
type: { kind: ValueTypeKind.basicType, value: BasicTypeValue.string },
|
||||
documentation: 'This is an example documentation for the member',
|
||||
}],
|
||||
methods: [{
|
||||
name: 'mockedMethod',
|
||||
parameters: [],
|
||||
returnType: null,
|
||||
isAsync: false,
|
||||
documentation: 'This is an example documentation for the method',
|
||||
}],
|
||||
},
|
||||
]);
|
||||
}, new Set(), true);
|
||||
});
|
||||
});
|
||||
|
|
|
@ -6,17 +6,21 @@ import { Parser } from '../src/parser/Parser';
|
|||
import { ValueParserError } from '../src/parser/ValueParserError';
|
||||
import { Method, ValueType } from '../src/types';
|
||||
|
||||
export function withTempParser(sourceCode: string, handler: (parser: Parser) => void, predefinedTypes: Set<string> = new Set()): void {
|
||||
export function withTempSkipParser(sourceCode: string, handler: (parser: Parser) => void, predefinedTypes: Set<string> = new Set(), skipInvalidMethods: boolean = false) {
|
||||
const tempPath = fs.mkdtempSync(`${os.tmpdir()}/`);
|
||||
const filePath = path.join(tempPath, `${UUID()}.ts`);
|
||||
fs.writeFileSync(filePath, sourceCode);
|
||||
|
||||
const parser = new Parser([filePath], predefinedTypes, false, undefined, undefined);
|
||||
const parser = new Parser([filePath], predefinedTypes, skipInvalidMethods, undefined, undefined);
|
||||
handler(parser);
|
||||
|
||||
fs.rmdirSync(tempPath, { recursive: true });
|
||||
}
|
||||
|
||||
export function withTempParser(sourceCode: string, handler: (parser: Parser) => void, predefinedTypes: Set<string> = new Set()): void {
|
||||
withTempSkipParser(sourceCode, handler, predefinedTypes, false)
|
||||
}
|
||||
|
||||
export function withTempMethodParser(methodCode: string, handler: (parseFunc: () => Method | null) => void, predefinedTypes: Set<string> = new Set(), customTypesCode: string = ''): void {
|
||||
const sourceCode = `
|
||||
${customTypesCode}
|
||||
|
|
Загрузка…
Ссылка в новой задаче