Skip to content

Commit 047cedb

Browse files
committed
fix(app): Invalid links to a class when the class name includes an interface name
fix #1239
1 parent a5bee44 commit 047cedb

File tree

6 files changed

+74
-25
lines changed

6 files changed

+74
-25
lines changed

src/app/engines/dependencies.engine.ts

Lines changed: 37 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -63,8 +63,8 @@ export class DependenciesEngine {
6363
}
6464

6565
private updateModulesDeclarationsExportsTypes() {
66-
let mergeTypes = entry => {
67-
let directive = this.findInCompodocDependencies(
66+
const mergeTypes = entry => {
67+
const directive = this.findInCompodocDependencies(
6868
entry.name,
6969
this.directives,
7070
entry.file
@@ -74,7 +74,7 @@ export class DependenciesEngine {
7474
entry.id = directive.data.id;
7575
}
7676

77-
let component = this.findInCompodocDependencies(
77+
const component = this.findInCompodocDependencies(
7878
entry.name,
7979
this.components,
8080
entry.file
@@ -84,7 +84,7 @@ export class DependenciesEngine {
8484
entry.id = component.data.id;
8585
}
8686

87-
let pipe = this.findInCompodocDependencies(entry.name, this.pipes, entry.file);
87+
const pipe = this.findInCompodocDependencies(entry.name, this.pipes, entry.file);
8888
if (typeof pipe.data !== 'undefined') {
8989
entry.type = 'pipe';
9090
entry.id = pipe.data.id;
@@ -147,24 +147,38 @@ export class DependenciesEngine {
147147
private findInCompodocDependencies(name, data, file?): IApiSourceResult<any> {
148148
let _result = {
149149
source: 'internal',
150-
data: undefined
150+
data: undefined,
151+
score: 0
151152
};
152153
let nameFoundCounter = 0;
153154
if (data && data.length > 0) {
154155
for (let i = 0; i < data.length; i++) {
155156
if (typeof name !== 'undefined') {
156157
if (typeof file !== 'undefined') {
157158
if (
159+
name === data[i].name &&
160+
file.replace(/\\/g, '/').indexOf(data[i].file) !== -1
161+
) {
162+
nameFoundCounter += 1;
163+
_result.data = data[i];
164+
_result.score = 2;
165+
} else if (
158166
name.indexOf(data[i].name) !== -1 &&
159167
file.replace(/\\/g, '/').indexOf(data[i].file) !== -1
160168
) {
161169
nameFoundCounter += 1;
162170
_result.data = data[i];
171+
_result.score = 1;
163172
}
164173
} else {
165-
if (name.indexOf(data[i].name) !== -1) {
174+
if (name === data[i].name) {
166175
nameFoundCounter += 1;
167176
_result.data = data[i];
177+
_result.score = 2;
178+
} else if (name.indexOf(data[i].name) !== -1) {
179+
nameFoundCounter += 1;
180+
_result.data = data[i];
181+
_result.score = 1;
168182
}
169183
}
170184
}
@@ -176,25 +190,25 @@ export class DependenciesEngine {
176190
for (let i = 0; i < data.length; i++) {
177191
if (typeof name !== 'undefined') {
178192
if (typeof file !== 'undefined') {
179-
if (
180-
name === data[i].name &&
181-
file.replace(/\\/g, '/').indexOf(data[i].file) !== -1
182-
) {
193+
if (name === data[i].name) {
183194
found = true;
184195
_result.data = data[i];
196+
_result.score = 2;
185197
}
186198
} else {
187199
if (name === data[i].name) {
188200
found = true;
189201
_result.data = data[i];
202+
_result.score = 2;
190203
}
191204
}
192205
}
193206
}
194207
if (!found) {
195208
_result = {
196209
source: 'internal',
197-
data: undefined
210+
data: undefined,
211+
score: 0
198212
};
199213
}
200214
}
@@ -203,8 +217,8 @@ export class DependenciesEngine {
203217
}
204218

205219
private manageDuplicatesName() {
206-
let processDuplicates = (element, index, array) => {
207-
let elementsWithSameName = _.filter(array, { name: element.name });
220+
const processDuplicates = (element, index, array) => {
221+
const elementsWithSameName = _.filter(array, { name: element.name });
208222
if (elementsWithSameName.length > 1) {
209223
// First element is the reference for duplicates
210224
for (let i = 1; i < elementsWithSameName.length; i++) {
@@ -251,15 +265,19 @@ export class DependenciesEngine {
251265
() => AngularApiUtil.findApi(name)
252266
];
253267

268+
let bestScore = 0;
269+
let bestResult = undefined;
270+
254271
for (let searchFunction of searchFunctions) {
255-
let result = searchFunction();
272+
const result = searchFunction();
256273

257-
if (result.data) {
258-
return result;
274+
if (result.data && result.score > bestScore) {
275+
bestScore = result.score;
276+
bestResult = result;
259277
}
260278
}
261279

262-
return undefined;
280+
return bestResult;
263281
}
264282

265283
public update(updatedData): void {
@@ -372,7 +390,7 @@ export class DependenciesEngine {
372390
}
373391

374392
public findInCompodoc(name: string) {
375-
let mergedData = _.concat(
393+
const mergedData = _.concat(
376394
[],
377395
this.modules,
378396
this.components,
@@ -390,7 +408,7 @@ export class DependenciesEngine {
390408
this.miscellaneous.variables,
391409
this.miscellaneous.functions
392410
);
393-
let result = _.find(mergedData, { name: name } as any);
411+
const result = _.find(mergedData, { name: name } as any);
394412
return result || false;
395413
}
396414

src/app/engines/html-engine-helpers/function-signature.helper.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@ export class FunctionSignatureHelper implements IHtmlEngineHelper {
1414
return `${arg.name}${this.getOptionalString(arg)}: () => void`;
1515
}
1616

17-
let argums = arg.function.map(argu => {
18-
let _result = DependenciesEngine.find(argu.type);
17+
const argums = arg.function.map(argu => {
18+
const _result = DependenciesEngine.find(argu.type);
1919
if (_result) {
2020
if (_result.source === 'internal') {
2121
let path = _result.data.type;
@@ -26,7 +26,7 @@ export class FunctionSignatureHelper implements IHtmlEngineHelper {
2626
_result.data.name
2727
}.html">${argu.type}</a>`;
2828
} else {
29-
let path = AngularVersionUtil.getApiLink(
29+
const path = AngularVersionUtil.getApiLink(
3030
_result.data,
3131
Configuration.mainData.angularVersion
3232
);
@@ -35,7 +35,7 @@ export class FunctionSignatureHelper implements IHtmlEngineHelper {
3535
)}: <a href="${path}" target="_blank">${argu.type}</a>`;
3636
}
3737
} else if (BasicTypeUtil.isKnownType(argu.type)) {
38-
let path = BasicTypeUtil.getTypeUrl(argu.type);
38+
const path = BasicTypeUtil.getTypeUrl(argu.type);
3939
return `${argu.name}${this.getOptionalString(
4040
arg
4141
)}: <a href="${path}" target="_blank">${argu.type}</a>`;
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
export interface IApiSourceResult<T> {
22
source: string;
33
data: T | undefined;
4+
score: number;
45
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,26 @@
11
export * from './app.module';
2+
3+
interface Aa {}
4+
5+
// The prefix of class name matches the interface name
6+
class AaBb {}
7+
// ^^
8+
9+
// The intermediate of class name matches the interface name
10+
class BbAaCc {}
11+
// ^^
12+
13+
// The suffix of class name matches the interface name
14+
class CcAa {}
15+
// ^^
16+
17+
class Container {
18+
// This type links to Aa interface, not AaBb class.
19+
a: AaBb;
20+
21+
// This type links to Aa interface, not BbAaCc class.
22+
b: BbAaCc;
23+
24+
// This type links to Aa interface, not CcAa class.
25+
c: CcAa;
26+
}

test/src/cli/cli-extends.spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ describe('CLI simple generation - extends app', () => {
4343
const file = read(distFolder + '/directives/DoNothingDirective.html');
4444
expect(file).to.contain('Extends');
4545
expect(file).to.contain(
46-
'code><a href="../directives/ADirective.html" target="_self" >ADirective'
46+
'<a href="../directives/ADirective.html" target="_self" >ADirective'
4747
);
4848
});
4949

test/src/cli/cli-generation-big-app.spec.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -672,7 +672,7 @@ describe('CLI simple generation - big app', () => {
672672

673673
it('should support component metadata entryComponents', () => {
674674
expect(aboutComponentFile).to.contain(
675-
'<code><a href="../classes/Todo.html" target="_self" >TodoComponent</a></code>'
675+
'<code><a href="../components/TodoComponent.html" target="_self" >TodoComponent</a></code>'
676676
);
677677
});
678678

@@ -914,4 +914,9 @@ describe('CLI simple generation - big app', () => {
914914
const file = read(distFolder + '/modules/HeaderModule.html');
915915
expect(file).to.contain('href="../components/HeaderComponent.html">HeaderComponent');
916916
});
917+
918+
it('should support class name includes an interface name', () => {
919+
const file = read(distFolder + '/classes/Container.html');
920+
expect(file).to.contain('href="../classes/AaBb.html" target="_self" >AaBb');
921+
});
917922
});

0 commit comments

Comments
 (0)