Skip to content

Commit

Permalink
fix(app): Invalid links to a class when the class name includes an in…
Browse files Browse the repository at this point in the history
…terface name

fix #1239
  • Loading branch information
vogloblinsky committed Mar 22, 2022
1 parent a5bee44 commit 047cedb
Show file tree
Hide file tree
Showing 6 changed files with 74 additions and 25 deletions.
56 changes: 37 additions & 19 deletions src/app/engines/dependencies.engine.ts
Expand Up @@ -63,8 +63,8 @@ export class DependenciesEngine {
}

private updateModulesDeclarationsExportsTypes() {
let mergeTypes = entry => {
let directive = this.findInCompodocDependencies(
const mergeTypes = entry => {
const directive = this.findInCompodocDependencies(
entry.name,
this.directives,
entry.file
Expand All @@ -74,7 +74,7 @@ export class DependenciesEngine {
entry.id = directive.data.id;
}

let component = this.findInCompodocDependencies(
const component = this.findInCompodocDependencies(
entry.name,
this.components,
entry.file
Expand All @@ -84,7 +84,7 @@ export class DependenciesEngine {
entry.id = component.data.id;
}

let pipe = this.findInCompodocDependencies(entry.name, this.pipes, entry.file);
const pipe = this.findInCompodocDependencies(entry.name, this.pipes, entry.file);
if (typeof pipe.data !== 'undefined') {
entry.type = 'pipe';
entry.id = pipe.data.id;
Expand Down Expand Up @@ -147,24 +147,38 @@ export class DependenciesEngine {
private findInCompodocDependencies(name, data, file?): IApiSourceResult<any> {
let _result = {
source: 'internal',
data: undefined
data: undefined,
score: 0
};
let nameFoundCounter = 0;
if (data && data.length > 0) {
for (let i = 0; i < data.length; i++) {
if (typeof name !== 'undefined') {
if (typeof file !== 'undefined') {
if (
name === data[i].name &&
file.replace(/\\/g, '/').indexOf(data[i].file) !== -1
) {
nameFoundCounter += 1;
_result.data = data[i];
_result.score = 2;
} else if (
name.indexOf(data[i].name) !== -1 &&
file.replace(/\\/g, '/').indexOf(data[i].file) !== -1
) {
nameFoundCounter += 1;
_result.data = data[i];
_result.score = 1;
}
} else {
if (name.indexOf(data[i].name) !== -1) {
if (name === data[i].name) {
nameFoundCounter += 1;
_result.data = data[i];
_result.score = 2;
} else if (name.indexOf(data[i].name) !== -1) {
nameFoundCounter += 1;
_result.data = data[i];
_result.score = 1;
}
}
}
Expand All @@ -176,25 +190,25 @@ export class DependenciesEngine {
for (let i = 0; i < data.length; i++) {
if (typeof name !== 'undefined') {
if (typeof file !== 'undefined') {
if (
name === data[i].name &&
file.replace(/\\/g, '/').indexOf(data[i].file) !== -1
) {
if (name === data[i].name) {
found = true;
_result.data = data[i];
_result.score = 2;
}
} else {
if (name === data[i].name) {
found = true;
_result.data = data[i];
_result.score = 2;
}
}
}
}
if (!found) {
_result = {
source: 'internal',
data: undefined
data: undefined,
score: 0
};
}
}
Expand All @@ -203,8 +217,8 @@ export class DependenciesEngine {
}

private manageDuplicatesName() {
let processDuplicates = (element, index, array) => {
let elementsWithSameName = _.filter(array, { name: element.name });
const processDuplicates = (element, index, array) => {
const elementsWithSameName = _.filter(array, { name: element.name });
if (elementsWithSameName.length > 1) {
// First element is the reference for duplicates
for (let i = 1; i < elementsWithSameName.length; i++) {
Expand Down Expand Up @@ -251,15 +265,19 @@ export class DependenciesEngine {
() => AngularApiUtil.findApi(name)
];

let bestScore = 0;
let bestResult = undefined;

for (let searchFunction of searchFunctions) {
let result = searchFunction();
const result = searchFunction();

if (result.data) {
return result;
if (result.data && result.score > bestScore) {
bestScore = result.score;
bestResult = result;
}
}

return undefined;
return bestResult;
}

public update(updatedData): void {
Expand Down Expand Up @@ -372,7 +390,7 @@ export class DependenciesEngine {
}

public findInCompodoc(name: string) {
let mergedData = _.concat(
const mergedData = _.concat(
[],
this.modules,
this.components,
Expand All @@ -390,7 +408,7 @@ export class DependenciesEngine {
this.miscellaneous.variables,
this.miscellaneous.functions
);
let result = _.find(mergedData, { name: name } as any);
const result = _.find(mergedData, { name: name } as any);
return result || false;
}

Expand Down
Expand Up @@ -14,8 +14,8 @@ export class FunctionSignatureHelper implements IHtmlEngineHelper {
return `${arg.name}${this.getOptionalString(arg)}: () => void`;
}

let argums = arg.function.map(argu => {
let _result = DependenciesEngine.find(argu.type);
const argums = arg.function.map(argu => {
const _result = DependenciesEngine.find(argu.type);
if (_result) {
if (_result.source === 'internal') {
let path = _result.data.type;
Expand All @@ -26,7 +26,7 @@ export class FunctionSignatureHelper implements IHtmlEngineHelper {
_result.data.name
}.html">${argu.type}</a>`;
} else {
let path = AngularVersionUtil.getApiLink(
const path = AngularVersionUtil.getApiLink(
_result.data,
Configuration.mainData.angularVersion
);
Expand All @@ -35,7 +35,7 @@ export class FunctionSignatureHelper implements IHtmlEngineHelper {
)}: <a href="${path}" target="_blank">${argu.type}</a>`;
}
} else if (BasicTypeUtil.isKnownType(argu.type)) {
let path = BasicTypeUtil.getTypeUrl(argu.type);
const path = BasicTypeUtil.getTypeUrl(argu.type);
return `${argu.name}${this.getOptionalString(
arg
)}: <a href="${path}" target="_blank">${argu.type}</a>`;
Expand Down
1 change: 1 addition & 0 deletions src/utils/api-source-result.interface.ts
@@ -1,4 +1,5 @@
export interface IApiSourceResult<T> {
source: string;
data: T | undefined;
score: number;
}
25 changes: 25 additions & 0 deletions test/fixtures/todomvc-ng2/src/app/index.ts
@@ -1 +1,26 @@
export * from './app.module';

interface Aa {}

// The prefix of class name matches the interface name
class AaBb {}
// ^^

// The intermediate of class name matches the interface name
class BbAaCc {}
// ^^

// The suffix of class name matches the interface name
class CcAa {}
// ^^

class Container {
// This type links to Aa interface, not AaBb class.
a: AaBb;

// This type links to Aa interface, not BbAaCc class.
b: BbAaCc;

// This type links to Aa interface, not CcAa class.
c: CcAa;
}
2 changes: 1 addition & 1 deletion test/src/cli/cli-extends.spec.ts
Expand Up @@ -43,7 +43,7 @@ describe('CLI simple generation - extends app', () => {
const file = read(distFolder + '/directives/DoNothingDirective.html');
expect(file).to.contain('Extends');
expect(file).to.contain(
'code><a href="../directives/ADirective.html" target="_self" >ADirective'
'<a href="../directives/ADirective.html" target="_self" >ADirective'
);
});

Expand Down
7 changes: 6 additions & 1 deletion test/src/cli/cli-generation-big-app.spec.ts
Expand Up @@ -672,7 +672,7 @@ describe('CLI simple generation - big app', () => {

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

Expand Down Expand Up @@ -914,4 +914,9 @@ describe('CLI simple generation - big app', () => {
const file = read(distFolder + '/modules/HeaderModule.html');
expect(file).to.contain('href="../components/HeaderComponent.html">HeaderComponent');
});

it('should support class name includes an interface name', () => {
const file = read(distFolder + '/classes/Container.html');
expect(file).to.contain('href="../classes/AaBb.html" target="_self" >AaBb');
});
});

0 comments on commit 047cedb

Please sign in to comment.