Prevent throwing an exception for invalid methods when skip flag is set (#137)

This commit is contained in:
Russell Cullen 2025-01-22 23:54:01 -05:00 коммит произвёл GitHub
Родитель ee0ba9ac3e
Коммит 6638d4f03f
Не найден ключ, соответствующий данной подписи
Идентификатор ключа GPG: B5690EEEBB952194
3 изменённых файлов: 54 добавлений и 3 удалений

Просмотреть файл

@ -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}