Comments (5)
No, never mind, if an actual newline character occur inside a string token and actual new line, Node.js doesn't even like it either.
$ cat | node
var i = "test
value"
[stdin]:1
var i = "test
^^^^^
SyntaxError: Invalid or unexpected token
ES5 (which is what slimit supports) doesn't have multiline strings like Python does, so fortunately for the parsers, this is a a valid syntax error in the provided ES5 script which the parser correctly provided.
However, if you meant to an escaped sequence representing the newline, this will then work (note the raw string prefix r
):
>>> from slimit.parser import Parser
>>> print(Parser().parse(r'var i = "test\nvalue"').to_ecma())
var i = "test\nvalue";
from slimit.
Well I had this problem when trying to scrape information out of a working JavaScript code on a high-traffic website.
from slimit.
Can you please provide the link to the example that choked?
from slimit.
Anyway, I do see what you mean - I had mistakenly used my patched version of slimit that correctly reported that as a parsing error. Anyway, the correct behavior with that input should throw a SyntaxError exception, which my patched version (and calmjs.parse
) does. The definition in the ECMA-262 specification that states this as an invalid syntax is defined in section 7.8.4 (specifically "A line terminator character cannot appear in a string literal" at the bottom of that section, where a "line terminator" includes newline characters)
To make things most clear, this is the input JavaScript with the invalid syntax:
var i = "test
value"
Assume that input is assigned to program
in the following Python code:
>>> from slimit.parser import Parser
>>> parser = Parser()
>>> node = parser.parse(program)
Illegal character '"' at 1:8 after LexToken(EQ,'=',1,6)
Illegal character '"' at 1:19 after LexToken(ID,'value',1,14)
>>> print(node.to_ecma())
var i = test;
value;
This changed the program entirely, as slimit erroneously fully parsed the input without raising an error and produced an incorrect AST, and this is where my initial confusion lied (when I saw the output which I then used as input, then I noticed the quotes on the original input). The correct behavior is implemented in calmjs.parse
, which correctly process this as a syntax error:
>>> from calmjs.parse import es5
>>> es5(program)
Traceback (most recent call last):
...
calmjs.parse.exceptions.ECMASyntaxError: Illegal character '"' at 1:9 after '=' at 1:7
from slimit.
Well, I’m probably mistaken: this should have been a non-working JavaScript extract among the working ones, because I have the same kind of error with newlines inside strings in my web browser’s console.
from slimit.
Related Issues (20)
- Syntatically wrong callback function validation check succeeds even though it would fail in a browser. HOT 2
- SyntaxError: Unexpected token (CLASS, 'class') HOT 1
- Unexpected token (ID, 'params') HOT 1
- Unexpected token (DELETE, 'delete') HOT 1
- Having promise catch statement breaks the minification HOT 1
- Release? HOT 4
- Minify fails on jquery file HOT 1
- Bug: Error when parsing "`" HOT 2
- how can i get the line number of the node in ast tree? HOT 2
- parser option
- Lose "()" after parse and cause Syntax error in js HOT 3
- Proposing a PR to fix a few small typos
- Release please: No longer installable on Python 3 due to setuptools dropping build_py_2to3 HOT 1
- keywords incorrectly disallowed as object property names HOT 1
- update pypi to reflect python 3 support
- Is there any plans for supporting ES6. HOT 1
- Switch default statement position not preserved (it is not always the last statement)
- Unexpected Token error -- script or slimit issue? HOT 2
- Silence ply errors HOT 4
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from slimit.