Search code examples
javascriptes6-moduleslexical-analysis

Is there relatively simple way to find all exported names from JavaScript text


So let's say we have some JavaScript es module as text

const ESMText = "export const answer = 42;"

I checked ECMAScript documents, and logic for export is quite complicated, is there kinda simple way to finde out all exports without executing it or implementing almost full spec lexical analysis stuff (tokenizer, etc...) ?


It should be done in browser using JavaScript (maybe wasm)


Should be mentioned: obviously parse file using existing parser is the right answer, but whole point of question is to find kinda tricky way to do it


it could be easy done with regex, but there is two edge cases that i can't handle:

// destructuring assignment
export const { name1, name2: bar } = o;
export const [ name1, name2 ] = array;

//simultaneous declaration and assignment
export const name1 = 1, name2 = 2/*, … */;

may be someone will figure out something of it


Solution

  • at the end, i did it with acorn (inside worker):

    self.importScripts("https://cdnjs.cloudflare.com/ajax/libs/acorn/8.8.2/acorn.min.js");
    const exp = new RegExp("export", "g");
    
    const ESMText = "export const answer = 42; const innerVar = 'str'"
    
    for (const match of ESMText.matchAll(exp)) {
        const parser = new acorn.Parser({
            ecmaVersion: 2020,
            sourceType: "module"
        }, text, match.index);
        parser.nextToken();
        const statement = parser.parseStatement(true, true, {});
        console.log(statement);
    }
    

    i checked acorn docs and its implementation, after that i find out how to solve my problem with pretty god performance.
    in the code abow parsed only export statements