Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: allow specifying filename in block meta #318

Open
wants to merge 6 commits into
base: main
Choose a base branch
from

Conversation

JoshuaKGoldberg
Copy link
Contributor

Fixes #226.

Per the RFC, does not include any name deduplication. That should already be done by ESLint itself.

Copy link
Member

@nzakas nzakas left a comment

Choose a reason for hiding this comment

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

This looks like a good start. I left a couple notes on some edge cases to address.

Also, can you please add some docs covering this feature?

assert.strictEqual(blocks[0].filename, "abc/def.js");
});

it("should parse a single-quoted filename in a directory from meta", () => {
Copy link
Member

Choose a reason for hiding this comment

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

Can you add an integration test that uses ESLint to process a file with slashes in the code block filename?

I think it should work okay but I'm not 100% sure if there's some assumption that the filename doesn't have a slash somewhere.

Copy link
Member

Choose a reason for hiding this comment

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

Here's a PR to add tests in the core to confirm this works: eslint/eslint#19425

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Is 235afbc what you're looking for on this end?

Copy link
Member

Choose a reason for hiding this comment

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

Yup. 👍

src/processor.js Outdated
* @returns {string | null | undefined} The filename, if parsed from block meta.
*/
function fileNameFromMeta(block) {
return block.meta?.match(codeBlockFileNameRegex)?.groups.filename;
Copy link
Member

Choose a reason for hiding this comment

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

I think we need some kind of validation on the filename characters to ensure it's a valid filename. Spaces, in particular may cause problems.

I'm not sure if it would make sense to throw an error or just replace any unexpected characters with a _ (or another character).

Copy link
Contributor Author

Choose a reason for hiding this comment

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

What if someone wants to have a space in the file name? Is there a source for what ESLint considers a valid filename?

Copy link
Member

Choose a reason for hiding this comment

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

It's not what ESLint considers a valid filename, it's what the system considers a valid filename. Because most systems require special syntax to support spaces (surround the filename in quotes), I think it's safe to assume we should not have spaces.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

👍 Added a \s+ replacer.

nzakas
nzakas previously approved these changes Feb 19, 2025
Copy link
Member

@nzakas nzakas left a comment

Choose a reason for hiding this comment

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

Just a bit of cleanup in the docs and I think we'll be good to go pending @mdjermanovic's review.

assert.strictEqual(blocks[0].filename, "abc/def.js");
});

it("should parse a single-quoted filename in a directory from meta", () => {
Copy link
Member

Choose a reason for hiding this comment

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

Yup. 👍

Co-authored-by: Nicholas C. Zakas <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Configure file name based on block meta
3 participants