diff --git a/packages/dev/config/eslint.js b/packages/dev/config/eslint.js index ac36e78..88a3ec6 100644 --- a/packages/dev/config/eslint.js +++ b/packages/dev/config/eslint.js @@ -25,6 +25,7 @@ import globals from 'globals'; import { overrideAll, overrideJs, overrideJsx, overrideSpec } from './eslint.rules.js'; import { standardRules } from './eslint.standard.js'; import { withoutContextualAssertions } from './eslint.typeAssertion.js'; +import { withoutExportAllFix } from './eslint.typeExports.js'; /** * eslint-plugin-header declares no options schema, and eslint 9+ rejects @@ -94,8 +95,8 @@ export default [ }, plugins: { '@stylistic': stylisticPlugin, - // see eslint.typeAssertion.js - '@typescript-eslint': withoutContextualAssertions(tsPlugin), + // see eslint.typeAssertion.js and eslint.typeExports.js + '@typescript-eslint': withoutExportAllFix(withoutContextualAssertions(tsPlugin)), // eslint-plugin-header reports at the first token after the leading // comments, so a generated file's /* eslint-disable */ covers it; the // maintained fork reports at line 1 instead. The compat layer supplies the diff --git a/packages/dev/config/eslint.typeExports.js b/packages/dev/config/eslint.typeExports.js new file mode 100644 index 0000000..1b3a6c0 --- /dev/null +++ b/packages/dev/config/eslint.typeExports.js @@ -0,0 +1,63 @@ +// Copyright 2017-2026 @pezkuwi/dev authors & contributors +// SPDX-License-Identifier: Apache-2.0 + +const RULE = 'consistent-type-exports'; + +/** + * consistent-type-exports reports an `export * from './x'` whose module only + * exports types, and its autofix turns it into `export type * from './x'`. + * That changes what runs: `export *` loads the module, `export type *` is + * erased, so a module that only exports types but registers something when it + * loads (a Handlebars partial, an interface augmentation) silently stops being + * loaded. Measured on the consumers: the api typegen lost its `docs` partial + * this way and the build failed only at template time. + * + * Named re-exports do not have this problem: under verbatimModuleSyntax a + * type re-exported without `type` is already a compile error (TS1205). + * + * This keeps the report as it is and moves the `export *` fix into a + * suggestion, so --fix no longer applies it and an editor still offers it. + * Whoever accepts it decides whether the module needs a side-effect import. + * + * @template {object} P + * @param {P} plugin the @typescript-eslint plugin + * @returns {P} + */ +export function withoutExportAllFix (plugin) { + const rules = /** @type {Record} */ (/** @type {{ rules: unknown }} */ (plugin).rules); + const rule = rules[RULE]; + + return /** @type {P} */ ({ + ...plugin, + rules: { + ...rules, + [RULE]: { + ...rule, + /** @param {import('eslint').Rule.RuleContext} context */ + create (context) { + const filtered = Object.create(context, { + report: { + value: (/** @type {import('eslint').Rule.ReportDescriptor} */ descriptor) => { + if ('node' in descriptor && descriptor.node.type === 'ExportAllDeclaration' && descriptor.fix) { + const { fix, ...rest } = descriptor; + + context.report({ + ...rest, + suggest: [{ desc: 'Use `export type *` (the module is no longer loaded: add a side-effect import if it registers anything)', fix }] + }); + + return; + } + + context.report(descriptor); + } + } + }); + + return rule.create(filtered); + }, + meta: { ...rule.meta, hasSuggestions: true } + } + } + }); +} diff --git a/packages/dev/src/typeExports.spec.ts b/packages/dev/src/typeExports.spec.ts new file mode 100644 index 0000000..1bf9f4a --- /dev/null +++ b/packages/dev/src/typeExports.spec.ts @@ -0,0 +1,80 @@ +// Copyright 2017-2026 @pezkuwi/dev authors & contributors +// SPDX-License-Identifier: Apache-2.0 + +/// + +import type { ESLint as ESLintType } from 'eslint'; + +import tsPlugin from '@typescript-eslint/eslint-plugin'; +import tsParser from '@typescript-eslint/parser'; +import { ESLint } from 'eslint'; +import fs from 'node:fs'; +import os from 'node:os'; +import path from 'node:path'; + +// Loaded by path: the config directory is plain JavaScript outside this +// package's TypeScript root. +const WRAPPER = path.join(process.cwd(), 'packages/dev/config/eslint.typeExports.js'); + +// only types, but loading it has an effect the importer relies on +const TYPES = `export interface Partial { name: string } + +(globalThis as Record)['registered'] = true; +`; + +const SOURCE = `export * from './types.js'; +`; + +interface Result { fixed: string; messages: { fix: boolean; line: number; messageId: string; suggestions: number }[] } + +async function lint (plugin: ESLintType.Plugin): Promise { + const dir = fs.mkdtempSync(path.join(os.tmpdir(), 'type-exports-')); + + try { + fs.writeFileSync(path.join(dir, 'tsconfig.json'), JSON.stringify({ compilerOptions: { module: 'esnext', moduleResolution: 'bundler', strict: true, target: 'es2022' }, include: ['*.ts'] })); + fs.writeFileSync(path.join(dir, 'types.ts'), TYPES); + fs.writeFileSync(path.join(dir, 'index.ts'), SOURCE); + + const options = (fix: boolean): ESLintType.Options => ({ + cwd: dir, + fix, + overrideConfig: [{ + files: ['**/*.ts'], + languageOptions: { + parser: tsParser, + parserOptions: { project: './tsconfig.json', tsconfigRootDir: dir } + }, + plugins: { '@typescript-eslint': plugin }, + rules: { '@typescript-eslint/consistent-type-exports': 'error' } + }], + overrideConfigFile: true + }); + const [reported] = await new ESLint(options(false)).lintFiles(['index.ts']); + const [fixed] = await new ESLint(options(true)).lintFiles(['index.ts']); + + return { + fixed: fixed.output ?? SOURCE, + messages: reported.messages.map(({ fix, line, messageId, suggestions }) => ({ fix: !!fix, line, messageId: messageId ?? '', suggestions: suggestions?.length ?? 0 })) + }; + } finally { + fs.rmSync(dir, { force: true, recursive: true }); + } +} + +describe('withoutExportAllFix', (): void => { + it('keeps the report on a types-only export *, offers the change only as a suggestion', async (): Promise => { + const { withoutExportAllFix } = await import(WRAPPER) as { withoutExportAllFix: (p: ESLintType.Plugin) => ESLintType.Plugin }; + + expect(await lint(withoutExportAllFix(tsPlugin as unknown as ESLintType.Plugin))).toEqual({ + fixed: SOURCE, + messages: [{ fix: false, line: 1, messageId: 'typeOverValue', suggestions: 1 }] + }); + }); + + it('is needed: the plugin as published rewrites it to export type *, which no longer loads the module', async (): Promise => { + expect(await lint(tsPlugin as unknown as ESLintType.Plugin)).toEqual({ + fixed: "export type * from './types.js';\n", + messages: [{ fix: true, line: 1, messageId: 'typeOverValue', suggestions: 0 }] + }); + }); +});