Skip to content

Commit

Permalink
#57 : variable renaming only outside of strings, or in ES6 substituti…
Browse files Browse the repository at this point in the history
…on patterns ${..}
  • Loading branch information
Siorki committed Jan 30, 2017
1 parent 0e5e9af commit 694d92f
Show file tree
Hide file tree
Showing 4 changed files with 89 additions and 2 deletions.
1 change: 1 addition & 0 deletions TestCases/gitHub#57-templateLiteralOneVariable.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

37 changes: 35 additions & 2 deletions shapeShifter.js
Original file line number Diff line number Diff line change
Expand Up @@ -1569,8 +1569,41 @@ ShapeShifter.prototype = {
for (var i=0; i<formerVariableCharList.length; ++i)
{
var oldVarName = formerVariableCharList[i];
var exp = new RegExp("(^|[^\\w\\d$_])"+(oldVarName=="$"?"\\":"")+oldVarName,"g");
output = output.replace(exp, "$1"+availableCharList[i]);
var exp = new RegExp("(^|[^\\w\\d$_])"+(oldVarName=="$"?"\\":"")+oldVarName,"g");
// #57 : replace if not in string, or if inside a substitution pattern in a `template literal`

var stringIndex = 0;
var variableMatch = exp.exec(output);
while (variableMatch && variableMatch[0] != "") {
var offset = variableMatch.index+variableMatch[0].indexOf(oldVarName);
while (stringIndex < inputData.containedStrings.length && inputData.containedStrings[stringIndex].end < offset) {
++stringIndex;
}
var doReplace = true;
if (stringIndex < inputData.containedStrings.length
&& inputData.containedStrings[stringIndex].begin < offset
&& offset < inputData.containedStrings[stringIndex].end) {
// the match is inside a string
doReplace = false;
// browse the string to find an ES6 substitution pattern : ${ ... }
for (var index=inputData.containedStrings[stringIndex].begin; index<offset; ++index) {
if (output.charCodeAt(index-1)!=92 && output.charCodeAt(index)==36 && output.charCodeAt(index+1)==123) {
// ${ not preceded by \ : beginning of a substitution pattern
doReplace = true;
}
if (output.charCodeAt(index-1)!=92 && output.charCodeAt(index)==125) {
// } not preceded by \ : end of a substitution pattern
doReplace = false;
}
}
}
if (doReplace) {
output = output.substr(0, offset) + availableCharList[i] + output.substr(offset+1);
}

variableMatch = exp.exec(output);
}

// Perform the replacement on the code appended by refactorToSetInterval()
inputData.interpreterCall = inputData.interpreterCall.replace(exp, "$1"+availableCharList[i]);
inputData.wrappedInit = inputData.wrappedInit.replace(exp, "$1"+availableCharList[i]);
Expand Down
2 changes: 2 additions & 0 deletions tests/allTests.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ var testIssue0045_closingBracket = require("./testIssue0045_closingBracket");
var testIssue0047_EscapeInCharClass = require("./testIssue0047_EscapeInCharClass");
var testIssue0055_stringDelimiters = require("./testIssue0055_stringDelimiters");
var testIssue0056_setIntervalDefaultParams = require("./testIssue0056_setIntervalDefaultParams");
var testIssue0057_replacementInString = require("./testIssue0057_replacementInString");
var testIssue0058_numberAsLoopVariable = require("./testIssue0058_numberAsLoopVariable");
var testIssue0063_backtickFunctionParam = require("./testIssue0063_backtickFunctionParam");
//var testIssue0050_unicodeSurrogate = require("./testIssue0050_unicodeSurrogate");
Expand All @@ -37,6 +38,7 @@ testIssue0045_closingBracket();
testIssue0047_EscapeInCharClass();
testIssue0055_stringDelimiters();
testIssue0056_setIntervalDefaultParams();
testIssue0057_replacementInString();
testIssue0058_numberAsLoopVariable();
testIssue0063_backtickFunctionParam();
//testIssue0050_unicodeSurrogate();
51 changes: 51 additions & 0 deletions tests/testIssue0057_replacementInString.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
var RegPack = require("../regPack")
var fs = require("fs");
var assert = require("assert");


function runTests() {
console.log("Issue #0057 - Renaming of variable $ in string : start");
testLettersOnly();
console.log("Issue #0057 - Renaming of variable $ in string : done");
}


/**
* Github issue #57 - collisiion between ES6 string substitution expression ${...} and variable $
* RegPack incorrectly replaces the $ in ${i}
*
* Associated test file : gitHub#57-templateLiteralOneVariable.js
*/
function testLettersOnly() {
var input = fs.readFileSync("../TestCases/gitHub#57-templateLiteralOneVariable.js", { encoding:"utf8"});
var options = {
withMath : false,
hash2DContext : true,
hashWebGLContext : true,
hashAudioContext : true,
contextVariableName : false,
contextType : parseInt(0),
reassignVars : true,
varsNotReassigned : "",
crushGainFactor : parseFloat(1),
crushLengthFactor : parseFloat(0),
crushCopiesFactor : parseFloat(0),
crushTiebreakerFactor : parseInt(1),
wrapInSetInterval : false,
timeVariableName : "",
useES6 : true
};

var result = RegPack.packer.preprocessor.preprocess(input, options);

// Expected result : k is the most frequent loop index, and thus selected
var preprocessed = result[0].contents;
// $ in ${ is not replaced
assert.notEqual(preprocessed.indexOf("c.fillStyle=`hsl(256,25%,${"), -1);
// T in ${... T ...} is replaced
assert.equal(preprocessed.indexOf("c.fillStyle=`hsl(256,25%,${Math.max(0,100-T)"), -1);

}


module.exports = runTests;

0 comments on commit 694d92f

Please sign in to comment.