Skip to content

Conversation

@sindresorhus
Copy link
Owner

No description provided.

@sindresorhus sindresorhus marked this pull request as draft September 26, 2025 08:08
@sindresorhus sindresorhus force-pushed the rule-tests branch 3 times, most recently from 3a23b91 to 0d06a6f Compare September 26, 2025 11:00
@sindresorhus sindresorhus marked this pull request as ready for review September 26, 2025 11:01
Copy link

@chatgpt-codex-connector chatgpt-codex-connector bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

@som-sm
Copy link
Collaborator

som-sm commented Oct 6, 2025

@sindresorhus Apologies for the delay on this, I'm a bit occupied with some household work. But, I'll get to this sometime this week or next.

@sindresorhus
Copy link
Owner Author

@som-sm Would be good to get this merged so it can also be used in your #1265 PR.

@som-sm
Copy link
Collaborator

som-sm commented Oct 14, 2025

@som-sm Would be good to get this merged so it can also be used in your #1265 PR.

Yeah, yeah, I had some thoughts around this. Give me a couple of days, and I'll give this a proper read and get back.

const {ruleTester, fixturePath} = createTypeAwareRuleTester({
'package.json': `${packageContent}\n`,
'index.d.ts': indexContent,
'source/exported-type.d.ts': exportedTypeContent,
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@sindresorhus QQ, out of curiosity, is the code on these on-disk files actually being used anywhere?


From my initial testing:

Create source/exported-type.d.ts file with some arbitrary content:

const {ruleTester, fixturePath} = createTypeAwareRuleTester({
	'package.json': `${packageContent}\n`,
	'index.d.ts': indexContent,
	'source/exported-type.d.ts': 'The code here isn\'t being used anywhere, because the virtual file will overwrite it.',
	// ...
});

The code that we specify in code is what is being used for linting:

const exportedTypeContent = 'export type ExportedType = {\n\tvalue: string;\n};\n';

ruleTester.run('require-exported-types', requireExportedTypesRule, {
	valid: [
		{
			code: exportedTypeContent,
			filename: fixturePath('source/exported-type.d.ts'),
		},
	],
	invalid: [],
});

Verified this using the following log in the lint rule:

console.log({
    sourceCode: context.sourceCode.text,
});

This log prints:

{ sourceCode: 'export type ExportedType = {\n\tvalue: string;\n};\n' }

Creating index.d.ts and package.json files on disk makes sense because the individual test cases don't create those files. But having to create files on disk for all test cases beforehand feels odd to me, but maybe that's a standard practice.


Does it make sense to set them to undefined instead?

const {ruleTester, fixturePath} = createTypeAwareRuleTester({
	'package.json': `${packageContent}\n`,
	'index.d.ts': indexContent,
	'source/exported-type.d.ts': undefined,
	'source/internal/internal-type.d.ts': undefined,
	'source/internal/nested/deep.d.ts': undefined,
	'source/private-type.d.ts': undefined,
	'source/missing-type.d.ts': undefined,
	'source/unexported-multiple.d.ts': undefined,
	'source/implementation.ts': undefined,
	'test/test.d.ts': undefined,
	'lib/test.d.ts': undefined,
});

And, in test-utils.js add an empty string fallback:

fs.writeFileSync(absolutePath, content ?? '');

Copy link
Collaborator

@som-sm som-sm Oct 16, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

IMO, it would have been better if we had to create only the files that are separate from the ones we're linting, i.e., index.d.ts and package.json.

But it seems like doing that brings in a lot of other complications. Here's all I had to do to get it working:

import {createTypeAwareRuleTester} from './test-utils.js';
import {requireExportedTypesRule} from './require-exported-types.js';

const packageContent = JSON.stringify({
	name: 'require-exported-types-fixture',
}, null, '\t');
const indexContent = 'export {type ExportedType} from \'./source/exported-type\';\n';
const exportedTypeContent = 'export type ExportedType = {\n\tvalue: string;\n};\n';

const {ruleTester, fixturePath} = createTypeAwareRuleTester({
	'package.json': `${packageContent}\n`,
	'index.d.ts': indexContent,
}, {
	ruleTester: {
		languageOptions: {
			parserOptions: {
				projectService: {
					allowDefaultProject: ['source/*.ts*'],
				},
			},
		},
	},
});

const withIndexReference = code => `/// <reference path="${fixturePath('index.d.ts')}" />\n${code}`;

ruleTester.run('require-exported-types', requireExportedTypesRule, {
	valid: [
		{
			code: withIndexReference(exportedTypeContent),
			filename: fixturePath('source/exported-type.d.ts'),
		},
	],
	invalid: [],
});

Changes:

  1. Only index.d.ts and package.json files are being created on disk.
  2. Had to update allowDefaultProject to include 'source/exported-type.d.ts'.
  3. Had to add /// <reference path="${fixturePath('index.d.ts')}" /> in code to include index.d.ts, otherwise index.d.ts seems to not be part of the program.

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does it make sense to set them to undefined instead?

Makes sense. Done.

Copy link
Collaborator

@som-sm som-sm left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

PR LGTM, just few comments regarding adding some test cases.

Repository owner deleted a comment from claude bot Oct 16, 2025
Repository owner deleted a comment from claude bot Oct 16, 2025
Repository owner deleted a comment from claude bot Oct 17, 2025
@sindresorhus sindresorhus merged commit 3c26560 into main Oct 17, 2025
9 checks passed
@sindresorhus sindresorhus deleted the rule-tests branch October 17, 2025 00:23
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants