[Pkg-javascript-commits] [less.js] 138/285: further simplifications to the parse context
Jonas Smedegaard
dr at jones.dk
Mon Oct 26 23:23:47 UTC 2015
This is an automated email from the git hooks/post-receive script.
js pushed a commit to annotated tag v2.0.0
in repository less.js.
commit 823ab221dd62edc767ef235d6a2f0c1d57b10c58
Author: Luke Page <luke.a.page at gmail.com>
Date: Mon Sep 22 21:24:48 2014 +0100
further simplifications to the parse context
---
lib/less-browser/index.js | 2 +-
lib/less/contexts.js | 36 ++++++-------------------------
lib/less/imports.js | 17 +++++++++++----
lib/less/parser/parser.js | 54 +++++++++++++++++++++++------------------------
lib/less/render.js | 20 ++++++++++++++++--
test/less-test.js | 18 +++++++++-------
6 files changed, 75 insertions(+), 72 deletions(-)
diff --git a/lib/less-browser/index.js b/lib/less-browser/index.js
index 6bfae4f..59d68bb 100644
--- a/lib/less-browser/index.js
+++ b/lib/less-browser/index.js
@@ -409,7 +409,7 @@ function loadStyleSheet(sheet, callback, reload, remaining, modifyVars) {
removeError(path);
if (data) {
- instanceOptions.currentFileInfo = newFileInfo;
+ instanceOptions.rootFileInfo = newFileInfo;
less.render(data, instanceOptions)
.then(function(result) {
callback(null, result.css, data, sheet, webInfo, path);
diff --git a/lib/less/contexts.js b/lib/less/contexts.js
index 4092296..467cfe6 100644
--- a/lib/less/contexts.js
+++ b/lib/less/contexts.js
@@ -12,11 +12,10 @@ var copyFromOriginal = function copyFromOriginal(original, destination, properti
};
/*
- parseEnv is 2 things
- 1. a set of options
- 2. The context of the current file information
+ parseEnv is used whilst parsing
*/
var parseCopyProperties = [
+ // options
'paths', // option - unmodified - paths to search for imports on
'relativeUrls', // option - whether to adjust URL's to be relative
'rootpath', // option - rootpath to append to URL's
@@ -24,43 +23,20 @@ var parseCopyProperties = [
'insecure', // option - whether to allow imports from insecure ssl hosts
'dumpLineNumbers', // option - whether to dump line numbers
'compress', // option - whether to compress
- 'processImports', // option - whether to process imports. if false then imports will not be imported
'syncImport', // option - whether to import synchronously
'chunkInput', // option - whether to chunk input. more performant but causes parse issues.
'mime', // browser only - mime type for sheet import
'useFileCache', // browser only - whether to use the per file session cache
- 'currentFileInfo' // information about the current file - for error reporting and importing and making urls relative etc.
+ // context
+ 'processImports', // option & context - whether to process imports. if false then imports will not be imported.
+ // Used by the import manager to stop multiple import visitors being created.
+ 'reference' // Used to indicate that the contents are imported by reference
];
-//currentFileInfo = {
-// 'relativeUrls' - option - whether to adjust URL's to be relative
-// 'filename' - full resolved filename of current file
-// 'rootpath' - path to append to normal URLs for this node
-// 'currentDirectory' - path to the current file, absolute
-// 'rootFilename' - filename of the base file
-// 'entryPath' - absolute path to the entry file
-// 'reference' - whether the file should not be output and only output parts that are referenced
-
contexts.parseEnv = function(options) {
copyFromOriginal(options, this, parseCopyProperties);
if (typeof this.paths === "string") { this.paths = [this.paths]; }
-
- if (!this.currentFileInfo) {
- var filename = (options && options.filename) || "input";
- var entryPath = filename.replace(/[^\/\\]*$/, "");
- if (options) {
- options.filename = null;
- }
- this.currentFileInfo = {
- filename: filename,
- relativeUrls: this.relativeUrls,
- rootpath: (options && options.rootpath) || "",
- currentDirectory: entryPath,
- entryPath: entryPath,
- rootFilename: filename
- };
- }
};
var evalCopyProperties = [
diff --git a/lib/less/imports.js b/lib/less/imports.js
index d360964..e426c6d 100644
--- a/lib/less/imports.js
+++ b/lib/less/imports.js
@@ -2,8 +2,18 @@ var contexts = require("./contexts"),
Parser = require('./parser/parser');
module.exports = function(environment) {
- var ImportManager = function(env) {
- this.rootFilename = env && env.filename;
+
+ // FileInfo = {
+ // 'relativeUrls' - option - whether to adjust URL's to be relative
+ // 'filename' - full resolved filename of current file
+ // 'rootpath' - path to append to normal URLs for this node
+ // 'currentDirectory' - path to the current file, absolute
+ // 'rootFilename' - filename of the base file
+ // 'entryPath' - absolute path to the entry file
+ // 'reference' - whether the file should not be output and only output parts that are referenced
+
+ var ImportManager = function(env, rootFileInfo) {
+ this.rootFilename = rootFileInfo.filename;
this.paths = env.paths || []; // Search paths, when importing
this.contents = {}; // map - filename to contents of all the files
this.contentsIgnoredChars = {}; // map - filename to lines at the begining of each file to ignore
@@ -67,7 +77,6 @@ module.exports = function(environment) {
var newEnv = new contexts.parseEnv(parserImports.env);
- newEnv.currentFileInfo = newFileInfo;
newEnv.processImports = false;
parserImports.contents[resolvedFilename] = contents;
@@ -78,7 +87,7 @@ module.exports = function(environment) {
if (importOptions.inline) {
fileParsedFunc(null, contents, resolvedFilename);
} else {
- new(Parser)(newEnv, parserImports).parse(contents, function (e, root) {
+ new Parser(newEnv, parserImports, newFileInfo).parse(contents, function (e, root) {
fileParsedFunc(e, root, resolvedFilename);
});
}
diff --git a/lib/less/parser/parser.js b/lib/less/parser/parser.js
index 1f77521..85792d3 100644
--- a/lib/less/parser/parser.js
+++ b/lib/less/parser/parser.js
@@ -37,7 +37,7 @@ var LessError = require('../less-error'),
// It also takes care of moving all the indices forwards.
//
//
-var Parser = function Parser(env, imports) {
+var Parser = function Parser(env, imports, fileInfo) {
var parsers,
parserInput = getParserInput();
@@ -63,7 +63,7 @@ var Parser = function Parser(env, imports) {
throw new LessError(
{
index: parserInput.i,
- filename: env.currentFileInfo.filename,
+ filename: fileInfo.filename,
type: type || 'Syntax',
message: msg
},
@@ -72,7 +72,7 @@ var Parser = function Parser(env, imports) {
}
function getDebugInfo(index) {
- var filename = env.currentFileInfo.filename;
+ var filename = fileInfo.filename;
filename = imports.getAbsolutePath(filename);
return {
@@ -100,13 +100,13 @@ var Parser = function Parser(env, imports) {
if (globalVars || (additionalData && additionalData.banner)) {
preText = ((additionalData && additionalData.banner) ? additionalData.banner : "") + globalVars;
- imports.contentsIgnoredChars[env.currentFileInfo.filename] = preText.length;
+ imports.contentsIgnoredChars[fileInfo.filename] = preText.length;
}
str = str.replace(/\r\n/g, '\n');
// Remove potential UTF Byte Order Mark
str = preText + str.replace(/^\uFEFF/, '') + modifyVars;
- imports.contents[env.currentFileInfo.filename] = str;
+ imports.contents[fileInfo.filename] = str;
// Start with the primary rule.
// The whole syntax tree is held under a Ruleset node,
@@ -118,7 +118,7 @@ var Parser = function Parser(env, imports) {
index: index,
type: 'Parse',
message: msg,
- filename: env.currentFileInfo.filename
+ filename: fileInfo.filename
}, imports);
});
@@ -126,7 +126,7 @@ var Parser = function Parser(env, imports) {
root.root = true;
root.firstRoot = true;
} catch (e) {
- return callback(new LessError(e, imports, env.currentFileInfo.filename));
+ return callback(new LessError(e, imports, fileInfo.filename));
}
// If `i` is smaller than the `input.length - 1`,
@@ -157,7 +157,7 @@ var Parser = function Parser(env, imports) {
type: "Parse",
message: message,
index: endInfo.furthest,
- filename: env.currentFileInfo.filename
+ filename: fileInfo.filename
}, imports);
}
@@ -166,7 +166,7 @@ var Parser = function Parser(env, imports) {
if (e) {
if (!(e instanceof LessError)) {
- e = new LessError(e, imports, env.currentFileInfo.filename);
+ e = new LessError(e, imports, fileInfo.filename);
}
return callback(e);
@@ -261,7 +261,7 @@ var Parser = function Parser(env, imports) {
comment: function () {
if (parserInput.commentStore.length) {
var comment = parserInput.commentStore.shift();
- return new(tree.Comment)(comment.text, comment.isLineComment, comment.index, env.currentFileInfo);
+ return new(tree.Comment)(comment.text, comment.isLineComment, comment.index, fileInfo);
}
},
@@ -279,7 +279,7 @@ var Parser = function Parser(env, imports) {
str = parserInput.$re(/^(~)?("((?:[^"\\\r\n]|\\.)*)"|'((?:[^'\\\r\n]|\\.)*)')/);
if (str) {
- return new(tree.Quoted)(str[2], str[3] || str[4], Boolean(str[1]), index, env.currentFileInfo);
+ return new(tree.Quoted)(str[2], str[3] || str[4], Boolean(str[1]), index, fileInfo);
}
},
@@ -335,7 +335,7 @@ var Parser = function Parser(env, imports) {
}
parserInput.forget();
- return new(tree.Call)(name, args, index, env.currentFileInfo);
+ return new(tree.Call)(name, args, index, fileInfo);
},
arguments: function () {
var args = [], arg;
@@ -404,7 +404,7 @@ var Parser = function Parser(env, imports) {
expectChar(')');
return new(tree.URL)((value.value != null || value instanceof tree.Variable) ?
- value : new(tree.Anonymous)(value), index, env.currentFileInfo);
+ value : new(tree.Anonymous)(value), index, fileInfo);
},
//
@@ -419,7 +419,7 @@ var Parser = function Parser(env, imports) {
var name, index = parserInput.i;
if (parserInput.currentChar() === '@' && (name = parserInput.$re(/^@@?[\w-]+/))) {
- return new(tree.Variable)(name, index, env.currentFileInfo);
+ return new(tree.Variable)(name, index, fileInfo);
}
},
@@ -428,7 +428,7 @@ var Parser = function Parser(env, imports) {
var curly, index = parserInput.i;
if (parserInput.currentChar() === '@' && (curly = parserInput.$re(/^@\{([\w-]+)\}/))) {
- return new(tree.Variable)("@" + curly[1], index, env.currentFileInfo);
+ return new(tree.Variable)("@" + curly[1], index, fileInfo);
}
},
@@ -492,7 +492,7 @@ var Parser = function Parser(env, imports) {
js = parserInput.$re(/^(~)?`([^`]*)`/);
if (js) {
- return new(tree.JavaScript)(js[2], Boolean(js[1]), index, env.currentFileInfo);
+ return new(tree.JavaScript)(js[2], Boolean(js[1]), index, fileInfo);
}
}
},
@@ -591,7 +591,7 @@ var Parser = function Parser(env, imports) {
if (!e) {
break;
}
- elem = new(tree.Element)(c, e, elemIndex, env.currentFileInfo);
+ elem = new(tree.Element)(c, e, elemIndex, fileInfo);
if (elements) { elements.push(elem); } else { elements = [ elem ]; }
c = parserInput.$char('>');
}
@@ -608,7 +608,7 @@ var Parser = function Parser(env, imports) {
if (parsers.end()) {
parserInput.forget();
- return new(tree.mixin.Call)(elements, args, index, env.currentFileInfo, important);
+ return new(tree.mixin.Call)(elements, args, index, fileInfo, important);
}
}
@@ -868,7 +868,7 @@ var Parser = function Parser(env, imports) {
}
}
- if (e) { return new(tree.Element)(c, e, index, env.currentFileInfo); }
+ if (e) { return new(tree.Element)(c, e, index, fileInfo); }
},
//
@@ -943,7 +943,7 @@ var Parser = function Parser(env, imports) {
}
}
- if (elements) { return new(tree.Selector)(elements, extendList, condition, index, env.currentFileInfo); }
+ if (elements) { return new(tree.Selector)(elements, extendList, condition, index, fileInfo); }
if (extendList) { error("Extend must be used to extend a selector, it cannot be used on its own"); }
},
attribute: function () {
@@ -1067,7 +1067,7 @@ var Parser = function Parser(env, imports) {
if (value) {
parserInput.forget();
// anonymous values absorb the end ';' which is reequired for them to work
- return new (tree.Rule)(name, value, false, merge, startOfRule, env.currentFileInfo);
+ return new (tree.Rule)(name, value, false, merge, startOfRule, fileInfo);
}
}
if (!tryValueFirst && !value) {
@@ -1079,7 +1079,7 @@ var Parser = function Parser(env, imports) {
if (value && this.end()) {
parserInput.forget();
- return new (tree.Rule)(name, value, important, merge, startOfRule, env.currentFileInfo);
+ return new (tree.Rule)(name, value, important, merge, startOfRule, fileInfo);
} else {
parserInput.restore();
if (value && !tryAnonymous) {
@@ -1123,7 +1123,7 @@ var Parser = function Parser(env, imports) {
error("missing semi-colon or unrecognised media features on import");
}
features = features && new(tree.Value)(features);
- return new(tree.Import)(path, features, options, index, env.currentFileInfo);
+ return new(tree.Import)(path, features, options, index, fileInfo);
}
else
{
@@ -1180,7 +1180,7 @@ var Parser = function Parser(env, imports) {
e = this.value();
if (parserInput.$char(')')) {
if (p && e) {
- nodes.push(new(tree.Paren)(new(tree.Rule)(p, e, null, null, parserInput.i, env.currentFileInfo, true)));
+ nodes.push(new(tree.Paren)(new(tree.Rule)(p, e, null, null, parserInput.i, fileInfo, true)));
} else if (e) {
nodes.push(new(tree.Paren)(e));
} else {
@@ -1231,7 +1231,7 @@ var Parser = function Parser(env, imports) {
rules = this.block();
if (rules) {
- media = new(tree.Media)(rules, features, parserInput.i, env.currentFileInfo);
+ media = new(tree.Media)(rules, features, parserInput.i, fileInfo);
if (env.dumpLineNumbers) {
media.debugInfo = debugInfo;
}
@@ -1334,7 +1334,7 @@ var Parser = function Parser(env, imports) {
if (rules || (!hasBlock && value && parserInput.$char(';'))) {
parserInput.forget();
- return new(tree.Directive)(name, value, rules, index, env.currentFileInfo,
+ return new(tree.Directive)(name, value, rules, index, fileInfo,
env.dumpLineNumbers ? getDebugInfo(index) : null);
}
@@ -1572,7 +1572,7 @@ var Parser = function Parser(env, imports) {
name[k] = (s.charAt(0) !== '@') ?
new(tree.Keyword)(s) :
new(tree.Variable)('@' + s.slice(2, -1),
- index[k], env.currentFileInfo);
+ index[k], fileInfo);
}
return name;
}
diff --git a/lib/less/render.js b/lib/less/render.js
index 5be9f5b..50d8899 100644
--- a/lib/less/render.js
+++ b/lib/less/render.js
@@ -21,9 +21,25 @@ module.exports = function(environment, ParseTree, ImportManager) {
});
} else {
var env = new contexts.parseEnv(options),
- imports = new ImportManager(env);
+ rootFileInfo;
- var parser = new(Parser)(env, imports);
+ if (options.rootFileInfo) {
+ rootFileInfo = options.rootFileInfo;
+ } else {
+ var filename = options.filename || "input";
+ var entryPath = filename.replace(/[^\/\\]*$/, "");
+ rootFileInfo = {
+ filename: filename,
+ relativeUrls: env.relativeUrls,
+ rootpath: env.rootpath || "",
+ currentDirectory: entryPath,
+ entryPath: entryPath,
+ rootFilename: filename
+ };
+ }
+
+ var imports = new ImportManager(env, rootFileInfo);
+ var parser = new Parser(env, imports, rootFileInfo);
return new PromiseConstructor(function (resolve, reject) {
parser.parse(input, function (e, root) {
diff --git a/test/less-test.js b/test/less-test.js
index 8ea848a..b4aa02a 100644
--- a/test/less-test.js
+++ b/test/less-test.js
@@ -124,7 +124,15 @@ module.exports = function() {
toCSS(options, path.join('test/less/', foldername + file), function (err, result) {
if (verifyFunction) {
- return verifyFunction(name, err, result.css, doReplacements, result.map);
+ return verifyFunction(name, err, result && result.css, doReplacements, result && result.map);
+ }
+ if (err) {
+ fail("ERROR: " + (err && err.message));
+ if (isVerbose) {
+ process.stdout.write("\n");
+ process.stdout.write(err.stack + "\n");
+ }
+ return;
}
var css_name = name;
if(nameModifier) { css_name = nameModifier(name); }
@@ -133,13 +141,7 @@ module.exports = function() {
css = css && doReplacements(css, 'test/less/' + foldername);
if (result.css === css) { ok('OK'); }
- else if (err) {
- fail("ERROR: " + (err && err.message));
- if (isVerbose) {
- process.stdout.write("\n");
- process.stdout.write(err.stack + "\n");
- }
- } else {
+ else {
difference("FAIL", css, result.css);
}
});
--
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-javascript/less.js.git
More information about the Pkg-javascript-commits
mailing list