globalizejs / globalize-compiler

Globalize.js runtime compiler for your formatters and parsers
Other
10 stars 14 forks source link

Give better error message on syntax errors #41

Open dantman opened 4 years ago

dantman commented 4 years ago

The errors generated by globalize-compiler are pretty cryptic and not useful when it fails to parse a file.

time.js

import Globalize from 'globalize';

/**
 * Intl Format a short time string e.g. "9:30 AM" in en-US
 */
export const formatTime = Globalize.dateFormatter({ date: 'short' });
globalize-compiler -l en -o en.js time.js

Gives the following result:

{...}/node_modules/globalize-compiler/node_modules/esprima/esprima.js:5702
            throw e;
            ^
Error: Line 1: Unexpected token
    at constructError ({...}/node_modules/globalize-compiler/node_modules/esprima/esprima.js:2407:21)
    at createError ({...}/node_modules/globalize-compiler/node_modules/esprima/esprima.js:2426:17)
    at unexpectedTokenError ({...}/node_modules/globalize-compiler/node_modules/esprima/esprima.js:2500:13)
    at tolerateUnexpectedToken ({...}/node_modules/globalize-compiler/node_modules/esprima/esprima.js:2509:21)
    at parseStatementListItem ({...}/node_modules/globalize-compiler/node_modules/esprima/esprima.js:3973:21)
    at parseScriptBody ({...}/node_modules/globalize-compiler/node_modules/esprima/esprima.js:5490:25)
    at parseProgram ({...}/node_modules/globalize-compiler/node_modules/esprima/esprima.js:5506:16)
    at Object.parse ({...}/node_modules/globalize-compiler/node_modules/esprima/esprima.js:5690:23)
    at Object.extractor [as extract] ({...}/node_modules/globalize-compiler/lib/extract.js:39:17)
    at {...}/node_modules/globalize-compiler/bin/globalize-compiler.js:84:27 {
  lineNumber: 1,
  description: 'Unexpected token',
  index: 0
}
error Command failed with exit code 1.
info Visit https://yarnpkg.com/en/docs/cli/run for documentation about this command.

In this example it's clear that globalize-compiler doesn't support ES20153. But It doesn't tell you what file the failure is. And if the error was somewhere in the middle of the file it wouldn't necessarily be easy to discover what the issue is.

rxaviers commented 4 years ago

Agreed it should have better error handling/messages. Any PRs addressing that will be welcome.

Using the compiler API (instead of the CLI) gives you better options for example to pass the AST directly (where you can use babel to understand ES2015 or newer). Another approach worth using is https://github.com/rxaviers/globalize-webpack-plugin/ (that will handle exactly that for you).

Thanks

dantman commented 4 years ago

I'm using create react app so the latter is not an option.