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

Ability to remove the default condition #6

Open
regseb opened this issue Nov 27, 2024 · 0 comments
Open

Ability to remove the default condition #6

regseb opened this issue Nov 27, 2024 · 0 comments

Comments

@regseb
Copy link

regseb commented Nov 27, 2024

Description

The eslint-plugin-jsdoc library would need a tool to get the types defined in the exports property. resolve-import could work, but it would need to be able to remove the default condition.

Proposal

You can support negative conditions. If you want to undo a default condition, you can use the ! prefix.

export const resolveConditionalValue = (
  exp: ConditionalValue,
  options: ResolveImportOpts,
): string | null => {
  if (exp === null || typeof exp === 'string') return exp
  if (Array.isArray(exp)) {
    for (const e of exp) {
      const r = resolveConditionalValue(e, options)
      if (r) return r
    }
    return null
  }

  const conditions = new Set(['default'])
  for (const condition of options.conditions ?? ['import', 'node']) {
    if (condition.startsWith('!')) {
      conditions.delete(condition.slice(1))
    } else {
      conditions.add(condition)
    }
  }
  for (const [k, v] of Object.entries(exp)) {
    if (conditions.has(k)) {
      return resolveConditionalValue(v, options)
    }
  }
  return null
}
console.log(await resolveImport('pkg', '/path/to/y.js', { conditions: ['!default', 'types'] }));

Related links

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

1 participant