Giter VIP home page Giter VIP logo

Comments (5)

jvilk avatar jvilk commented on May 13, 2024

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:

  1. Use value instead of raw, 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.
  2. 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. ignoring source-map operations) of map is currently O(1). This will make it O(code.length).

I think some variant on the first option would be cleanest -- check for newline, special case if found.

from astring.

davidbonnet avatar davidbonnet commented on May 13, 2024

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.

jvilk avatar jvilk commented on May 13, 2024

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.

davidbonnet avatar davidbonnet commented on May 13, 2024

No need to match Babel specifically. And counting the occurrences of \\\r?\n should suffice.

from astring.

connor4312 avatar connor4312 commented on May 13, 2024

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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.