sindresorhus/refined-github

False positive in `link-to-changelog-file`, linking to a `.js` file which is not the changelog #4577

jdreesen posted onGitHub

Please ensure:

  • The bug is caused by Refined GitHub. It doesn't happen if I disable the extension.

Describe the problem and how to replicate it

RGH adds the CHANGELOG button on the top of the releases page of hyper, with a link to the https://github.com/vercel/hyper/blob/HEAD/release.js page, which is not the changelog (I'm actually not sure whether there even is one, though...)

Screenshots/video/gif showing the issue, if it’s visual

grafik

Console errors, if any

No response

Example URL

https://github.com/vercel/hyper/releases

Browser(s) used

Firefox 90.0 (64-Bit)


Currently RG takes into account only the filenames, not their extensions too:

<details> <summary>click to see the relevant code lines:</summary>

https://github.com/sindresorhus/refined-github/blob/afa8da1a27e80f6364a3e68ab4b4c5a7c1916d2e/source/features/link-to-changelog-file.tsx#L18 https://github.com/sindresorhus/refined-github/blob/afa8da1a27e80f6364a3e68ab4b4c5a7c1916d2e/source/features/link-to-changelog-file.tsx#L20-L26

</details>

What would the optimal improvement to findChangelogName() be? To add just a disallowed (.js) or an allowed-only (.md,.txt,.rst) file extension list?

<details> <summary>In details:</summary>

The former would be to have something like (maybe adding more extensions):

const changelogExtensionsDisallowed = new Set(['js']); 

function findChangelogName(files: string[]): string | false { 
  for (const file of files) { 
    if (changelogNames.has(file.toLowerCase().split('.', 1)[0])) { 
      if (!changelogExtensionsDisallowed.has(file.toLowerCase().split('.').pop())) { // get the last element of the split
        return file; 
      } 
    }

The latter would be something like this:

const changelogExtensions = new Set(['md', 'txt', 'rst', '']); 

function findChangelogName(files: string[]): string | false { 
  // ..
    if (changelogNames.has(file.toLowerCase().split('.', 1)[0])) { 
      if (changelogExtensions.has(file.toLowerCase().split('.').pop())) { 
        return file; 
      } 
  }

</details>

   I'd say the latter is better. If you find my approach useful, I'd like to step up.

posted by darkred over 3 years ago

We have a markdown extension regex somewhere, we just need to use regex.test(extension)

Also don't repeat the split, but instead:

const [name, extension] = file.toLowerCase().split('.', 2);

If files have more dots then they don't match our search.

But… really we just need a single regex here:

const regex /* better name please */ = /^(changelog|news|changes|history|release|whatsnew)\.(mdx?etccopy the other regex)$/
function findChangelogName(files) {
    return files.find(name => regex.test(name));
}
posted by fregante over 3 years ago

Thanks for the guidance. I made PR #4578 following your instructions.

posted by darkred over 3 years ago

Thx for fixing this so fast! @darkred

posted by jdreesen over 3 years ago

Fund this Issue

$0.00
Funded

Pull requests