-
Notifications
You must be signed in to change notification settings - Fork 28
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
Comments
Not yet fully safe but: adding file 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 import { insertChars } from './utils/cst-formatter/insertion'; in front of Line 5 in 5a01a09
and by adding insertChars(cst); before Line 14 in 5a01a09
would reduce chances for overlap to approx 1 / ( (2n) + (0.1 * 54n * 44) )
Possibly causes Possibly causes Possibly causes Similar problems to those may occur. |
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. |
It turns copy-pasted wrong line of code; now is fixed. |
New information By adding let newStrs: Array<String> = []; in front of shittier/src/utils/cst-formatter/case.ts Line 10 in 5a01a09
and by changing shittier/src/utils/cst-formatter/case.ts Line 26 in 5a01a09
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 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 |
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}$ .
The text was updated successfully, but these errors were encountered: