Skip to content

Commit 3fbe864

Browse files
committed
Fix wacky code that was throwing a function call
1 parent 734a3c0 commit 3fbe864

2 files changed

Lines changed: 19 additions & 12 deletions

File tree

‎libraries/api-extractor/src/ApiDefinitionReference.ts‎

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -101,10 +101,10 @@ export default class ApiDefinitionReference {
101101
* and deconstructs it into an IApiDefinitionReference interface object.
102102
*/
103103
public static createFromString(apiReferenceExpr: string,
104-
reportError: (message: string) => void): ApiDefinitionReference {
104+
reportError: (message: string) => void): ApiDefinitionReference | undefined {
105105
if (!apiReferenceExpr || apiReferenceExpr.split(' ').length > 1) {
106106
reportError('An API item reference must use the notation: "@scopeName/packageName:exportName.memberName"');
107-
return;
107+
return undefined;
108108
}
109109

110110
const apiDefRefParts: IApiDefinintionReferenceParts = {
@@ -131,12 +131,14 @@ export default class ApiDefinitionReference {
131131
apiDefRefParts.memberName = parts[2] ? parts[2] : ''; // e.g. equals
132132
} else {
133133
// the export name is required
134-
throw reportError(`The API item reference contains an invalid "exportName.memberName"`
134+
reportError(`The API item reference contains an invalid "exportName.memberName"`
135135
+ ` expression: "${apiReferenceExpr}"`);
136+
return undefined;
136137
}
137138

138139
if (!apiReferenceExpr.match(ApiDefinitionReference._exportRegEx)) {
139-
throw reportError(`The API item reference contains invalid characters: "${apiReferenceExpr}"`);
140+
reportError(`The API item reference contains invalid characters: "${apiReferenceExpr}"`);
141+
return undefined;
140142
}
141143

142144
return ApiDefinitionReference.createFromParts(apiDefRefParts);

‎libraries/api-extractor/src/test/ApiDefinitionReference.test.ts‎

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { assert } from 'chai';
44
import ApiDefinitionReference from '../ApiDefinitionReference';
55

66
/* tslint:disable:no-function-expression - Mocha uses a poorly scoped "this" pointer */
7-
const capturedErrors: {
7+
let capturedErrors: {
88
message: string;
99
fileName: string;
1010
lineNumber: number;
@@ -14,6 +14,15 @@ function testErrorHandler(message: string, fileName: string, lineNumber: number)
1414
capturedErrors.push({ message, fileName, lineNumber });
1515
}
1616

17+
function clearCapturedErrors(): void {
18+
capturedErrors = [];
19+
}
20+
21+
function assertCapturedErrors(expectedMessages: string[]): void {
22+
assert.deepEqual(capturedErrors.map(x => x.message), expectedMessages,
23+
'The captured errors did not match the expected output.');
24+
}
25+
1726
describe('ApiDocumentation tests', function (): void {
1827
this.timeout(10000);
1928

@@ -52,16 +61,12 @@ describe('ApiDocumentation tests', function (): void {
5261
});
5362

5463
it('_parseApiReferenceExpression() without scope name and invalid memberName', function (): void {
64+
clearCapturedErrors();
5565
// This won't raise an error (based on our current decision to only show warnings in the *.api.ts
5666
// files if we can't find a reference)
5767
apiReferenceExpr = 'sp-core-library:Guid:equals';
58-
let caughtError: boolean = false;
59-
try {
60-
actual = ApiDefinitionReference.createFromString(apiReferenceExpr, console.log);
61-
} catch (error) {
62-
caughtError = true;
63-
}
64-
assert.equal(caughtError, true);
68+
actual = ApiDefinitionReference.createFromString(apiReferenceExpr, console.log);
69+
assertCapturedErrors([]);
6570
});
6671
});
6772
});

0 commit comments

Comments
 (0)