ajitid / fzf-for-js

Do fuzzy matching using FZF algorithm in JavaScript
https://fzf.netlify.app
BSD 3-Clause "New" or "Revised" License
888 stars 21 forks source link

'positions' is sometimes undefined #70

Closed peterbe closed 3 years ago

peterbe commented 3 years ago

I just tried installing 0.4.1 and using it. See https://github.com/mdn/yari/pull/4484 Thankfully I manually tested it in my browser too because I managed to stumble into an error that I really struggle to reproduce now. I wasn't able to capture what the input was when it happened.

But basically, the result's .positions was undefined. I would have expected it to be a new Set() at least.

Looking at this code: https://github.com/ajitid/fzf-for-js/blob/3de0f96210facb8efcd02f7a7fac71df45e81e61/src/lib/matchers.ts#L49 it seems it tries to fall back to an empty Set but only if it's already null.

The reason I noticed was that I have this code:

function BreadcrumbURI({
  uri,
  positions,
}: {
  uri: string;
  positions: Set<number>;
}) {
  if (positions.size) {
    const chars = uri.split("");
    return (
      <small>
        {chars.map((char, i) => {
          if (positions.has(i)) {
            return <mark key={i}>{char}</mark>;
          } else {
            return <span key={i}>{char}</span>;
          }
        })}
      </small>
    );
  }
....

and so it would fail hard on if (positions.size) { because it's like doing if (undefined.size) {. My TypeScript is unable to notice because it thinks that FzfResultItem always contains a .positions that is a Set<number>.

peterbe commented 3 years ago

User error! I misinterpreted my own code. So sorry for the distraction.