esm: improve check for ESM syntax
This PR speeds up and simplifies the error path for when users require
ES modules. In particular, the previous hasEsmSyntax
helper used Acorn to parse the required module’s source code to find ESM syntax. In this refactor, the check moves into C++ and relies on V8 throwing a SyntaxError
with one of the error messages that we know are caused by ESM syntax in a module compiled as CommonJS. This allows us to eliminate one use of Acorn from the codebase.
This also creates the helper function that https://github.com/nodejs/node/pull/50096 needs.
@nodejs/loaders @nodejs/cpp-reviewers