import visitIdentifierNodes from './visitIdentifierNodes';

export default function findUndefinedIdentifiers(ast, globalVariables = []) {
  const result = new Set();
  visitIdentifierNodes(
    ast.program,
    ({ isReference, isJSX, name, definedInScope }) => {
      if (isJSX && !definedInScope.has('React')) {
        result.add('React'); // Implicit dependency
      }
      if (isReference) {
        return;
      }
      if (definedInScope.has(name)) {
        return;
      }
      if (isJSX && /^[a-z]/.test(name)) {
        return;
      }
      result.add(name);
    },
    {
      definedInScope: new Set(globalVariables),
    },
  );
  return result;
}

import visitIdentifierNodes from './visitIdentifierNodes';

const JSX_BUILT_IN_ELEMENT_PATTERN = /^[a-z]/;

export default function findUndefinedIdentifiers(ast, globalVariables = []) {
  const result = new Set();
  visitIdentifierNodes(
    ast.program,
    ({ isReference, isJSX, name, definedInScope }) => {
      if (isJSX && !definedInScope.has('React')) {
        result.add('React'); // Implicit dependency
      }
      if (isReference) {
        return;
      }
      if (definedInScope.has(name)) {
        return;
      }
      if (isJSX && JSX_BUILT_IN_ELEMENT_PATTERN.test(name)) {
        return;
      }
      result.add(name);
    },
    {
      definedInScope: new Set(globalVariables),
    },
  );
  return result;
}

Make regexp to find built-in jsx elements a constant
Make regexp to find built-in jsx elements a constant

This will prevent having to prepare the regexp every time it is used.
It also better explains the intent of the conditional which should help
readers of the code in the future.
