diff --git a/README.md b/README.md index 3f76a05..772f057 100644 --- a/README.md +++ b/README.md @@ -50,6 +50,16 @@ await fixBadDeclarationOutput( ); ``` +For removing `/// " export const two = 2;`); }); + + test('can remove everything', () => { + let code = stripIndent` + /// + /// + /// + export const two = 2;`; + + let result = fixReferences(code, { types: 'all' }); + + expect(result).toBe(`export const two = 2;`); + }); }); diff --git a/src/index.js b/src/index.js index 65eae3b..f34763e 100644 --- a/src/index.js +++ b/src/index.js @@ -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 - * @template {Issue} Key + * @typedef {import('./types.js').Issue} Issue + * @typedef {import('./types.js').FixerPair} FixerPair + * @typedef {import('./types.js').IssueFunction} IssueFunction */ /** @@ -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, @@ -40,7 +36,7 @@ export async function fixBadDeclarationOutput( `List of fixes missing for 'fixBadDeclarationOutput'. Please specify the 'fixes' for the second arg.` ); - /** @type {Array} */ + /** @type {IssueFunction[]} */ let fixesToApply = []; /** @type {string[]} */ let names = []; @@ -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}.`); diff --git a/src/index.test.ts b/src/index.test.ts new file mode 100644 index 0000000..334f21f --- /dev/null +++ b/src/index.test.ts @@ -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` + /// + /// + /// + 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;`); + }); +}); diff --git a/src/types.ts b/src/types.ts new file mode 100644 index 0000000..24158ac --- /dev/null +++ b/src/types.ts @@ -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, IssuesMap[Key]]; + +// eslint-disable-next-line @typescript-eslint/no-explicit-any +export type Fixes = (Issue | FixerPair)[];