Skip to content

Commit

Permalink
Merge pull request #10 from NullVoxPopuli/be-more-robust
Browse files Browse the repository at this point in the history
Add more tests
  • Loading branch information
NullVoxPopuli committed Feb 9, 2024
2 parents 56c1b08 + 5468ab9 commit fbb8bfd
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 5 deletions.
7 changes: 3 additions & 4 deletions src/fixes/typescript.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,13 @@ export function fixReferences(contents, options = {}) {
const removeAll = !options.types || options.types === 'all';
const find = removeAll ? `/ <reference types=` : `/ <reference types="${options.types}`;

const fixed = root
root
// @ts-expect-error
.find(j.Comment)
// @ts-expect-error
.filter((path) => path.value.value.startsWith(find))
// @ts-expect-error
.forEach((path) => j(path).remove())
.toSource();
.forEach((path) => j(path).remove());

return fixed;
return root.toSource();
}
62 changes: 62 additions & 0 deletions src/fixes/typescript.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,4 +76,66 @@ describe('fixReferences', () => {

expect(result).toBe(`export const two = 2;`);
});

describe('https://github.com/machty/ember-concurrency/issues/564', () => {
test('declarations/helpers/cancel-all.d.ts', () => {
let code = stripIndent`
/// <reference types="ember-source/types/preview/@ember/component/-private/signature-utils" />
/// <reference types="ember-source/types/preview/@ember/component/helper" />
import type { Task } from '../index';
type CancelAllParams = [task: Task<any, any[]>];
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<any, any[]>];
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 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';
`;

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';"
`);
});
});
});
9 changes: 8 additions & 1 deletion src/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,10 @@ describe('fixBadDeclarationOutput', () => {
/// <reference types="@glint/whatever/module">
/// <reference types="node_modules/@glint/whatever2/module">
/// <reference types="xyz">
export declare const two: number;
export declare const three: string;
export declare const four: 'literal';
`
);

Expand All @@ -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';"
`);
});
});

0 comments on commit fbb8bfd

Please sign in to comment.