facebook/idx

babel-plugin does not verify idx was imported from the package idx

Closed this issue · 5 comments

pierr commented

Babel plugin doesn't verify idx comes from an import from idx package.
This change could be dangerous if somebody has a function called idx in their code.

I was trying to add idx in create-react-app by default as it would be usefull for many people and a way to expand its use.

See facebook/create-react-app#1874

I know this should be check here.
I will try to make a PR for this.

cc @gaearon

Ahh... indeed, we should add this check. A PR would be most welcome!

I found this snippet from an internal plugin that may serve as a useful starting point:

function isDefinedLocally(path, name) {
  const binding = path.scope.getBinding(name);
  if (!binding) {
    return false;
  }

  // Binding comes from import.
  if (binding.kind === 'module') {
    return false;
  }

  // Binding comes from require.
  if (
    binding.path.isVariableDeclarator() &&
    binding.path.get('init.callee').isIdentifier({name: 'require'})
  ) {
    return false;
  }

  // Otherwise, defined locally.
  return true;
}

You probably want the inverse of this with some more checks about what is actually imported, but as I said... starting point.

pierr commented

OK thanks, i'll try to work on this today.

Awesome! Glad to see this addressed. Then we can include it in Create React App.

Did you have an opportunity to look at this yet?

Fixed by #22.