Skip to content

Conversation

@GlenDC
Copy link

@GlenDC GlenDC commented Aug 20, 2020

I'm also not sure if it's me making a mistake somewhere,
but the following line of code doesn't seem to work for me:

const findIndex = (c, d) => c - destringifyOffset.first().value;

Seems the first function is returning pretty much the same as what destringifyOffset
already gives me at that point. The following does work for me:

const findIndex = (c, d) => c - destringifyOffset.nodes[0].value;

Didn't modify that in the README example in this PR yet, as I do not know if it's a bug, desired or something I'm simply doing wrong.

@ism
Copy link

ism commented Apr 15, 2021

Hello everyone,

it is always discouraging when examples of something that is new to you doesn't work as expected.

I fixed advanced example to work for me like follows

``
const { refactor } = require('shift-refactor');
const Shift = require('shift-ast');
const { commonMethods } = require('refactor-plugin-common');

// Obfuscated source
const src = var a=['\x74\x61\x72\x67\x65\x74','\x73\x65\x74\x54\x61\x72\x67\x65\x74','\x77\x6f\x72\x6c\x64','\x67\x72\x65\x65\x74','\x72\x65\x61\x64\x65\x72'];var b=function(c,d){c=c-0x0;var e=a[c];return e;};(function(){class c{constructor(d){this[b('0x0')]=d;}['\x67\x72\x65\x65\x74'](){console['\x6c\x6f\x67']('\x48\x65\x6c\x6c\x6f\x20'+this[b('0x0')]);}[b('0x1')](e){this['\x74\x61\x72\x67\x65\x74']=e;}}const f=new c(b('0x2'));f[b('0x3')]();f[b('0x1')](b('0x4'));f[b('0x3')]();}());;

const $script = refactor(src, commonMethods);

const strings = $script(Script > :first-child ArrayExpression > .elements);

const destringifyDeclarator = $script(VariableDeclarator[binding.name="b"][init.params.items.length=2]);

destringifyDeclarator.rename('destringify');

const destringifyOffset = destringifyDeclarator.$(BinaryExpression > LiteralNumericExpression);
const findIndex = (c, d) => c - destringifyOffset.nodes[0].value;

$script(CallExpression[callee.name="destringify"]).replace(
node => {
return new Shift.LiteralStringExpression({
value: strings.get(findIndex(node.arguments[0].value)).value
})
}
)

$script([binding.name="a"]).delete();
$script([binding.name="destringify"]).delete();

$script.convertComputedToStatic();

console.log($script.print());
``

there were 2 issues:

  1. with .first() which i think is a bug
  2. convertComputedToStatic() method were moved to separate library

Thanks!

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

Successfully merging this pull request may close these issues.

2 participants