Skip to content

Commit 5d6eef2

Browse files
Kamil SobolAmplifiyer
andauthored
Handle aliased symbols in namespaces in api check (#1926)
* 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]>
1 parent d4c10fe commit 5d6eef2

File tree

8 files changed

+44
-9
lines changed

8 files changed

+44
-9
lines changed

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

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,7 @@ export class ApiUsageGenerator {
9999
namespaceBySymbol: new Map<string, string>(),
100100
namespaceNames: new Set<string>(),
101101
topLevelNamespaces: new Set<string>(),
102+
aliasedSymbols: new Map<string, string>(),
102103
};
103104
for (const statement of this.apiReportAST.statements) {
104105
if (statement.kind === ts.SyntaxKind.ModuleDeclaration) {
@@ -118,9 +119,22 @@ export class ApiUsageGenerator {
118119
const namedExports =
119120
exportDeclaration.exportClause as ts.NamedExports;
120121
for (const namedExport of namedExports.elements) {
121-
const symbolName = namedExport.name.getText();
122+
let symbolNameInApiView: string;
123+
if (namedExport.propertyName) {
124+
// If property name is present this means that
125+
// API Extractor aliased type definition to avoid duplicate
126+
// and exported from namespace as 'SomeType_2 as SomeType'
127+
symbolNameInApiView = namedExport.propertyName.getText();
128+
const exportedSymbolName = namedExport.name.getText();
129+
namespaceDefinitions.aliasedSymbols.set(
130+
symbolNameInApiView,
131+
exportedSymbolName
132+
);
133+
} else {
134+
symbolNameInApiView = namedExport.name.getText();
135+
}
122136
namespaceDefinitions.namespaceBySymbol.set(
123-
symbolName,
137+
symbolNameInApiView,
124138
namespaceName
125139
);
126140
}

scripts/components/api-changes-validator/test-resources/test-projects/without-breaks/project-with-namespace/API.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,10 @@ export type SomeTypeUnderNamespace = {
44
someProperty: string;
55
}
66

7+
export type SomeTypeUnderNamespace_2 = {
8+
someProperty: string;
9+
}
10+
711
type SomeTypeUnderSubNamespace = {
812
someOtherProperty: string;
913
}
@@ -26,6 +30,12 @@ declare namespace someNamespace {
2630
}
2731
}
2832

33+
declare namespace someNamespaceWithSameType {
34+
export {
35+
SomeTypeUnderNamespace_2 as SomeTypeUnderNamespace,
36+
}
37+
}
38+
2939
type SomeTypeUnderOtherEntryPoint = {
3040
somePropertyUnderOtherEntryPoint: string;
3141
}
Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
import * as someNamespace from './some_namespace.js';
2+
import * as someNamespaceWithSameType from './some_namespace_with_same_type.js';
23

3-
export { someNamespace };
4+
export { someNamespace, someNamespaceWithSameType };

scripts/components/api-changes-validator/test-resources/test-projects/without-breaks/project-with-namespace/src/some_namespace.ts

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,7 @@
11
import * as someSubNamespace from './some_sub_namespace.js';
2+
import { SomeTypeUnderNamespace } from './types.js';
23

3-
export type SomeTypeUnderNamespace = {
4-
someProperty: string;
5-
};
6-
7-
export { someSubNamespace };
4+
export { someSubNamespace, SomeTypeUnderNamespace };
85

96
export const functionUsingTypes1 = (
107
props: SomeTypeUnderNamespace
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
import { SomeTypeUnderNamespace } from './types.js';
2+
3+
export { SomeTypeUnderNamespace };
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
export type SomeTypeUnderNamespace = {
2+
someProperty: string;
3+
};

scripts/components/api-changes-validator/types.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,4 +25,5 @@ export type NamespaceDefinitions = {
2525
topLevelNamespaces: Set<string>;
2626
namespaceNames: Set<string>;
2727
namespaceBySymbol: Map<string, string>;
28+
aliasedSymbols: Map<string, string>;
2829
};

scripts/components/api-changes-validator/usage_statemets_renderer.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,9 +57,15 @@ export class UsageStatementsRenderer {
5757
namespaceHierarchy.unshift(currentSymbolName);
5858
}
5959
} while (currentSymbolName);
60+
const symbolAlias =
61+
this.namespaceDefinitions.aliasedSymbols.get(symbolName);
62+
let actualSymbolName = symbolName;
63+
if (symbolAlias) {
64+
actualSymbolName = symbolAlias;
65+
}
6066
const symbolWithNamespace = `${namespaceHierarchy.join(
6167
'.'
62-
)}.${symbolName}`;
68+
)}.${actualSymbolName}`;
6369

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

0 commit comments

Comments
 (0)