From 6638d4f03ffeaec6fb5c53c18d19e8a69e70e36c Mon Sep 17 00:00:00 2001 From: Russell Cullen <1281199+russellcullen@users.noreply.github.com> Date: Wed, 22 Jan 2025 23:54:01 -0500 Subject: [PATCH] Prevent throwing an exception for invalid methods when skip flag is set (#137) --- src/parser/ValueParser.ts | 1 + test/parser-test.ts | 48 ++++++++++++++++++++++++++++++++++++++- test/utils.ts | 8 +++++-- 3 files changed, 54 insertions(+), 3 deletions(-) diff --git a/src/parser/ValueParser.ts b/src/parser/ValueParser.ts index 681c7f5..4838353 100644 --- a/src/parser/ValueParser.ts +++ b/src/parser/ValueParser.ts @@ -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; diff --git a/test/parser-test.ts b/test/parser-test.ts index 2d8c4d4..7ef9b6f 100644 --- a/test/parser-test.ts +++ b/test/parser-test.ts @@ -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); + }); }); diff --git a/test/utils.ts b/test/utils.ts index b1968ac..e72ddac 100644 --- a/test/utils.ts +++ b/test/utils.ts @@ -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 = new Set()): void { +export function withTempSkipParser(sourceCode: string, handler: (parser: Parser) => void, predefinedTypes: Set = 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 = new Set()): void { + withTempSkipParser(sourceCode, handler, predefinedTypes, false) +} + export function withTempMethodParser(methodCode: string, handler: (parseFunc: () => Method | null) => void, predefinedTypes: Set = new Set(), customTypesCode: string = ''): void { const sourceCode = ` ${customTypesCode}