Comments (5)
The issue is that the map
function assumes that code
doesn't contain multiple newlines:
https://github.com/davidbonnet/astring/blob/master/src/astring.js#L997
However, it does (astring uses the raw
property):
{
"type": "Literal",
"value": "var COMPILED=!0,goog=goog||{};goog.global=this;goog.DEBUG=!0;goog.LOCALE=\"en\";goog.provide=function(a){if(!COMPILED){if(goog.isProvided_(a))t",
"raw": "\"var COMPILED=!0,goog=goog||{};goog.global=this;goog.DEBUG=!0;goo\\\ng.LOCALE=\\\"en\\\";goog.provide=function(a){if(!COMPILED){if(goog.isProvided_(a))t\""
}
A few potential solutions:
- Use
value
instead ofraw
, which lacks the syntactical newlines. This is what Escodegen seems to do.- Downside: This will slow down astring, since all strings will need to be JSON'd / escaped properly, resulting in additional string allocations / garbage collection / processing / etc.
- We could do a search through the string for newlines, and only resort to this when a newline is found. While this is O(literal.length), it doesn't require any string allocations in the common case.
- Downside 2: This does not match babel's behavior, causing a multiline string test case to fail.
- Change
map
to scan for newlines. Rather than just check the end of the string, do a search through the string for newlines.- Downside: The
astring
side (e.g. ignoringsource-map
operations) ofmap
is currently O(1). This will make it O(code.length).
- Downside: The
I think some variant on the first option would be cleanest -- check for newline, special case if found.
from astring.
Thanks @jvilk for reporting this.
Solution 1 is best (O(code.length) will always be worse than O(literal.length)). Inspecting the raw value without JSONing should keep Astring fast.
from astring.
The question then is: Do we use value
instead of raw
in that case? That behavior outputs the string on a single line, which does not match Babel, which outputs the string as multiple lines.
If you want astring to match Babel, then there needs to be a clean way to write a multiline code segment.
from astring.
No need to match Babel specifically. And counting the occurrences of \\\r?\n
should suffice.
from astring.
I've created a PR for this: #354
I went with option 2 to start and establish test cases, since it's a simple and very reliable+correct approach. Though it runs for each character of the code it's practically very fast, about 300 MB/s on my macbook. But I can investigate option 1 as well if we think the cost is high.
Something notable is that simple JSON.stringification in option 1 does not work for template literals. That is, if we see a template literal such as
const a = `hello
world`;
JSON.stringify'ing that value into const a = "hello\nworld"
would be a change in the syntax tree. We could manually trim out the quotes for backticks, but that leaves unnecessarily escaped characters in the string, which is ugly imo.
from astring.
Related Issues (20)
- Super class (class extends) isn't parenthesized correctly
- Debugger statements lack sourcemaps HOT 3
- Version 1.7.3 breaks imports HOT 2
- Bad comment location HOT 1
- *mistake*
- Missing parentheses in LogicalExpression HOT 1
- Support ES2022
- Skip unknown node types HOT 1
- `baseGenerator` is untyped HOT 2
- Does not handle undefined private properties HOT 1
- chore(package): use `files` in `package.json` instead of `.npmignore` HOT 4
- The "minify" mode HOT 1
- classes with computed property definitions not supported HOT 1
- Comments HOT 4
- Add types to package exports HOT 2
- Issue with preserving parenthesis HOT 1
- astring 1.8.5 generates invalid code when `??` is used HOT 1
- LogicalExpression with nullish coalescing on the left side produces invalid syntax HOT 4
- TypeError: state.generator[node.type] is not a function HOT 1
- bug: `import.*` object access causes failure HOT 3
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 astring.