Skip to content

Commit

Permalink
Handle aliased symbols in namespaces in api check (#1926)
Browse files Browse the repository at this point in the history
* handle aliased symbols in namespaces

* handle aliased symbols in namespaces

* Update scripts/components/api-changes-validator/api_usage_generator.ts

Co-authored-by: Amplifiyer <[email protected]>

---------

Co-authored-by: Amplifiyer <[email protected]>
  • Loading branch information
sobolk and Amplifiyer authored Aug 28, 2024
1 parent d4c10fe commit 5d6eef2
Show file tree
Hide file tree
Showing 8 changed files with 44 additions and 9 deletions.
18 changes: 16 additions & 2 deletions scripts/components/api-changes-validator/api_usage_generator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ export class ApiUsageGenerator {
namespaceBySymbol: new Map<string, string>(),
namespaceNames: new Set<string>(),
topLevelNamespaces: new Set<string>(),
aliasedSymbols: new Map<string, string>(),
};
for (const statement of this.apiReportAST.statements) {
if (statement.kind === ts.SyntaxKind.ModuleDeclaration) {
Expand All @@ -118,9 +119,22 @@ export class ApiUsageGenerator {
const namedExports =
exportDeclaration.exportClause as ts.NamedExports;
for (const namedExport of namedExports.elements) {
const symbolName = namedExport.name.getText();
let symbolNameInApiView: string;
if (namedExport.propertyName) {
// If property name is present this means that
// API Extractor aliased type definition to avoid duplicate
// and exported from namespace as 'SomeType_2 as SomeType'
symbolNameInApiView = namedExport.propertyName.getText();
const exportedSymbolName = namedExport.name.getText();
namespaceDefinitions.aliasedSymbols.set(
symbolNameInApiView,
exportedSymbolName
);
} else {
symbolNameInApiView = namedExport.name.getText();
}
namespaceDefinitions.namespaceBySymbol.set(
symbolName,
symbolNameInApiView,
namespaceName
);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ export type SomeTypeUnderNamespace = {
someProperty: string;
}

export type SomeTypeUnderNamespace_2 = {
someProperty: string;
}

type SomeTypeUnderSubNamespace = {
someOtherProperty: string;
}
Expand All @@ -26,6 +30,12 @@ declare namespace someNamespace {
}
}

declare namespace someNamespaceWithSameType {
export {
SomeTypeUnderNamespace_2 as SomeTypeUnderNamespace,
}
}

type SomeTypeUnderOtherEntryPoint = {
somePropertyUnderOtherEntryPoint: string;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import * as someNamespace from './some_namespace.js';
import * as someNamespaceWithSameType from './some_namespace_with_same_type.js';

export { someNamespace };
export { someNamespace, someNamespaceWithSameType };
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
import * as someSubNamespace from './some_sub_namespace.js';
import { SomeTypeUnderNamespace } from './types.js';

export type SomeTypeUnderNamespace = {
someProperty: string;
};

export { someSubNamespace };
export { someSubNamespace, SomeTypeUnderNamespace };

export const functionUsingTypes1 = (
props: SomeTypeUnderNamespace
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import { SomeTypeUnderNamespace } from './types.js';

export { SomeTypeUnderNamespace };
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export type SomeTypeUnderNamespace = {
someProperty: string;
};
1 change: 1 addition & 0 deletions scripts/components/api-changes-validator/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,5 @@ export type NamespaceDefinitions = {
topLevelNamespaces: Set<string>;
namespaceNames: Set<string>;
namespaceBySymbol: Map<string, string>;
aliasedSymbols: Map<string, string>;
};
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,15 @@ export class UsageStatementsRenderer {
namespaceHierarchy.unshift(currentSymbolName);
}
} while (currentSymbolName);
const symbolAlias =
this.namespaceDefinitions.aliasedSymbols.get(symbolName);
let actualSymbolName = symbolName;
if (symbolAlias) {
actualSymbolName = symbolAlias;
}
const symbolWithNamespace = `${namespaceHierarchy.join(
'.'
)}.${symbolName}`;
)}.${actualSymbolName}`;

// characters that can be found before or after symbol
// this is to prevent partial matches in case one symbol's characters are subset of longer one
Expand Down

0 comments on commit 5d6eef2

Please sign in to comment.