gogocode icon indicating copy to clipboard operation
gogocode copied to clipboard

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

Open HoikanChan opened this issue 1 year ago • 1 comments

  • version: 1.0.55
  • playground demo: https://play.gogocode.io/#code/N4IglgdgDgrgLgYQPYBMCmIBcI0A8pIBOcABAGYwQDGcYSEJAtgJ4BilNdEAFAJQnASAHQYkxhNHBiEGARgDcwiAF8QAGhAB3IgGtk6LCArVa9EnEIBDCAGcyRRtzJgANmgCSEe2pKWoYHyQoU1t+YBExKnobUgASEgBeX38AOgBzJAyo9AiSKNtSGyRpKjRE8lcPLyQUopK0EVyxXIB6FpIJKRkSZmLCcytbe0JGNBQ81DKACzQJXM7pBljuOsJS3iaSFOcIFG4AAzwCYjyXSxsbEliAfVjY2QEr29iAJhJlfY3RMRS0SyoptxuGA4GhGPwEgA+ASbSLRUgQSyjcogsEpRiWOAAlL3ADaAAYALopABulhcMDQ8lhJFRjBSEigZ1KACFmAcaZEzhcrsBEaNlI9YsA6ejMdjXgTiWSKWhBcpOSRrtcjkQ4MruAByXA6zU+TXC-lyvW8o3KXjU75iEifS3W968dJoCCzTFoPgiBUQdTgaDwAAy1jShjgzCgaBsVEIYGCPqm5wACp1aLMsBZKRobDAAEYANTAaE0ABUwxhsBY0BhlEA
  • your description: I write a transformer to transform export class A() { name: 'hhh'} => class A() { name: 'hhh'}; __export__(FILENAME, 'A', A); there is a error when parsing export function, errr msg isast cannot be parsed cause replace export class is work when source is export function
// 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!
**/

HoikanChan avatar Jan 29 '24 09:01 HoikanChan

class myFunction { return 1; }

shuerguo999 avatar Feb 11 '24 11:02 shuerguo999