Skip to content

Commit 38537d7

Browse files
committed
Fix pretokeniser's parsing of templated strings after recent changes
1 parent 2df7b0d commit 38537d7

File tree

1 file changed

+11
-11
lines changed

1 file changed

+11
-11
lines changed

plugins/pretokenise.js

+11-11
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131

3232
// When code is sent to Espruino, search it for modules and add extra code required to load them
3333
Espruino.addProcessor("transformForEspruino", function(code, callback) {
34-
if (!Espruino.Config.PRETOKENISE) return callback(code);
34+
if (Espruino.Config.PRETOKENISE == 0) return callback(code); // disabled?
3535
if (Espruino.Config.SAVE_ON_SEND == 0) {
3636
console.log("pretokenise> Can't pretokenise code sent to REPL (RAM)");
3737
return callback(code);
@@ -40,7 +40,7 @@
4040
});
4141
// When code is sent to Espruino, search it for modules and add extra code required to load them
4242
Espruino.addProcessor("transformModuleForEspruino", function(module, callback) {
43-
if (!Espruino.Config.PRETOKENISE ||
43+
if (Espruino.Config.PRETOKENISE == 0 || // disabled?
4444
Espruino.Config.MODULE_AS_FUNCTION) return callback(module);
4545
/* if MODULE_AS_FUNCTION is specified the module is uploaded inside a 'function'
4646
block, in which case it will be pretokenised anyway in a later step */
@@ -135,7 +135,7 @@
135135
if (Espruino.Config.PRETOKENISE==2) { // force all options always
136136
pretokeniseStrings = true;
137137
pretokeniseInts = true;
138-
} else if (boardData && boardData.VERSION) {
138+
} else if (boardData && boardData.VERSION) { // Espruino.Config.PRETOKENISE is nonzero or we wouldn't be called
139139
var v = parseFloat(boardData.VERSION.replace("v","0"));
140140
if (v >= 2020.48)
141141
pretokeniseStrings = true;
@@ -147,7 +147,7 @@
147147
let t = acorn.tokenizer(code, { ecmaVersion : 2020 });
148148
return { next : function() {
149149
let tk = t.getToken();
150-
let tkStr = code.substring(tk.start, tk.end), tkValue = tk.value;
150+
let tkStr = code.substring(tk.start, tk.end), tkValue = tk.value, tkEnd = tk.end;
151151
if (tk.type.label=="eof") return undefined;
152152
let tp = "?";
153153
if (tk.type.label=="`") { // template string
@@ -158,17 +158,17 @@
158158
if (tk2.type.label=="${")
159159
hasTemplate = true;
160160
} while (tk2.type.label!="`");
161-
tkStr = code.substring(tk.start, tk2.end);
161+
tkEnd = tk2.end;
162+
tkStr = code.substring(tk.start, tkEnd);
162163
tp = hasTemplate ? "TEMPLATEDSTRING" : "STRING"; // if we don't have any templates, treat as a normal string (https://github.com/espruino/Espruino/issues/2577)
163164
tkValue = hasTemplate ? tkStr : eval(tkStr); // don't evaluate if it has templates as it must be done at runtime!
164-
}
165-
if (tk.type.label=="string") tp="STRING";
166-
if (tk.type.label=="num") tp="NUMBER";
167-
if (tk.type.keyword || tk.type.label=="name") tp="ID";
168-
if (tp=="?" && tk.start+1==tk.end) tp="CHAR";
165+
} else if (tk.type.label=="string") tp="STRING";
166+
else if (tk.type.label=="num") tp="NUMBER";
167+
else if (tk.type.keyword || tk.type.label=="name") tp="ID";
168+
else if (tp=="?" && tk.start+1==tk.end) tp="CHAR";
169169
return {
170170
startIdx : tk.start,
171-
endIdx : tk.end,
171+
endIdx : tkEnd,
172172
str : tkStr,
173173
value : tkValue,
174174
type : tp

0 commit comments

Comments
 (0)