Skip to content

Commit

Permalink
Merge pull request #4 from NullVoxPopuli/add-option-to-remove-all-ref…
Browse files Browse the repository at this point in the history
…erences

Add option to remove all references
  • Loading branch information
NullVoxPopuli authored Feb 1, 2024
2 parents e6ef402 + b26b3b5 commit 6c42c4a
Show file tree
Hide file tree
Showing 6 changed files with 97 additions and 13 deletions.
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,16 @@ await fixBadDeclarationOutput(
);
```

For removing `/// <reference` entirely, you can configure:

```js
import { fixBadDeclarationOutput } from "fix-bad-declaration-output";

await fixBadDeclarationOutput("./declarations/**/*.d.ts", [
["TypeScript#56571", { types: "all " }],
]);
```

#### Removes lines starting with `/// <reference types="ember`

Starting with TS 5.3.x, iirc, ember-source's strategy for shipping public types causes `/// <reference` declarations to be added in libraries.
Expand Down
7 changes: 5 additions & 2 deletions src/fixes/typescript.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,14 @@ const defaultFind = 'ember';

/**
* @param {string} contents
* @param {{ types?: string }} [ options ]
* @param {{ types?: string | 'all' }} [ options ]
*/
export function fixReferences(contents, options = {}) {
const root = j(contents);
const find = `/ <reference types="${options.types || defaultFind}`;
const find =
options.types === 'all'
? `/ <reference types=`
: `/ <reference types="${options.types || defaultFind}`;

const fixed = root
// @ts-expect-error
Expand Down
12 changes: 12 additions & 0 deletions src/fixes/typescript.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,4 +72,16 @@ describe('fixReferences', () => {
/// <reference types="node_modules/@glint/whatever2/module">"
export const two = 2;`);
});

test('can remove everything', () => {
let code = stripIndent`
/// <reference types="@glint/whatever/module">
/// <reference types="node_modules/@glint/whatever2/module">
/// <reference types="xyz">
export const two = 2;`;

let result = fixReferences(code, { types: 'all' });

expect(result).toBe(`export const two = 2;`);
});
});
17 changes: 6 additions & 11 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,9 @@ import { issues } from './fixes/index.js';
import { fixFile, getFiles } from './utils.js';

/**
* @typedef {typeof issues} IssuesMap
* @typedef {keyof IssuesMap} Issue
*/

/**
* @typedef {[Key, IssuesMap[Key]]} FixerPair<Key>
* @template {Issue} Key
* @typedef {import('./types.js').Issue} Issue
* @typedef {import('./types.js').FixerPair<any>} FixerPair
* @typedef {import('./types.js').IssueFunction} IssueFunction
*/

/**
Expand All @@ -24,7 +20,7 @@ const DEFAULT_OPTIONS = {

/**
* @param {string} glob
* @param {Issue[]} fixes
* @param {import('./types.js').Fixes} fixes
*/
export async function fixBadDeclarationOutput(
glob = DEFAULT_GLOB,
Expand All @@ -40,7 +36,7 @@ export async function fixBadDeclarationOutput(
`List of fixes missing for 'fixBadDeclarationOutput'. Please specify the 'fixes' for the second arg.`
);

/** @type {Array<IssuesMap[Issue]>} */
/** @type {IssueFunction[]} */
let fixesToApply = [];
/** @type {string[]} */
let names = [];
Expand All @@ -52,8 +48,7 @@ export async function fixBadDeclarationOutput(
let name = requested[0];
let fixOptions = requested[1];

/** @type {IssuesMap[Issue]} */

/** @type {IssueFunction} */
let fixer = issues[name];

assert(fixer, `Could not find fixer with name ${name}.`);
Expand Down
54 changes: 54 additions & 0 deletions src/index.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
import crypto from 'node:crypto';
import fs from 'node:fs/promises';
import os from 'node:os';
import path from 'node:path';

import { stripIndent } from 'common-tags';
import { describe, expect as hardAssert, test } from 'vitest';

import { fixBadDeclarationOutput } from './index.js';

const expect = hardAssert.soft;

async function mkdirp() {
const tmpDir = os.tmpdir();
const tmpPath = path.join(tmpDir, 'fix-bad-declaration-output');
const suffix = crypto.randomBytes(16).toString('hex').slice(0, 16);
const fullPath = path.join(tmpPath, `test-${suffix}`);

await fs.mkdir(fullPath, { recursive: true });

return fullPath;
}

async function read(filePath: string) {
let buffer = await fs.readFile(filePath);

return buffer.toString();
}

describe('fixBadDeclarationOutput', () => {
test('it works', async () => {
let tmp = await mkdirp();

let a = path.join(tmp, 'a.d.ts');

await fs.writeFile(
a,
stripIndent`
/// <reference types="@glint/whatever/module">
/// <reference types="node_modules/@glint/whatever2/module">
/// <reference types="xyz">
export declare const two: number;
`
);

await fixBadDeclarationOutput(`${tmp}/**/*.d.ts`, [['TypeScript#56571', { types: 'all' }]], {
log: true,
});

let aContents = await read(a);

expect(aContents).toBe(`export declare const two: number;`);
});
});
10 changes: 10 additions & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import type { issues } from './fixes/index.js';

export type IssuesMap = typeof issues;
export type Issue = keyof IssuesMap;
export type IssueFunction = IssuesMap[Issue];

export type FixerPair<Key extends Issue> = [Key, IssuesMap[Key]];

// eslint-disable-next-line @typescript-eslint/no-explicit-any
export type Fixes = (Issue | FixerPair<any>)[];

0 comments on commit 6c42c4a

Please sign in to comment.