findRhinoOnlySyntax(source) scans script text for old Rhino-only patterns and returns [line, pattern, hint] rows: for each (, .getYear(), conditional catch (e if ...), and .toSource(). Use it to find what needs to change before you move an old script to the V8 runtime.
When to use
- Inheriting a script that was written years ago, before V8 was the default
- Making a list of lines to fix before asking Co-Pilot to modernize the code
- Scanning every file in a project with Get Script Project Files
How to use this snippet
The scan works line by line with simple patterns. It skips lines that start with //, but it can still flag a pattern inside a string or a block comment, so treat each row as a place to look, not a verdict. Line numbers are 1-based. Two of the four patterns (for each and conditional catch) are syntax errors in V8, so the file will not run at all until they are fixed. .toSource() is missing in V8 and fails when called. .getYear() still exists but is deprecated and returns the year minus 1900, so it is worth fixing even though it will not crash.
/**
* Scan script text for old Rhino-only patterns. Returns [line, pattern, hint]
* rows (line is 1-based). Checks: for each (, .getYear(), catch (e if ...),
* .toSource(). Line-based and approximate: it skips // comment lines but can
* still match inside strings or block comments. Returns [] when nothing is found.
*
* @param {string} source
* @return {Array<Array<*>>}
*/
function findRhinoOnlySyntax(source) {
var checks = [
{
name: 'for each (',
re: /\bfor\s+each\s*\(/,
hint: 'Syntax error in V8. Use for (var x of list) for values, or for (var k in obj) for keys.'
},
{
name: '.getYear()',
re: /\.getYear\s*\(\s*\)/,
hint: 'Deprecated; it can return the year minus 1900. Use getFullYear().'
},
{
name: 'catch (e if ...)',
re: /\bcatch\s*\(\s*[A-Za-z_$][\w$]*\s+if\b/,
hint: 'Conditional catch is a syntax error in V8. Use catch (e) and check the condition inside, rethrowing with throw e when it does not match.'
},
{
name: '.toSource()',
re: /\.toSource\s*\(\s*\)/,
hint: 'Not available in V8. Use JSON.stringify(value) for plain data.'
}
];
var lines = String(source == null ? '' : source).split(/\r\n|\n|\r/);
var rows = [];
for (var i = 0; i < lines.length; i++) {
if (/^\s*\/\//.test(lines[i])) continue;
for (var c = 0; c < checks.length; c++) {
if (checks[c].re.test(lines[i])) {
rows.push([i + 1, checks[c].name, checks[c].hint]);
}
}
}
return rows;
}
Example
function scanProjectForRhino() {
var files = getScriptProjectFiles(ScriptApp.getScriptId());
var out = [['file', 'line', 'pattern', 'hint']];
files.forEach(function (f) {
if (f.type !== 'SERVER_JS') return;
findRhinoOnlySyntax(f.source).forEach(function (row) {
out.push([f.name].concat(row));
});
});
var ss = SpreadsheetApp.getActive();
var tab = ss.getSheetByName('Rhino scan') || ss.insertSheet('Rhino scan');
tab.clearContents();
tab.getRange(1, 1, out.length, 4).setValues(out);
}
The example uses the Apps Script API through getScriptProjectFiles, so it needs the read-only scope described on that snippet's page.
Notes
- This is a read. It does not change the script.
- It does not find every difference between runtimes. A clean scan means none of these four patterns were found, not that the script is guaranteed to run unchanged on V8.
varis not on the list on purpose. It works in V8, and swapping it forletorconstis optional.
Happy Coding!
