Fix "Unexpected token 'class'" and "Unexpected reserved word"
SyntaxError: Unexpected token 'class'
SyntaxError: Unexpected token 'import'
SyntaxError: Unexpected reserved word (enum)
Why it happens
ECMAScript reserves words like class, import, export, enum, extends and super. Rhino allowed many of them as variable and function names; V8 does not. One function import() {} makes the whole file fail to load.
Object keys are fine in every runtime: { class: 'x' } and obj.class still work.
The fix
Rename the variable or function — and every place that refers to it by name as a string:
// Rhino
function onOpen() {
SpreadsheetApp.getUi().createMenu('Sales').addItem('Import orders', 'import').addToUi();
}
function import() { … }
var class = obj.constructor.name;
// V8
function onOpen() {
SpreadsheetApp.getUi().createMenu('Sales').addItem('Import orders', 'import_').addToUi();
}
function import_() { … }
var class_ = obj.constructor.name;
Places that reference functions by name
Menu.addItem(caption, 'functionName')ScriptApp.newTrigger('functionName')— and existing installed triggers (re-create them under Triggers)google.script.run.functionName()in HTML files- Libraries: callers using
MyLib.import()
Strict-mode words
static, public, private, interface, package, implements, let and yield still work as names in V8's non-strict mode, but break if you add "use strict" or move code into a class. Worth renaming while you're there.
How V8Shift handles it
Declared reserved names and their uses are renamed to name_, including addItem and newTrigger string references. HTML callers and installed triggers are called out in the report.