Skip to content

Commit 7ea76b2

Browse files
authored
Merge pull request microsoft#240 from Microsoft/pgonzal/ae-pipe-bug
Fix a bug with handling of @link tags for API Extractor
2 parents edbb57a + 73c78fb commit 7ea76b2

4 files changed

Lines changed: 75 additions & 39 deletions

File tree

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
{
2+
"changes": [
3+
{
4+
"packageName": "@microsoft/api-extractor",
5+
"comment": "Fix a bug with parsing of @link tags",
6+
"type": "patch"
7+
}
8+
],
9+
"packageName": "@microsoft/api-extractor",
10+
"email": "pgonzal@users.noreply.github.com"
11+
}

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

Lines changed: 38 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,13 @@ import Tokenizer from './Tokenizer';
77
import ResolvedApiItem from './ResolvedApiItem';
88

99
export default class DocElementParser {
10-
1110
/**
12-
* Matches only strings that contain characters for words.
13-
* Any non word characters or spaces, will be present in the third entry in the match results
14-
* if they exist.
11+
* Used to validate the display text for an \@link tag. The display text can contain any
12+
* characters except for certain AEDoc delimiters: "@", "|", "{", "}".
13+
* This RegExp matches the first bad character.
14+
* Example: "Microsoft's {spec}" --> "{"
1515
*/
16-
private static _wordRegEx: RegExp = /^([\w\s]*)/;
16+
private static _displayTextBadCharacterRegEx: RegExp = /[@|{}]/;
1717

1818
/**
1919
* Matches a href reference. This is used to get an idea whether a given reference is for an href
@@ -142,67 +142,72 @@ export default class DocElementParser {
142142
}
143143

144144
// Make sure there are no extra pipes
145-
let pipeSplitContent: string[] = tokenItem.text.split('|');
146-
pipeSplitContent = pipeSplitContent.map( value => {
145+
const pipeSplitContent: string[] = tokenItem.text.split('|').map(value => {
147146
if (value) {
148147
return value.trim();
149148
}
150149
});
151150
if (pipeSplitContent.length > 2) {
152151
documentation.reportError('The {@link} tag contains more than one pipe character ("|")');
153-
return;
152+
return undefined;
154153
}
155154

155+
const addressPart: string = pipeSplitContent[0];
156+
const displayTextPart: string = pipeSplitContent.length > 1 ? pipeSplitContent[1] : '';
157+
156158
// Try to guess if the tokenContent is a link or API definition reference
157159
let linkDocElement: ICodeLinkElement | IHrefLinkElement;
158-
if (tokenItem.text.match(this._hrefRegEx)) {
159-
const urlContent: string[] = pipeSplitContent[0].split(' ');
160-
160+
if (this._hrefRegEx.test(addressPart)) {
161161
// Make sure only a single URL is given
162-
if (urlContent.length > 1 && urlContent[1] !== '' ) {
162+
if (addressPart.indexOf(' ') >= 0) {
163163
documentation.reportError('The {@link} tag contains additional spaces after the URL;'
164164
+ ' if the URL contains spaces, encode them using %20; for display text, use a pipe delimiter ("|")');
165-
return;
165+
return undefined;
166166
}
167167

168168
linkDocElement = {
169169
kind: 'linkDocElement',
170170
referenceType: 'href',
171-
targetUrl: urlContent[0],
172-
value: ''
171+
targetUrl: addressPart
172+
// ("value" will be assigned below)
173173
};
174174

175175
} else {
176176
// we are processing an API definition reference
177177
const apiDefitionRef: ApiDefinitionReference = ApiDefinitionReference.createFromString(
178-
pipeSplitContent[0],
178+
addressPart,
179179
documentation.reportError
180180
);
181181

182182
// Once we can locate local API definitions, an error should be reported here if not found.
183-
if (apiDefitionRef) {
184-
185-
linkDocElement = {
186-
kind: 'linkDocElement',
187-
referenceType: 'code',
188-
scopeName: apiDefitionRef.scopeName,
189-
packageName: apiDefitionRef.packageName,
190-
exportName: apiDefitionRef.exportName,
191-
memberName: apiDefitionRef.memberName
192-
};
183+
if (!apiDefitionRef) {
184+
return undefined;
193185
}
186+
187+
linkDocElement = {
188+
kind: 'linkDocElement',
189+
referenceType: 'code',
190+
scopeName: apiDefitionRef.scopeName,
191+
packageName: apiDefitionRef.packageName,
192+
exportName: apiDefitionRef.exportName,
193+
memberName: apiDefitionRef.memberName
194+
// ("value" will be assigned below)
195+
};
194196
}
195197

196198
// If a display name is given, ensure it only contains characters for words.
197-
if (linkDocElement && pipeSplitContent.length > 1) {
198-
const displayTextParts: string[] = pipeSplitContent[1].match(this._wordRegEx);
199-
if (displayTextParts && displayTextParts[0].length !== pipeSplitContent[1].length) {
200-
documentation.reportError(`The {@link} tag\'s display text contains unsupported `
201-
+ ` symbols: "${pipeSplitContent[1]}"`);
202-
return;
199+
if (displayTextPart) {
200+
const match: RegExpExecArray | undefined = this._displayTextBadCharacterRegEx.exec(displayTextPart);
201+
if (match) {
202+
documentation.reportError(`The {@link} tag\'s display text contains an unsupported`
203+
+ ` character: "${match[0]}"`);
204+
return undefined;
203205
}
204206
// Full match is valid text
205-
linkDocElement.value = displayTextParts[0].trim();
207+
linkDocElement.value = displayTextPart;
208+
} else {
209+
// If the display text is not explicitly provided, then use the address as the display text
210+
linkDocElement.value = addressPart;
206211
}
207212

208213
return linkDocElement;

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

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ describe('DocElementParser tests', function (): void {
101101
kind: 'linkDocElement',
102102
referenceType: 'href',
103103
targetUrl: 'https://github.com/OfficeDev/office-ui-fabric-react',
104-
value: ''
104+
value: 'https://github.com/OfficeDev/office-ui-fabric-react'
105105
} as IHrefLinkElement
106106
];
107107
const actualSummary: IDocElement[] = DocElementParser.parse(myDocumentedClass.documentation, tokenizer);
@@ -203,7 +203,7 @@ describe('DocElementParser tests', function (): void {
203203
kind: 'linkDocElement',
204204
referenceType: 'href',
205205
targetUrl: 'http://wikipedia.org/pixel_units',
206-
value: ''
206+
value: 'http://wikipedia.org/pixel_units'
207207
} as IHrefLinkElement
208208
];
209209
const expectedParam: IParam = {
@@ -239,7 +239,7 @@ describe('DocElementParser tests', function (): void {
239239
const linkDocElement: IHrefLinkElement = (docElements[0] as IHrefLinkElement);
240240
assert.equal(linkDocElement.referenceType, 'href');
241241
assert.equal(linkDocElement.targetUrl, 'https://microsoft.com');
242-
assert.equal(linkDocElement.value, '');
242+
assert.equal(linkDocElement.value, 'https://microsoft.com');
243243
assertCapturedErrors([]);
244244
});
245245

@@ -284,7 +284,25 @@ describe('DocElementParser tests', function (): void {
284284
+ ' contains spaces, encode them using %20; for display text, use a pipe delimiter ("|")']);
285285
});
286286

287-
it('Should parse @link with API defintion reference', (): void => {
287+
it('Should reject @link with bad display text character', (): void => {
288+
clearCapturedErrors();
289+
290+
const docs: string = '{@link https://example.com | Ex@ample}';
291+
const tokenizer: Tokenizer = new Tokenizer(docs, console.log);
292+
293+
let docElements: IDocElement[];
294+
/* tslint:disable-next-line:no-any */
295+
let errorMessage: any;
296+
try {
297+
docElements = DocElementParser.parse(myDocumentedClass.documentation, tokenizer);
298+
} catch (error) {
299+
errorMessage = error;
300+
}
301+
assert.isUndefined(errorMessage);
302+
assertCapturedErrors(['The {@link} tag\'s display text contains an unsupported character: "@"']);
303+
});
304+
305+
it('Should parse @link with API definition reference', (): void => {
288306
clearCapturedErrors();
289307
const docs: string = '{@link @microsoft/sp-core-library:Guid.equals}';
290308
const tokenizer: Tokenizer = new Tokenizer(docs, console.log);

‎libraries/api-extractor/testInputs/example3/example3-output.json‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -344,15 +344,17 @@
344344
"scopeName": "",
345345
"packageName": "",
346346
"exportName": "publicEnum",
347-
"memberName": ""
347+
"memberName": "",
348+
"value": "publicEnum"
348349
},
349350
{
350351
"kind": "linkDocElement",
351352
"referenceType": "code",
352353
"scopeName": "",
353354
"packageName": "",
354355
"exportName": "_internalEnum",
355-
"memberName": ""
356+
"memberName": "",
357+
"value": "_internalEnum"
356358
}
357359
],
358360
"remarks": [],

0 commit comments

Comments
 (0)