Skip to content

Commit

Permalink
✨ Also make destringify/depropertize optimize property chains
Browse files Browse the repository at this point in the history
  • Loading branch information
skerit committed Feb 16, 2024
1 parent 57dfb88 commit 1fbabd5
Showing 1 changed file with 89 additions and 22 deletions.
111 changes: 89 additions & 22 deletions lib/server_functions.js
Original file line number Diff line number Diff line change
Expand Up @@ -433,13 +433,14 @@ module.exports = function serverFunctions(Blast, extras) {
if (options.depropertize) {
tokens = Blast.depropertizeCode(tokens, {
properties: options.depropertize,
return_tokens: true
return_tokens: true,
});
}

if (options.destringify) {
tokens = Blast.destringifyCode(tokens, {
return_tokens: true
return_tokens : true,
depropertize : options.depropertize,
});
}

Expand Down Expand Up @@ -1319,9 +1320,16 @@ module.exports = function serverFunctions(Blast, extras) {

let symbol_name_indexes = [],
remove_symbol_names = options.remove_symbol_names ?? true,
previous_type,
current_type,
depropertize = !!options.depropertize,
property_map = new Map(),
string_map = new Map(),
usage_map = new Map(),
first_char,
next_type,
last_char,
is_string,
new_code = 'const ',
replaced = 0,
new_name,
Expand All @@ -1330,15 +1338,15 @@ module.exports = function serverFunctions(Blast, extras) {
index,
token,
next,
type,
i;

for (i = 0; i < tokens.length; i++) {
token = tokens[i];
first_char = token[0];
is_string = false;

if (first_char != '"' && first_char != "'") {
continue;
if (first_char == '"' || first_char == "'") {
is_string = true;
}

// The replaced variable names will be 3 characters long + 1 comma
Expand All @@ -1349,30 +1357,44 @@ module.exports = function serverFunctions(Blast, extras) {
last_char = token[token.length - 1];

if (last_char != first_char) {
is_string = false;
}

if (is_string) {
current_type = 'string';
} else if (!depropertize) {
continue;
} else {
current_type = Fn.getTokenType(token);
}

previous = tokens[i - 1];
type = Fn.getTokenType(previous);
previous_type = Fn.getTokenType(previous);

if (type == 'name') {
if (previous_type == 'name') {
continue;
}

if (type == 'whitespace') {
if (previous_type == 'whitespace') {
previous = tokens[i - 2];
type = Fn.getTokenType(previous);
previous_type = Fn.getTokenType(previous);
}

if (!is_string) {
if (previous != '.' && previous != '?.') {
continue;
}
}

next = tokens[i + 1];
type = Fn.getTokenType(next);
next_type = Fn.getTokenType(next);

if (type == 'whitespace') {
if (next_type == 'whitespace') {
next = tokens[i + 2];
type = Fn.getTokenType(next);
next_type = Fn.getTokenType(next);
}

if (type == 'name') {
if (next_type == 'name') {
continue;
}

Expand All @@ -1395,23 +1417,48 @@ module.exports = function serverFunctions(Blast, extras) {
}
}

if (first_char == '"' && !token.includes("'")) {
if (!is_string) {
token = "'" + token + "'";
} else if (first_char == '"' && !token.includes("'")) {
token = "'" + token.slice(1, -1) + "'";
}

indexes = string_map.get(token);

// Always add at least an empty array of indexes to the string map
if (!indexes) {
indexes = [];
string_map.set(token, indexes);
}

if (is_string) {
indexes.push(i);
} else {
indexes = property_map.get(token);

if (!indexes) {
indexes = [];
property_map.set(token, indexes);
}

indexes.push(i);
}

indexes = usage_map.get(token);

if (!indexes) {
indexes = [];
usage_map.set(token, indexes);
}

indexes.push(i);
}

let sorted = [];

for (let [string, indexes] of string_map) {
for (let [string, string_indexes] of string_map) {

indexes = usage_map.get(string);

// Ignore strings that are only used once
if (indexes.length < 2) {
Expand All @@ -1423,9 +1470,10 @@ module.exports = function serverFunctions(Blast, extras) {
}

sorted.push({
string : string,
indexes : indexes,
count : indexes.length,
string : string,
string_indexes : string_indexes,
property_indexes : property_map.get(string),
count : indexes.length,
});
}

Expand All @@ -1435,7 +1483,11 @@ module.exports = function serverFunctions(Blast, extras) {
return b.count - a.count;
});

for (let {string, indexes} of sorted) {
// Re-use the string map for mapping
string_map = new Map();
options.string_map = string_map;

for (let {string, string_indexes, property_indexes} of sorted) {

if (replaced) {
new_code += '\n, ';
Expand All @@ -1446,9 +1498,24 @@ module.exports = function serverFunctions(Blast, extras) {

new_code += new_name + ' = ' + string + '';

for (i = 0; i < indexes.length; i++) {
index = indexes[i];
tokens[indexes[i]] = new_name;
string_map.set(string, new_name);

for (i = 0; i < string_indexes.length; i++) {
index = string_indexes[i];
tokens[index] = new_name;
}

if (!property_indexes?.length) {
continue;
}

for (i = 0; i < property_indexes.length; i++) {
index = property_indexes[i];
tokens[index] = '[' + new_name + ']';

if (tokens[index - 1] === '.') {
tokens[index - 1] = '';
}
}
}

Expand Down

0 comments on commit 1fbabd5

Please sign in to comment.