thx / gogocode

GoGoCode is a transformer for JavaScript/Typescript/HTML based on AST but providing a more intuitive API.
MIT License
5.47k stars 417 forks source link

BUG: ast cannot be parsed cause replace `export class` is work when source is export function #245

Open HoikanChan opened 7 months ago

HoikanChan commented 7 months ago
// source code
export function myFunction() { 
    return 1; 
}

// transform script
function transform(fileInfo, api, options) {
  const $ = api.gogocode
  const source = fileInfo.source

  // return your transformed code here
  return $(source)
   .replace(`
        export class $_$1 { $_$2 }`,
        `
        class $_$1 { $_$2 }
        __export__('xxx', '${name}', ${name});
      `
    ).root().generate()
}

Got error:

/**
Something goes wrong...
Error: replace failed: 
        class myFunction { return 1; }
        __export__('xxx', '', );
       cannot be parsed!
**/
shuerguo999 commented 6 months ago

class myFunction { return 1; }