Error guides

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

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.

Have a whole project to migrate? Paste your .gs or upload a zip for a free V8 compatibility report — every occurrence of this pattern with file and line, plus an auto-rewritten project.
Related errors