From 55a8b61430124ba002859fa1e800ba38b4d3380a Mon Sep 17 00:00:00 2001 From: NullVoxPopuli <199018+NullVoxPopuli@users.noreply.github.com> Date: Fri, 9 Feb 2024 00:47:23 -0500 Subject: [PATCH 1/3] Fix parsing issue with certain declarations for the https://github.com/microsoft/TypeScript/issues/56571 issue Normalize some checking, expand test cases --- src/fixes/typescript.js | 7 ++-- src/fixes/typescript.test.ts | 62 ++++++++++++++++++++++++++++++++++++ src/index.test.ts | 9 +++++- 3 files changed, 73 insertions(+), 5 deletions(-) diff --git a/src/fixes/typescript.js b/src/fixes/typescript.js index 5c2072d..a1022c8 100644 --- a/src/fixes/typescript.js +++ b/src/fixes/typescript.js @@ -11,14 +11,13 @@ export function fixReferences(contents, options = {}) { const removeAll = !options.types || options.types === 'all'; const find = removeAll ? `/ + /// + import type { Task } from '../index'; + type CancelAllParams = [task: Task]; + export declare function cancelHelper(args: CancelAllParams): (...innerArgs: any[]) => any; + declare const _default: import("@ember/component/helper").FunctionBasedHelper<{ + Args: { + Positional: CancelAllParams; + Named: import("@ember/component/helper").EmptyObject; + }; + Return: (...innerArgs: any[]) => any; + }>; + export default _default; + `; + + let result = fixReferences(code); + + expect(result).toMatchInlineSnapshot(` + "import type { Task } from '../index'; + type CancelAllParams = [task: Task]; + export declare function cancelHelper(args: CancelAllParams): (...innerArgs: any[]) => any; + declare const _default: import("@ember/component/helper").FunctionBasedHelper<{ + Args: { + Positional: CancelAllParams; + Named: import("@ember/component/helper").EmptyObject; + }; + Return: (...innerArgs: any[]) => any; + }>; + export default _default;" + `); + }); + }) + test('declarations/-private/ember-environment.d.ts', () => { + let code = stripIndent` + export class EmberEnvironment extends Environment { + assert(...args: any[]): void; + reportUncaughtRejection(error: any): void; + defer(): any; + globalDebuggingEnabled(): any; + } + export const EMBER_ENVIRONMENT: EmberEnvironment; + import { Environment } from './external/environment'; + `; + + let result = fixReferences(code); + + expect(result).toMatchInlineSnapshot(` + "export class EmberEnvironment extends Environment { + assert(...args: any[]): void; + reportUncaughtRejection(error: any): void; + defer(): any; + globalDebuggingEnabled(): any; + } + export const EMBER_ENVIRONMENT: EmberEnvironment; + import { Environment } from './external/environment';" + `); + }); }); diff --git a/src/index.test.ts b/src/index.test.ts index 334f21f..0906645 100644 --- a/src/index.test.ts +++ b/src/index.test.ts @@ -39,7 +39,10 @@ describe('fixBadDeclarationOutput', () => { /// /// /// + export declare const two: number; + export declare const three: string; + export declare const four: 'literal'; ` ); @@ -49,6 +52,10 @@ describe('fixBadDeclarationOutput', () => { let aContents = await read(a); - expect(aContents).toBe(`export declare const two: number;`); + expect(aContents).toMatchInlineSnapshot(` + "export declare const two: number; + export declare const three: string; + export declare const four: 'literal';" + `); }); }); From ec84367e344cba0855ef7af784cc9d26043daee0 Mon Sep 17 00:00:00 2001 From: NullVoxPopuli <199018+NullVoxPopuli@users.noreply.github.com> Date: Fri, 9 Feb 2024 01:06:57 -0500 Subject: [PATCH 2/3] Fix declare -- the types were wrong --- src/fixes/typescript.test.ts | 49 ++++++++++++++++++------------------ 1 file changed, 25 insertions(+), 24 deletions(-) diff --git a/src/fixes/typescript.test.ts b/src/fixes/typescript.test.ts index cd93b11..87599bf 100644 --- a/src/fixes/typescript.test.ts +++ b/src/fixes/typescript.test.ts @@ -112,30 +112,31 @@ describe('fixReferences', () => { export default _default;" `); }); - }) - test('declarations/-private/ember-environment.d.ts', () => { - let code = stripIndent` - export class EmberEnvironment extends Environment { - assert(...args: any[]): void; - reportUncaughtRejection(error: any): void; - defer(): any; - globalDebuggingEnabled(): any; - } - export const EMBER_ENVIRONMENT: EmberEnvironment; - import { Environment } from './external/environment'; - `; - let result = fixReferences(code); + test('declarations/-private/ember-environment.d.ts', () => { + let code = stripIndent` + export declare class EmberEnvironment extends Environment { + assert(...args: any[]): void; + reportUncaughtRejection(error: any): void; + defer(): any; + globalDebuggingEnabled(): any; + } + export declare const EMBER_ENVIRONMENT: EmberEnvironment; + import { Environment } from './external/environment'; + `; - expect(result).toMatchInlineSnapshot(` - "export class EmberEnvironment extends Environment { - assert(...args: any[]): void; - reportUncaughtRejection(error: any): void; - defer(): any; - globalDebuggingEnabled(): any; - } - export const EMBER_ENVIRONMENT: EmberEnvironment; - import { Environment } from './external/environment';" - `); - }); + let result = fixReferences(code); + + expect(result).toMatchInlineSnapshot(` + "export declare class EmberEnvironment extends Environment { + assert(...args: any[]): void; + reportUncaughtRejection(error: any): void; + defer(): any; + globalDebuggingEnabled(): any; + } + export declare const EMBER_ENVIRONMENT: EmberEnvironment; + import { Environment } from './external/environment';" + `); + }); + }) }); From 5468ab91e75a87cf78ef2bad84db843b4cc2627e Mon Sep 17 00:00:00 2001 From: NullVoxPopuli <199018+NullVoxPopuli@users.noreply.github.com> Date: Fri, 9 Feb 2024 01:08:12 -0500 Subject: [PATCH 3/3] Lint:fix --- src/fixes/typescript.test.ts | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/fixes/typescript.test.ts b/src/fixes/typescript.test.ts index 87599bf..da6fa14 100644 --- a/src/fixes/typescript.test.ts +++ b/src/fixes/typescript.test.ts @@ -78,7 +78,6 @@ describe('fixReferences', () => { }); describe('https://github.com/machty/ember-concurrency/issues/564', () => { - test('declarations/helpers/cancel-all.d.ts', () => { let code = stripIndent` /// @@ -138,5 +137,5 @@ describe('fixReferences', () => { import { Environment } from './external/environment';" `); }); - }) + }); });