humanify icon indicating copy to clipboard operation
humanify copied to clipboard

Error while renaming a variable. (TypeError: Duplicate declaration)

Open Anooxy17 opened this issue 1 year ago • 1 comments

Console output:

[2024-08-28 21:12:49]  Renaming global
[2024-08-28 21:12:49]  Context:     throw CustomError.C(null);
      }
      return {
        us: ______________inputString[7],
        Cp: b
      };
    }
  }
  class Image {
    constructor() {
      this.Sg = this.Tg = this.ya = null;

[2024-08-28 21:12:49]  Renamed to inputString
[2024-08-28 21:12:49]  Processing: 55%
[2024-08-28 21:12:49]  Processing: 100%

C:\Users\ktepe\AppData\Roaming\npm\node_modules\humanifyjs\node_modules\@babel\core\lib\transformation\file\file.js:195
    return new _Error(msg);
           ^

TypeError: Duplicate declaration "Ya"

    at File.buildCodeFrameError (C:\Users\ktepe\AppData\Roaming\npm\node_modules\humanifyjs\node_modules\@babel\core\lib\transformation\file\file.js:195:12)
    at Scope.checkBlockScopedCollisions (C:\Users\ktepe\AppData\Roaming\npm\node_modules\humanifyjs\node_modules\@babel\traverse\lib\scope\index.js:408:22)
    at Scope.registerBinding (C:\Users\ktepe\AppData\Roaming\npm\node_modules\humanifyjs\node_modules\@babel\traverse\lib\scope\index.js:544:16)
    at Scope.registerDeclaration (C:\Users\ktepe\AppData\Roaming\npm\node_modules\humanifyjs\node_modules\@babel\traverse\lib\scope\index.js:500:12)
    at Object.BlockScoped (C:\Users\ktepe\AppData\Roaming\npm\node_modules\humanifyjs\node_modules\@babel\traverse\lib\scope\index.js:259:12)
    at Object.newFn (C:\Users\ktepe\AppData\Roaming\npm\node_modules\humanifyjs\node_modules\@babel\traverse\lib\visitors.js:207:17)
    at NodePath._call (C:\Users\ktepe\AppData\Roaming\npm\node_modules\humanifyjs\node_modules\@babel\traverse\lib\path\context.js:49:20)
    at NodePath.call (C:\Users\ktepe\AppData\Roaming\npm\node_modules\humanifyjs\node_modules\@babel\traverse\lib\path\context.js:35:15)
    at NodePath.visit (C:\Users\ktepe\AppData\Roaming\npm\node_modules\humanifyjs\node_modules\@babel\traverse\lib\path\context.js:85:31)
    at TraversalContext.visitQueue (C:\Users\ktepe\AppData\Roaming\npm\node_modules\humanifyjs\node_modules\@babel\traverse\lib\context.js:89:16)
    at TraversalContext.visitMultiple (C:\Users\ktepe\AppData\Roaming\npm\node_modules\humanifyjs\node_modules\@babel\traverse\lib\context.js:61:17)
    at TraversalContext.visit (C:\Users\ktepe\AppData\Roaming\npm\node_modules\humanifyjs\node_modules\@babel\traverse\lib\context.js:110:19)
    at traverseNode (C:\Users\ktepe\AppData\Roaming\npm\node_modules\humanifyjs\node_modules\@babel\traverse\lib\traverse-node.js:22:17)
    at NodePath.visit (C:\Users\ktepe\AppData\Roaming\npm\node_modules\humanifyjs\node_modules\@babel\traverse\lib\path\context.js:91:52)
    at TraversalContext.visitQueue (C:\Users\ktepe\AppData\Roaming\npm\node_modules\humanifyjs\node_modules\@babel\traverse\lib\context.js:89:16)
    at TraversalContext.visitSingle (C:\Users\ktepe\AppData\Roaming\npm\node_modules\humanifyjs\node_modules\@babel\traverse\lib\context.js:65:19)
    at TraversalContext.visit (C:\Users\ktepe\AppData\Roaming\npm\node_modules\humanifyjs\node_modules\@babel\traverse\lib\context.js:112:19)
    at traverseNode (C:\Users\ktepe\AppData\Roaming\npm\node_modules\humanifyjs\node_modules\@babel\traverse\lib\traverse-node.js:22:17)
    at NodePath.visit (C:\Users\ktepe\AppData\Roaming\npm\node_modules\humanifyjs\node_modules\@babel\traverse\lib\path\context.js:91:52)
    at TraversalContext.visitQueue (C:\Users\ktepe\AppData\Roaming\npm\node_modules\humanifyjs\node_modules\@babel\traverse\lib\context.js:89:16)
    at TraversalContext.visitSingle (C:\Users\ktepe\AppData\Roaming\npm\node_modules\humanifyjs\node_modules\@babel\traverse\lib\context.js:65:19)
    at TraversalContext.visit (C:\Users\ktepe\AppData\Roaming\npm\node_modules\humanifyjs\node_modules\@babel\traverse\lib\context.js:112:19)
    at traverseNode (C:\Users\ktepe\AppData\Roaming\npm\node_modules\humanifyjs\node_modules\@babel\traverse\lib\traverse-node.js:22:17)
    at NodePath.visit (C:\Users\ktepe\AppData\Roaming\npm\node_modules\humanifyjs\node_modules\@babel\traverse\lib\path\context.js:91:52)
    at TraversalContext.visitQueue (C:\Users\ktepe\AppData\Roaming\npm\node_modules\humanifyjs\node_modules\@babel\traverse\lib\context.js:89:16)
    at TraversalContext.visitSingle (C:\Users\ktepe\AppData\Roaming\npm\node_modules\humanifyjs\node_modules\@babel\traverse\lib\context.js:65:19)
    at TraversalContext.visit (C:\Users\ktepe\AppData\Roaming\npm\node_modules\humanifyjs\node_modules\@babel\traverse\lib\context.js:112:19)
    at traverseNode (C:\Users\ktepe\AppData\Roaming\npm\node_modules\humanifyjs\node_modules\@babel\traverse\lib\traverse-node.js:22:17)
    at NodePath.visit (C:\Users\ktepe\AppData\Roaming\npm\node_modules\humanifyjs\node_modules\@babel\traverse\lib\path\context.js:91:52)
    at TraversalContext.visitQueue (C:\Users\ktepe\AppData\Roaming\npm\node_modules\humanifyjs\node_modules\@babel\traverse\lib\context.js:89:16)
    at TraversalContext.visitMultiple (C:\Users\ktepe\AppData\Roaming\npm\node_modules\humanifyjs\node_modules\@babel\traverse\lib\context.js:61:17)
    at TraversalContext.visit (C:\Users\ktepe\AppData\Roaming\npm\node_modules\humanifyjs\node_modules\@babel\traverse\lib\context.js:110:19)
    at traverseNode (C:\Users\ktepe\AppData\Roaming\npm\node_modules\humanifyjs\node_modules\@babel\traverse\lib\traverse-node.js:22:17)
    at traverse (C:\Users\ktepe\AppData\Roaming\npm\node_modules\humanifyjs\node_modules\@babel\traverse\lib\index.js:53:34)
    at NodePath.traverse (C:\Users\ktepe\AppData\Roaming\npm\node_modules\humanifyjs\node_modules\@babel\traverse\lib\path\index.js:119:24)
    at Scope.crawl (C:\Users\ktepe\AppData\Roaming\npm\node_modules\humanifyjs\node_modules\@babel\traverse\lib\scope\index.js:704:10)
    at Scope.init (C:\Users\ktepe\AppData\Roaming\npm\node_modules\humanifyjs\node_modules\@babel\traverse\lib\scope\index.js:675:12)
    at NodePath.setScope (C:\Users\ktepe\AppData\Roaming\npm\node_modules\humanifyjs\node_modules\@babel\traverse\lib\path\context.js:123:53)
    at NodePath.setContext (C:\Users\ktepe\AppData\Roaming\npm\node_modules\humanifyjs\node_modules\@babel\traverse\lib\path\context.js:135:8)
    at new File (C:\Users\ktepe\AppData\Roaming\npm\node_modules\humanifyjs\node_modules\@babel\core\lib\transformation\file\file.js:90:8)
    at normalizeFile (C:\Users\ktepe\AppData\Roaming\npm\node_modules\humanifyjs\node_modules\@babel\core\lib\transformation\normalize-file.js:98:10)
    at normalizeFile.next (<anonymous>)
    at run (C:\Users\ktepe\AppData\Roaming\npm\node_modules\humanifyjs\node_modules\@babel\core\lib\transformation\index.js:21:50)
    at run.next (<anonymous>)
    at C:\Users\ktepe\AppData\Roaming\npm\node_modules\humanifyjs\node_modules\@babel\core\lib\transform-ast.js:23:33
    at Generator.next (<anonymous>)
    at step (C:\Users\ktepe\AppData\Roaming\npm\node_modules\humanifyjs\node_modules\gensync\index.js:261:32)
    at C:\Users\ktepe\AppData\Roaming\npm\node_modules\humanifyjs\node_modules\gensync\index.js:273:13
    at async.call.result.err.err (C:\Users\ktepe\AppData\Roaming\npm\node_modules\humanifyjs\node_modules\gensync\index.js:223:11)
    at C:\Users\ktepe\AppData\Roaming\npm\node_modules\humanifyjs\node_modules\gensync\index.js:50:45

Anooxy17 avatar Aug 28 '24 21:08 Anooxy17

See also:

  • https://github.com/jehna/humanify/issues/8
  • https://github.com/jehna/humanify/issues/20

0xdevalias avatar Aug 29 '24 00:08 0xdevalias

Is there a workaround or anything for this? Or is this blocked by the effort to understand how to perform the rename without collision?

I would be totally okay if it just became variable$1.

brianjenkins94 avatar Oct 18 '24 16:10 brianjenkins94

@brianjenkins94 did you try with v2.1.3 yet? It should have a better logic against invalid identifiers

jehna avatar Oct 18 '24 17:10 jehna

If the identifier conflicts with another identifier that's not generated by Humanify, then there's no valid fix yet.

I wonder what should be done in case of an identifier that we cannot rename? A quick solution would be to prefix it with an underscore

jehna avatar Oct 18 '24 17:10 jehna

I suppose the best solution could be that Humanify would retry with a prompt like:

Rename a function ..., here is a list of names that you cannot use: ${failedIdentifiers}

jehna avatar Oct 18 '24 17:10 jehna

Yes, I am using humanify v2.1.3 with:

  "overrides": {
    "humanifyjs": {
      "webcrack": "latest"
    }
  }

which resolves to webcrack v2.14.1.

If the identifier conflicts with another identifier that's not generated by Humanify, then there's no valid fix yet.

I don't think that's the problem I'm having, all my variables are named v1, p1, etc.

I wonder what should be done in case of an identifier that we cannot rename? A quick solution would be to prefix it with an underscore

That's what I was saying, if it could just add a $1 suffix, like esbuild does to avoid collisions.

here's my repo:

https://github.com/brianjenkins94/reverse-engineered-nodebox

brianjenkins94 avatar Oct 18 '24 18:10 brianjenkins94

Okay, I think I found the issue. Fixing this now

jehna avatar Oct 18 '24 19:10 jehna

I updated my test harness so I can test against the main branch. I'll see if it gets further this time.

Aside: Can I run humanify against multiple files simultaneously? Or would that run the risk of making requests too fast?

brianjenkins94 avatar Oct 18 '24 21:10 brianjenkins94

Just released v2.2.0, should fix this issue for now.

Can I run humanify against multiple files simultaneously? Or would that run the risk of making requests too fast?

I'd say it depends solely on your OpenAI subscription level. Unfortunately there's not that good rate limit throttling/retry logic built into Humanify yet.

jehna avatar Oct 18 '24 21:10 jehna

Added some thoughts about parallelism to issue #167

jehna avatar Oct 18 '24 21:10 jehna

Unfortunately there's not that good rate limit throttling/retry logic built into Humanify yet.

Added some thoughts about parallelism to issue #167

See also:

  • https://github.com/jehna/humanify/issues/2
  • https://github.com/jehna/humanify/issues/139

0xdevalias avatar Oct 20 '24 01:10 0xdevalias

I suppose the best solution could be that Humanify would retry with a prompt like:

Rename a function ..., here is a list of names that you cannot use: ${failedIdentifiers}

@jehna That's what's been proposed in the past, eg.

Also it can happen that a variable gets renamed but another one with the same name already exists in the scope, or the code gets split up in such a way that not all references are renamed.

Off the top of my head, if it's not easily solved via tweaking the prompt, you could potentially have it do a 'refinement/feedback round' where you provide feedback to the LLM + get it to correct the issues (which could be repeated up to X times) eg.

The following variables were already defined in the scope, so we can't use the names you chose. Please choose a new name for them so that they don't clash:

  • X
  • Y
  • Z

Originally posted by @0xdevalias in https://github.com/jehna/humanify/issues/8#issuecomment-1853022637

https://github.com/jehna/humanify/pull/164 feels a bit like a workaround rather than an optimal solution.

0xdevalias avatar Oct 20 '24 01:10 0xdevalias

Related context (from secondary comment on another issue):

Also it can happen that a variable gets renamed but another one with the same name already exists in the scope, or the code gets split up in such a way that not all references are renamed. Maybe use babel's scope-aware rename that also handles these conflicts (have to find a way of getting rid of the _ prefix): astexplorer.net#/gist/7283141e13dab314521744603a95e9b7/05b11370db8d5ef257550b2916b87d6e72e4eb1d

Originally posted by @j4k0xb in https://github.com/jehna/humanify/issues/8

0xdevalias avatar Oct 20 '24 02:10 0xdevalias

Aside: Can I run humanify against multiple files simultaneously? Or would that run the risk of making requests too fast?

In trying to produce benchmark results for #172, I have determined that simultaneous runs cause 429s and cause the application to crash.

Just closing the loop.

brianjenkins94 avatar Oct 21 '24 20:10 brianjenkins94