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

JSON is parsed as valid Javascript #67

Closed
Jacob-Roberts opened this issue Nov 19, 2020 · 4 comments
Closed

JSON is parsed as valid Javascript #67

Jacob-Roberts opened this issue Nov 19, 2020 · 4 comments

Comments

@Jacob-Roberts
Copy link

When using this library with the following code:

l := js.NewLexer(parse.NewInputBytes(data))
for {
	tt, _ := l.Next()
	if tt == js.ErrorToken {
		if l.Err() != io.EOF {
			return false, "The Javascript file is not valid", l.Err()
		} else {
			return true, "", nil
		}
	}
}

and the following input

{
    "elementId": "mJn9mpMQwIYyBe",
}

No error is detected. This returns true.

@tdewolff
Copy link
Owner

Hey @Jacob-Roberts , that is correct! The lexer does not detect semantic errors (and it shouldn't!) but just makes sure that the input can be converted into valid JS tokens (such as keywords, strings, number literals, and interpunction/operators). In this case the source contains all valid tokens. If you want to find semantic errors, you should run the parser instead (js.NewParser). This should return the following:

ERROR: cannot minify in.js: unexpected : in expression on line 2 and column 16
    2:     "elementId": "mJn9mpMQwIYyBe",
                      ^

Let me know if that works for you or whether you have additional questions! ;-)

@Jacob-Roberts
Copy link
Author

Thank you!

@Jacob-Roberts
Copy link
Author

Maybe as an improvement point, you could point out in the Readme for the JS page that the parser is available. It seems from the README that the Lexer is the only option

@tdewolff
Copy link
Owner

Good idea, I've added a short description, hopefully that covers it.

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