Open 7ijme opened 1 month ago
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);
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]) {
const update = insertVariableValidCharRandomly(token.value);
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 { randomizeCase } from './utils/cst-formatter/case';
in front of https://github.com/rohitdhas/shittier/blob/5a01a098d1dec9986ce421766a327a9de3a142e4/src/index.ts#L5 and by adding
insertChars(cst);
before https://github.com/rohitdhas/shittier/blob/5a01a098d1dec9986ce421766a327a9de3a142e4/src/index.ts#L14
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
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.
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:
output:
The chance of this happening is slim, depending on the length of the variable: $\frac{1}{2^n}$.