Skip to content

Commit

Permalink
Prevent throwing an exception for invalid methods when skip flag is s…
Browse files Browse the repository at this point in the history
…et (#137)
  • Loading branch information
russellcullen authored Jan 23, 2025
1 parent ee0ba9a commit 6638d4f
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 3 deletions.
1 change: 1 addition & 0 deletions src/parser/ValueParser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
48 changes: 47 additions & 1 deletion test/parser-test.ts
Original file line number Diff line number Diff line change
@@ -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';

Expand Down Expand Up @@ -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);
});
});
8 changes: 6 additions & 2 deletions test/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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}
Expand Down

0 comments on commit 6638d4f

Please sign in to comment.