Skip to content

Commit c6ba175

Browse files
author
Kamil Sobol
authored
Exclude FromJSONSchema from api checks due to its complexity (#2079)
1 parent 0a5e51c commit c6ba175

File tree

8 files changed

+49
-6
lines changed

8 files changed

+49
-6
lines changed

scripts/check_api_changes.ts

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,18 @@ console.log(
4747

4848
const packagePaths = await glob(`${latestRepositoryPath}/packages/*`);
4949

50+
const excludedTypesByPackageName: Record<string, Array<string>> = {
51+
'ai-constructs': [
52+
// FromJSONSchema is complex enough to trigger
53+
// index.ts(113,9): error TS2589: Type instantiation is excessively deep and possibly infinite.
54+
// index.ts(113,87): error TS2589: Type instantiation is excessively deep and possibly infinite.
55+
// index.ts(113,87): error TS2590: Expression produces a union type that is too complex to represent.
56+
// See https://github.com/ThomasAribart/json-schema-to-ts/blob/main/documentation/FAQs/i-get-a-type-instantiation-is-excessively-deep-and-potentially-infinite-error-what-should-i-do.md.
57+
// Therefore, excluding this type from checks.
58+
'FromJSONSchema',
59+
],
60+
};
61+
5062
const validationResults = await Promise.allSettled(
5163
packagePaths.map(async (packagePath) => {
5264
const packageName = path.basename(packagePath);
@@ -70,7 +82,8 @@ const validationResults = await Promise.allSettled(
7082
await new ApiChangesValidator(
7183
packagePath,
7284
baselinePackageApiReportPath,
73-
workingDirectory
85+
workingDirectory,
86+
excludedTypesByPackageName[packageName]
7487
).validate();
7588
console.log(`Validation of ${packageName} completed successfully`);
7689
})

scripts/components/api-changes-validator/api_changes_validator.test.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ void describe('Api changes validator', { concurrency: true }, () => {
6161
latestPackagePath,
6262
baselinePackageApiReportPath,
6363
workingDirectory,
64+
[],
6465
'npmLocalLink'
6566
);
6667

@@ -92,6 +93,7 @@ void describe('Api changes validator', { concurrency: true }, () => {
9293
latestPackagePath,
9394
baselinePackageApiReportPath,
9495
workingDirectory,
96+
['SampleIgnoredType'],
9597
'npmLocalLink'
9698
);
9799

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ export class ApiChangesValidator {
3030
private readonly latestPackagePath: string,
3131
private readonly baselinePackageApiReportPath: string,
3232
private readonly workingDirectory: string,
33+
private readonly excludedTypes: Array<string> = [],
3334
private readonly latestPackageDependencyDeclarationStrategy:
3435
| 'npmRegistry'
3536
| 'npmLocalLink' = 'npmRegistry'
@@ -103,7 +104,8 @@ export class ApiChangesValidator {
103104
const apiReportAST = ApiReportParser.parse(apiReportContent);
104105
const usage = new ApiUsageGenerator(
105106
latestPackageJson.name,
106-
apiReportAST
107+
apiReportAST,
108+
this.excludedTypes
107109
).generate();
108110
await fsp.writeFile(path.join(this.testProjectPath, 'index.ts'), usage);
109111
await execa('npm', ['install'], { cwd: this.testProjectPath });

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

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -337,6 +337,15 @@ const someTypeUnderSubNamespaceUsageFunction = (someTypeUnderSubNamespaceFunctio
337337
}
338338
`,
339339
},
340+
{
341+
description: 'Skips ignored type',
342+
apiReportCode: `
343+
export type SampleIgnoredType = {
344+
someProperty: string;
345+
}
346+
`,
347+
expectedApiUsage: '',
348+
},
340349
];
341350

342351
const nestInMarkdownCodeBlock = (apiReportCode: string) => {
@@ -351,7 +360,8 @@ void describe('Api usage generator', () => {
351360
);
352361
const apiUsage = new ApiUsageGenerator(
353362
'samplePackageName',
354-
apiReportAST
363+
apiReportAST,
364+
['SampleIgnoredType']
355365
).generate();
356366
assert.strictEqual(
357367
// .replace() removes EOL differences between Windows and other OS so output matches for all

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

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,8 @@ export class ApiUsageGenerator {
2525
*/
2626
constructor(
2727
private readonly packageName: string,
28-
private readonly apiReportAST: ts.SourceFile
28+
private readonly apiReportAST: ts.SourceFile,
29+
private readonly excludedTypes: Array<string>
2930
) {
3031
this.namespaceDefinitions = this.getNamespaceDefinitions();
3132
}
@@ -65,7 +66,8 @@ export class ApiUsageGenerator {
6566
case ts.SyntaxKind.TypeAliasDeclaration:
6667
return new TypeUsageStatementsGenerator(
6768
node as ts.TypeAliasDeclaration,
68-
this.packageName
69+
this.packageName,
70+
this.excludedTypes
6971
).generate();
7072
case ts.SyntaxKind.EnumDeclaration:
7173
return new EnumUsageStatementsGenerator(

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

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,10 +122,14 @@ export class TypeUsageStatementsGenerator implements UsageStatementsGenerator {
122122
*/
123123
constructor(
124124
private readonly typeAliasDeclaration: ts.TypeAliasDeclaration,
125-
private readonly packageName: string
125+
private readonly packageName: string,
126+
private readonly excludedTypes: Array<string>
126127
) {}
127128
generate = (): UsageStatementsGeneratorOutput => {
128129
const typeName = this.typeAliasDeclaration.name.getText();
130+
if (this.excludedTypes.includes(typeName)) {
131+
return {};
132+
}
129133
const constName = toLowerCamelCase(typeName);
130134
const genericTypeParametersDeclaration =
131135
new GenericTypeParameterDeclarationUsageStatementsGenerator(

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,4 +79,9 @@ export type SampleTypeUsingClass = {
7979
export type SampleTypeThatReferencesFunction<T extends typeof someFunction1> = {
8080
sampleProperty: T;
8181
};
82+
83+
// This type is intentionally different from what's in sources
84+
export type SampleIgnoredType = {
85+
someProperty: string;
86+
};
8287
```

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,3 +110,8 @@ export type SampleTypeUsingClass = {
110110
export type SampleTypeThatReferencesFunction<T extends typeof someFunction1> = {
111111
sampleProperty: T;
112112
};
113+
114+
// This type is intentionally different from what's in api report
115+
export type SampleIgnoredType = {
116+
someProperty: number;
117+
};

0 commit comments

Comments
 (0)