Files
firefox/devtools/shared/webconsole/parser-helper.js
T
Julian Descottes 0c7f6dc0d0 Bug 1568823 - Simplify parser.js and rename to parser-helper r=nchevobbe
Depends on D39335
Looking in details at get(), the implementation can also be simplified

Differential Revision: https://phabricator.services.mozilla.com/D39338
2019-07-25 13:21:03 +00:00

71 lines
2.2 KiB
JavaScript

/* -*- indent-tabs-mode: nil; js-indent-level: 2 -*- */
/* vim: set ft=javascript ts=2 et sw=2 tw=80: */
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
"use strict";
const DevToolsUtils = require("devtools/shared/DevToolsUtils");
loader.lazyRequireGetter(
this,
"Reflect",
"resource://gre/modules/reflect.jsm",
true
);
/**
* Gets a collection of parser methods for a specified source.
*
* @param string source
* The source text content.
* @param boolean logExceptions
*/
function getSyntaxTrees(source, logExceptions) {
// The source may not necessarily be JS, in which case we need to extract
// all the scripts. Fastest/easiest way is with a regular expression.
// Don't worry, the rules of using a <script> tag are really strict,
// this will work.
const regexp = /<script[^>]*?(?:>([^]*?)<\/script\s*>|\/>)/gim;
const syntaxTrees = [];
const scriptMatches = [];
let scriptMatch;
if (source.match(/^\s*</)) {
// First non whitespace character is &lt, so most definitely HTML.
while ((scriptMatch = regexp.exec(source))) {
// Contents are captured at index 1 or nothing: Self-closing scripts
// won't capture code content
scriptMatches.push(scriptMatch[1] || "");
}
}
// If there are no script matches, send the whole source directly to the
// reflection API to generate the AST nodes.
if (!scriptMatches.length) {
// Reflect.parse throws when encounters a syntax error.
try {
syntaxTrees.push(Reflect.parse(source));
} catch (e) {
if (logExceptions) {
DevToolsUtils.reportException("Parser:get", e);
}
}
} else {
// Generate the AST nodes for each script.
for (const script of scriptMatches) {
// Reflect.parse throws when encounters a syntax error.
try {
syntaxTrees.push(Reflect.parse(script));
} catch (e) {
if (logExceptions) {
DevToolsUtils.reportException("Parser:get", e);
}
}
}
}
return syntaxTrees;
}
exports.getSyntaxTrees = getSyntaxTrees;