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

[Feature Request] Replace on match, but prepend others #28

Open
daymxn opened this issue Dec 1, 2024 · 4 comments
Open

[Feature Request] Replace on match, but prepend others #28

daymxn opened this issue Dec 1, 2024 · 4 comments

Comments

@daymxn
Copy link
Collaborator

daymxn commented Dec 1, 2024

If there's an existing header that isn't correct, but is of the same type, then I'd like to update the header. But if there's a header that is not the same type, then I'd like to prepend or append- depending on the context.

Example

Given the file:

/* eslint-disable unicorn/consistent-destructuring */
/**
 * Copyright 2020 Daymon
 */

I'd like to only update the year, while preserving the eslint pragma:

/* eslint-disable unicorn/consistent-destructuring */
/**
 * Copyright 2024 Daymon
 */

Edge case

If the string isn't a pragma, and there's a license missing, then ideally the license would be prepended to the start of the file.

For example:

/**
 * This is the main function
 */
export function main() {}

Would become:

/**
 * Copyright 2024 Daymon
 */

/**
 * This is the main function
 */
export function main() {}

Proposal

This may be leading into "too complex of a use-case" territory, but a possible solution could be having support for multiple onNonMatchingHeader with configurable regex and tolerance.

{
  "notice/notice": [
    "error",
    {
      templateFile: "assets/license-header.txt",
      onNonMatchingHeader: "prepend",
      onNonMatchingHeaderOverrides: [
        {
          matches: /^\/* eslint-disable/,
          behavior: "append",
        },
        {
          tolerance: 0.9, // if there's an existing template that's within tolerance, but not a 1:1 match- then replace
          behavior: "replace",
        },
      ],
    },
  ],
}
@nickdeis
Copy link
Owner

nickdeis commented Dec 1, 2024

Hey @daymxn ,

First off, thanks for filing such a clear issue and proposal. This is a great idea. To me, it has a "upsert/compare if match" vibe. You also caught me at a pretty good time: I'm converting a lot of my older repos to typescript so I can maintain them a little better. I do always offer the issue filer to create their own MR, but if not: I like this idea and want to do this within the next 2-4 weeks. Let me know if that works for you.

Best,
Nick

@daymxn
Copy link
Collaborator Author

daymxn commented Dec 3, 2024

Hey @daymxn ,

First off, thanks for filing such a clear issue and proposal. This is a great idea. To me, it has a "upsert/compare if match" vibe. You also caught me at a pretty good time: I'm converting a lot of my older repos to typescript so I can maintain them a little better. I do always offer the issue filer to create their own MR, but if not: I like this idea and want to do this within the next 2-4 weeks. Let me know if that works for you.

Best, Nick

Hey!

That's great to hear! I'd be open to creating a PR/MR for it, but it may be worth doing the typescript migration first- as that'd make adding the feature easier. It'd also make the typescript migration less painful.

If you haven't already started, I could also create a PR for converting the repo to TS? I already have a branch with most of the migration done anyhow- but if you've already got your own WIP branch, then I can hold off on it :)

@nickdeis
Copy link
Owner

nickdeis commented Dec 4, 2024

Hey @daymxn,
I would be happy reviewing both MRs. I'm an not an over-opinionated engineer, just an overworked one 😃. So any help I can get I appreciate.
Best,
Nick

@nickdeis
Copy link
Owner

Hey @daymxn, did you get my email? And if not, is there another email other than [email protected] that I can send you an email?

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

No branches or pull requests

2 participants