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

Give better error message on syntax errors #41

Open
dantman opened this issue Feb 8, 2020 · 2 comments
Open

Give better error message on syntax errors #41

dantman opened this issue Feb 8, 2020 · 2 comments

Comments

@dantman
Copy link

dantman commented Feb 8, 2020

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
Copy link
Member

rxaviers commented Feb 8, 2020

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
Copy link
Author

dantman commented Feb 8, 2020

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

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

2 participants