Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

A backwards - compatible change to embed_tokens placing NodeWithTokens in .loc #201

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 17 additions & 2 deletions lib/parse-js.js
Original file line number Diff line number Diff line change
Expand Up @@ -663,6 +663,10 @@ function NodeWithToken(str, start, end) {

NodeWithToken.prototype.toString = function() { return this.name; };

// embed_tokens === true: first element is NodeWithToken,
// === ".loc": array .loc property set to NodeWithToken,
// else first element is string name of node.

function parse($TEXT, exigent_mode, embed_tokens) {

var S = {
Expand Down Expand Up @@ -750,14 +754,25 @@ function parse($TEXT, exigent_mode, embed_tokens) {
function add_tokens(str, start, end) {
return str instanceof NodeWithToken ? str : new NodeWithToken(str, start, end);
};

function append_tokens(ast, start, end) {
ast.loc = ast.loc || new NodeWithToken(ast[0], start, end);
return ast;
};

function maybe_embed_tokens(parser) {
if (embed_tokens) return function() {
if (embed_tokens === true) return function() {
var start = S.token;
var ast = parser.apply(this, arguments);
ast[0] = add_tokens(ast[0], start, prev());
return ast;
};
else if (embed_tokens === ".loc") return function() {
var start = S.token;
var ast = parser.apply(this, arguments);
ast = append_tokens(ast, start, prev());
return ast;
};
else return parser;
};

Expand Down Expand Up @@ -1278,7 +1293,7 @@ function array_to_hash(a) {
};

function slice(a, start) {
return Array.prototype.slice.call(a, start == null ? 0 : start);
return Array.prototype.slice.call(a, start || 0);
};

function characters(str) {
Expand Down