Skip to content

Commit 6638d4f

Browse files
Prevent throwing an exception for invalid methods when skip flag is set (#137)
1 parent ee0ba9a commit 6638d4f

File tree

3 files changed

+54
-3
lines changed

3 files changed

+54
-3
lines changed

src/parser/ValueParser.ts

+1
Original file line numberDiff line numberDiff line change
@@ -602,6 +602,7 @@ export class ValueParser {
602602
if (error instanceof ValueParserError) {
603603
if (this.skipInvalidMethods) {
604604
this.logger.warnSkippedNode(decrationNode, error.message, error.guide);
605+
return;
605606
}
606607

607608
throw error;

test/parser-test.ts

+47-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { describe, it } from 'mocha';
22
import { expect } from 'chai';
3-
import { withTempParser } from './utils';
3+
import { withTempParser, withTempSkipParser } from './utils';
44
import { ValueParserError } from '../src/parser/ValueParserError';
55
import { BasicTypeValue, ValueTypeKind } from '../src/types';
66

@@ -122,4 +122,50 @@ describe('Parser', () => {
122122
expect(() => parser.parse()).to.throw(ValueParserError).with.property('message', 'it has multiple parameters');
123123
});
124124
});
125+
126+
it('Multiple parameters with skip flag', () => {
127+
const sourceCode = `
128+
/**
129+
* @shouldExport true
130+
*/
131+
interface MockedInterface {
132+
/**
133+
* This documentation should be skipped
134+
*/
135+
multipleParamsMethod(foo: string, bar: number);
136+
/**
137+
* This is an example documentation for the member
138+
*/
139+
mockedMember: string;
140+
/**
141+
* This is an example documentation for the method
142+
*/
143+
mockedMethod(): void;
144+
}
145+
`;
146+
147+
withTempSkipParser(sourceCode, (parser) => {
148+
const modules = parser.parse();
149+
expect(modules).to.deep.equal([
150+
{
151+
name: 'MockedInterface',
152+
exportedInterfaceBases: [],
153+
documentation: '',
154+
customTags: {},
155+
members: [{
156+
name: 'mockedMember',
157+
type: { kind: ValueTypeKind.basicType, value: BasicTypeValue.string },
158+
documentation: 'This is an example documentation for the member',
159+
}],
160+
methods: [{
161+
name: 'mockedMethod',
162+
parameters: [],
163+
returnType: null,
164+
isAsync: false,
165+
documentation: 'This is an example documentation for the method',
166+
}],
167+
},
168+
]);
169+
}, new Set(), true);
170+
});
125171
});

test/utils.ts

+6-2
Original file line numberDiff line numberDiff line change
@@ -6,17 +6,21 @@ import { Parser } from '../src/parser/Parser';
66
import { ValueParserError } from '../src/parser/ValueParserError';
77
import { Method, ValueType } from '../src/types';
88

9-
export function withTempParser(sourceCode: string, handler: (parser: Parser) => void, predefinedTypes: Set<string> = new Set()): void {
9+
export function withTempSkipParser(sourceCode: string, handler: (parser: Parser) => void, predefinedTypes: Set<string> = new Set(), skipInvalidMethods: boolean = false) {
1010
const tempPath = fs.mkdtempSync(`${os.tmpdir()}/`);
1111
const filePath = path.join(tempPath, `${UUID()}.ts`);
1212
fs.writeFileSync(filePath, sourceCode);
1313

14-
const parser = new Parser([filePath], predefinedTypes, false, undefined, undefined);
14+
const parser = new Parser([filePath], predefinedTypes, skipInvalidMethods, undefined, undefined);
1515
handler(parser);
1616

1717
fs.rmdirSync(tempPath, { recursive: true });
1818
}
1919

20+
export function withTempParser(sourceCode: string, handler: (parser: Parser) => void, predefinedTypes: Set<string> = new Set()): void {
21+
withTempSkipParser(sourceCode, handler, predefinedTypes, false)
22+
}
23+
2024
export function withTempMethodParser(methodCode: string, handler: (parseFunc: () => Method | null) => void, predefinedTypes: Set<string> = new Set(), customTypesCode: string = ''): void {
2125
const sourceCode = `
2226
${customTypesCode}

0 commit comments

Comments
 (0)