Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Variable merging #28

Open
7ijme opened this issue Jul 17, 2024 · 4 comments
Open

Variable merging #28

7ijme opened this issue Jul 17, 2024 · 4 comments

Comments

@7ijme
Copy link

7ijme commented Jul 17, 2024

Because we are using bad practices anyway, when you have two variables with the same name, but different capitalization, there is a chance of the variables merging.

input:

const message = "hello there"
const Message = "hello there"

output:

const MESSAGE = "hello there"
const MESSAGE = "hello there"

The chance of this happening is slim, depending on the length of the variable: $\frac{1}{2^n}$.

@HelloWorld-n
Copy link

HelloWorld-n commented Jul 24, 2024

Not yet fully safe but:


adding file src/utils/cst-formatter/insertion.ts

interface Imap {
  [key: string]: string;
}

const requiredTokenTypes = [
  'VariableDeclarator',
  'FunctionDeclaration',
  'ClassDeclaration',
];
const map: Imap = Object.create(null);



let newStrs: Array<String> = [];

function insertChars(ast: any) {
  ast.selectTokensByType('Identifier').forEach((token: any) => {
    if (token.parentElement.parentElement.type === 'FunctionDeclaration') {
      if (!map[token.value]) {
        const update = insertVariableValidCharRandomly(token.value);
        map[token.value] = update;
      }
    }
  });

  ast.selectTokensByType('Identifier').forEach((token: any) => {
    try {
      if (requiredTokenTypes.includes(token.parentElement.parentElement.type)) {
        if (!map[token.value]) {
          let update = insertVariableValidCharRandomly(token.value);
          while (newStrs.includes(update)){
            update = insertVariableValidCharRandomly(token.value);
          }
          newStrs[newStrs.length] = update;
          map[token.value] = update;
        }
        const val = map[token.value];
        token.value = val;
        token._sourceCode = val;
        token._sourceCodeLines = [val];
      } else {
        const val = map[token.value] || token.value;
        token.value = val;
        token._sourceCode = val;
        token._sourceCodeLines = [val];
      }
    } catch (err) {
      // TODO: handle token errors
      // console.log(err);
    }
  });
}

function getRandomCharFromStr(str: string): string {
  return str[Math.floor(Math.random() * str.length)];
}

function insertVariableValidCharRandomly(str: string) {
  const charsValidEverywhere = '$ABCDEFGHIJKLMNOPQRSTUVWXYZ_';
  const charsValidConditional = '0123456789';
  const randomInsertionChances = 0.1;

  let modifiedStr = '';
  for (let i = 0; i <= str.length; i++) {
    if (Math.random() < randomInsertionChances) {
      if (i === 0) {
        modifiedStr += getRandomCharFromStr(charsValidEverywhere);
      } else {
        modifiedStr += getRandomCharFromStr(charsValidEverywhere + charsValidConditional);
      }
    }
    if (i < str.length) {
      modifiedStr += str[i];
    }
  }
  
  return modifiedStr;
}


export { insertChars };

then modifying file /src/index.ts by adding

import { insertChars } from './utils/cst-formatter/insertion';

in front of

import { randomizeCase } from './utils/cst-formatter/case';

and by adding

  insertChars(cst);

before

randomizeCase(cst);


would reduce chances for overlap to approx 1 / (  (2n) + (0.1 * 54n * 44)  )


Possibly causes overlap of variables that aren't even same size such as vrb and Varb (fixed as of 2024-10-31; check comment edits for quicker glance)

Possibly causes fnCtion to be renamed to function

Possibly causes LT to be renamed to let

Possibly causes clAss to be renamed to class

Similar problems to those may occur.

@jxmked
Copy link

jxmked commented Oct 11, 2024

The code will be uglified but will not work due to multiple declaration of the namespace and variable names. Just don't use it if you want your code to not to work.

@HelloWorld-n
Copy link

It turns copy-pasted wrong line of code; now is fixed.

@HelloWorld-n
Copy link

HelloWorld-n commented Oct 31, 2024

New information


By adding

let newStrs: Array<String> = [];

in front of

const map: Imap = Object.create(null);

and by changing

const update = changeCaseRandomly(token.value);

with

          let update = changeCaseRandomly(token.value);
          while (newStrs.includes(update)){
            update = changeCaseRandomly(token.value);
          }
          newStrs[newStrs.length] = update;

solves problem completly regarding new words

Adding

const problematicWords = [
  // keywords
  // // declarations
  "class", "function", "let", "var", "const", "new", "enum", "static",

  // // flow
  "if", "else", "while", "for", "return", "yield", "async", "await",
  "do", "break", "continue", "try", "catch", "finally", "throws", "throw",
  "goto", "switch", "case", "default",

  // // values
  "super", "this", "false", "true", "null", "undefined",

  // types
  "boolean", "Boolean", "char", "Char", "number", "Number", "string", "String",
  "integer", "float", "bigint", "BigInt", 
    
  // // misc
  "abstract", "delete", "do", "export", "import", "extends", 

  // builtins
  "console", "Math", "document", "window",
]

export { problematicWords };

in new file src/utils/cst-formatter/js-problematic-words.ts

and

import { problematicWords } from "./js-problematic-words";
for (let word of problematicWords){
    newStrs[newStrs.length] = word;
}

in after every

let newStrs: Array<String> = [];

fixes it even for cases of many existing words

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

No branches or pull requests

3 participants