Skip to content

When parsing more than one file "start" and "end" accumulate values #39

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

Open
lostsequence opened this issue Jan 31, 2022 · 5 comments
Open

Comments

@lostsequence
Copy link

lostsequence commented Jan 31, 2022

const files = ['file1', 'file2']  
files.forEach(f => {  
    const text = Deno.readTextFileSync(f);  
    const ast = parse(text, {  
		target: "es2020",  
		syntax: "typescript",  
		comments: false,  
		tsx: true,  
	});  
})  

In that case for 'file1' "span.start" for Module will be "0"
But for 'file2' "span.start" will be "span.end" of another file.

Is it possible to have "0" as "span.start" for each file ?

@lostsequence lostsequence changed the title When parsing more than one file "start" and "end" accumulates When parsing more than one file "start" and "end" accumulate values Feb 1, 2022
@eibens
Copy link

eibens commented Oct 23, 2022

Any update on this?


I patched this on my end by running the parser in a worker. It works, but starting up the worker takes about 900 ms on my system.

import { parse } from "swc";

self.postMessage("ready");

self.onmessage = (evt) => {
  try {
    const program = parse(evt.data, {
      target: "es2019",
      syntax: "typescript",
      comments: true,
      tsx: true,
    });
    const ast = JSON.stringify(program);
    self.postMessage(ast);
  } catch (e) {
    self.postMessage({
      error: {
        message: e.message,
      },
    });
  }
};
export function parse(source: string): Promise<string> {
  return new Promise((resolve, reject) => {
    const url = new URL("./parse.worker.js", import.meta.url);
    const worker = new Worker(url.href, { type: "module" });

    worker.addEventListener("message", (event) => {
      if (event.data !== "ready") {
        reject("Worker failed to initialize");
        return;
      }

      worker.addEventListener("message", (event) => {
        resolve(event.data);
      }, { once: true });

      worker.postMessage(source);
    }, { once: true });

    worker.addEventListener("error", (event) => {
      reject(event.error);
    });

    worker.addEventListener("messageerror", (event) => {
      reject(event.data);
    });
  });
}

@eibens
Copy link

eibens commented Nov 27, 2022

The problem comes from the main package. Here is the issue.

Unfortunately, most of the solutions there do not currently work. You have to either instantiate the module for each file, or subtract program.span.start from all spans in the AST. I am now using the latter approach, since downloading the WASM file for every parse call caused unacceptable delays and instability on bad networks.

@rizrmd
Copy link

rizrmd commented Dec 10, 2022

What happen when span.start exceeding Number.MAX_SAFE_INTEGER ? Does swc crash ?

@eibens
Copy link

eibens commented Dec 10, 2022

@rizrmd You have to parse 9,007,199,254,740,991 bytes (~9 Petabytes) of source code to get to that point. What are you planning to do?

@rizrmd
Copy link

rizrmd commented Dec 10, 2022

Haha, sorry, just curious… I never thought MAX_SAFE_INTEGER is 9 petabytes

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

3 participants