Repository: gatsbyjs/gatsby
File: packages/gatsby/src/internal-plugins/webpack-theme-component-shadowing/index.js (JavaScript)

Code before:
        let dir
        try {
          // we use fs/path instead of require.resolve to work with
          // TypeScript and alternate syntaxes
          dir = fs.readdirSync(path.dirname(possibleComponentPath))
        } catch (e) {
          return false
        }
        const existsDir = dir.map(filepath => path.basename(filepath))

        const isExactPath = existsDir.includes(
          path.basename(possibleComponentPath)
        ) // find if there is an exact path match

        if (!isExactPath) {
          const matchingExtension = this.extensions.find(ext =>
            existsDir.includes(path.basename(possibleComponentPath) + ext)
          ) // find extension
          return matchingExtension
            ? possibleComponentPath + matchingExtension
            : false // if extension matches, create path
        } else {
          return possibleComponentPath
        }
      })
      .find(path => path != false) // if full path matched, return it
  }

  getMatchingThemesForPath(filepath) {
    // find out which theme's src/components dir we're requiring from
    const allMatchingThemes = this.themes.filter(({ themeDir }) =>
      filepath.includes(path.join(themeDir, `src`))
    )

    // The same theme can be included twice in the themes list causing multiple
    // matches. This case should only be counted as a single match for that theme.
    return _.uniqBy(allMatchingThemes, `themeName`)
  }

  // given a theme name, return all of the possible shadow locations
  getBaseShadowDirsForThemes(theme) {
    return Array.from(this.themes)
      .reverse()
      .map(({ themeName, themeDir }) => {
        if (themeName === theme) {
          return path.join(themeDir, `src`)
        } else {
          return path.join(themeDir, `src`, theme)
        }
      })
  }

Review comment (suggestion):
We can make this filter more succinct if all values are falsey or a matched path, a suggestion to consider:

```suggestion
      .find(Boolean) // if full path matched, return it
```

Code after:
        let dir
        try {
          // we use fs/path instead of require.resolve to work with
          // TypeScript and alternate syntaxes
          dir = fs.readdirSync(path.dirname(possibleComponentPath))
        } catch (e) {
          return false
        }
        const existsDir = dir.map(filepath => path.basename(filepath))

        const isExactPath = existsDir.includes(
          path.basename(possibleComponentPath)
        ) // find if there is an exact path match

        if (!isExactPath) {
          const matchingExtension = this.extensions.find(ext =>
            existsDir.includes(path.basename(possibleComponentPath) + ext)
          ) // find extension
          return matchingExtension
            ? possibleComponentPath + matchingExtension
            : false // if extension matches, create path
        } else {
          return possibleComponentPath
        }
      })
      .find(Boolean) // if full path matched, return it
  }

  getMatchingThemesForPath(filepath) {
    // find out which theme's src/components dir we're requiring from
    const allMatchingThemes = this.themes.filter(({ themeDir }) =>
      filepath.includes(path.join(themeDir, `src`))
    )

    // The same theme can be included twice in the themes list causing multiple
    // matches. This case should only be counted as a single match for that theme.
    return _.uniqBy(allMatchingThemes, `themeName`)
  }

  // given a theme name, return all of the possible shadow locations
  getBaseShadowDirsForThemes(theme) {
    return Array.from(this.themes)
      .reverse()
      .map(({ themeName, themeDir }) => {
        if (themeName === theme) {
          return path.join(themeDir, `src`)
        } else {
          return path.join(themeDir, `src`, theme)
        }
      })
  }