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

добавил обработку директивы // @require file_path.js #2

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
.idea/
Empty file modified index.js
100644 → 100755
Empty file.
289 changes: 151 additions & 138 deletions lib/JossyParser.js
Original file line number Diff line number Diff line change
@@ -1,159 +1,170 @@
module.exports = JossyParser;
var FileStructure, JossyError, fileExists;

FileStructure = require('./FileStructure');
JossyError = require('./JossyError');
fileExists = require('fs').exists || require('path').exists;

function _include(file, params, callback) {
var paramsParts = params.split('::');
var includeFileName = paramsParts.shift();
if (includeFileName) {
var absFileName = file.getRelativePathOf(includeFileName);
this.parseFile(absFileName, function(err, includeFile) {
if (err) {
callback(err);
return;
}
file.addInclude(includeFile, paramsParts);
callback();
});
} else {
try {
file.addInclude(file, paramsParts);
callback();
} catch (err) {
callback(err);
}
}
}

var FileStructure = require('./FileStructure');
var JossyError = require('./JossyError');
function _without(file, params, callback) {
var paramsParts = params.split('::');
var includeFname = file.getRelativePathOf(paramsParts.shift());
this.parseFile(includeFname, function(err, includeFile) {
if (err) {
callback(err);
return;
}
file.addWithout(includeFile, paramsParts);
callback();
});
}

var fileExists = require('fs').exists || require('path').exists;
function _label(file, label) {
file.beginLabel(label);
}

function JossyParser() {
this._cache = {};
function _endlabel(file) {
file.endLabel();
}

JossyParser.prototype = {
parseFile: function(fname, callback) {
var that = this;
normalizePath(fname, function(err, fname) {
function _if(file, params) {
if (!params.trim()) {
throw new Error('Bad "if" directive');
}
var args = params.split(/\s+/);
var value = true;
if (args.length > 1 && args[0] == 'not') {
value = false;
args.shift();
}
file.beginIf(args[0], value);
}

function _endif(file) {
file.endIf();
}

function _set(file, params) {
if (!params.trim()) {
throw new Error('Bad set directive');
}
var args = params.split(/\s+/);
file.addSet(args[0]);
}

function _unset(file, params) {
if (!params.trim()) {
throw new Error('Bad unset directive');
}
var args = params.split(/\s+/);
file.addUnset(args[0]);
}

function parseFile(fname, callback) {
var that = this;
normalizePath(fname, function(err, fname) {
if (err) {
callback(err);
return;
}
if (that._cache[fname]) {
callback(null, that._cache[fname]);
return;
}
require('fs').readFile(fname, 'utf8', function(err, content) {
if (err) {
callback(err);
return;
}
if (that._cache[fname]) {
callback(null, that._cache[fname]);
return;
}
require('fs').readFile(fname, 'utf8', function(err, content) {
if (err) {
callback(err);
return;
}
var fileStructure = new FileStructure(fname);
that._cache[fname] = fileStructure;
var lines = content.split(/\r?\n/);
(function parseLines(start) {
var i;
var errors = [];

var appendError = function(err) {
var msg = err.message;
var line = i + 1;
errors.push(new JossyError(msg, fname, line));
fileStructure.error(msg);
};

var asyncParseCallback = function(err) {
if (err) {
appendError(err);
}
parseLines(i + 1);
};

for (i = start; i < lines.length; i++) {
var line = lines[i];
if (line.match(/^\s*\/\/#([\s\S]*)$/)) {
if (RegExp.$1) {
var command = RegExp.$1.split(' ');
var directive = command.shift();
var params = command.join(' ');
if (/^(include|without)$/.test(directive)) {
that['_' + directive](fileStructure, params, asyncParseCallback);
return;
} else if (/^(label|endlabel|if|endif|set|unset)$/.test(directive)) {
try {
that['_' + directive](fileStructure, params);
} catch (err) {
appendError(err);
}
} else {
appendError(new Error('Unknown directive ' + directive));
var fileStructure = new FileStructure(fname);
that._cache[fname] = fileStructure;
var lines = content.split(/\r?\n/);
(function parseLines(start) {
var i;
var errors = [];

var appendError = function(err) {
var msg = err.message;
var line = i + 1;
errors.push(new JossyError(msg, fname, line));
fileStructure.error(msg);
};

var asyncParseCallback = function(err) {
if (err) {
appendError(err);
}
parseLines(i + 1);
};

for (i = start; i < lines.length; i++) {
var line = lines[i];
if (line.match(/^\s*\/\/(#|\s*@)([\s\S]*)$/)) {
if (RegExp.$2) {
var command = RegExp.$2.split(' ');
var directive = command.shift();
var params = command.join(' ');
if (/^(require|include|without)$/.test(directive)) {
that['_' + directive](fileStructure, params, asyncParseCallback);
return;
} else if (/^(label|endlabel|if|endif|set|unset)$/.test(directive)) {
try {
that['_' + directive](fileStructure, params);
} catch (err) {
appendError(err);
}
} else if (RegExp.$1.indexOf('@') === -1) {
appendError(new Error('Unknown directive ' + directive));
}
} else {
fileStructure.addCode(line + (i < lines.length - 1 ? '\n' : ''));
}
} else {
fileStructure.addCode(line + (i < lines.length - 1 ? '\n' : ''));
}

callback(null, fileStructure);
})(0);
});
});
},

_include: function(file, params, callback) {
var paramsParts = params.split('::');
var includeFileName = paramsParts.shift();
if (includeFileName) {
var absFileName = file.getRelativePathOf(includeFileName);
this.parseFile(absFileName, function(err, includeFile) {
if (err) {
callback(err);
return;
}
file.addInclude(includeFile, paramsParts);
callback();
});
} else {
try {
file.addInclude(file, paramsParts);
callback();
} catch (err) {
callback(err);
}
}
},

_without: function(file, params, callback) {
var paramsParts = params.split('::');
var includeFname = file.getRelativePathOf(paramsParts.shift());
this.parseFile(includeFname, function(err, includeFile) {
if (err) {
callback(err);
return;
}
file.addWithout(includeFile, paramsParts);
callback();
callback(null, fileStructure);
})(0);
});
},

_label: function(file, label) {
file.beginLabel(label);
},

_endlabel: function(file) {
file.endLabel();
},

_if: function(file, params) {
if (!params.trim()) {
throw new Error('Bad "if" directive');
}
var args = params.split(/\s+/);
var value = true;
if (args.length > 1 && args[0] == 'not') {
value = false;
args.shift();
}
file.beginIf(args[0], value);
},

_endif: function(file) {
file.endIf();
},
});
}

_set: function(file, params) {
if (!params.trim()) {
throw new Error('Bad set directive');
}
var args = params.split(/\s+/);
file.addSet(args[0]);
},
function JossyParser() {
this._cache = {};
}

_unset: function(file, params) {
if (!params.trim()) {
throw new Error('Bad unset directive');
}
var args = params.split(/\s+/);
file.addUnset(args[0]);
}
JossyParser.prototype = {
parseFile: parseFile,

_include: _include,
_require: _include,
_without: _without,
_label: _label,
_endlabel: _endlabel,
_if: _if,
_endif: _endif,
_set: _set,
_unset: _unset
};

var realpathCache = {};
Expand Down Expand Up @@ -188,3 +199,5 @@ function normalizePath(fname, callback) {
});
}
}

module.exports = JossyParser;
12 changes: 6 additions & 6 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
{
"author": {
"name": "Kolyaj",
"email": "[email protected]",
"url": "https://github.com/Kolyaj"
},
"author": "Kolyaj <[email protected]> (https://github.com/Kolyaj)",
"contributors": [
"Kolyaj <[email protected]> (https://github.com/Kolyaj)",
"Pavel Akhmetchanov <[email protected]> (http://github.com/pavelpower)"
],
"name": "jossy",
"description": "JavaScript files builder",
"version": "0.0.2",
"version": "0.0.3",
"homepage": "https://github.com/Kolyaj/Jossy",
"repository": {
"type": "git",
Expand Down
4 changes: 4 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@ Jossy необходим только на этапе разработки, по

//#include file.js

Альтернативная декларация аналог `#include`:

// @require file.js

Путь к файлу указывается относительно расположения текущего файла. Технически, вместо строки с директивой просто вставляется содержимое указанного файла. Однако, если указанный файл уже подключен в текущем модуле ранее, то повторно он включен не будет. Например, файл f1.js

alert(1);
Expand Down
5 changes: 5 additions & 0 deletions tests/require/result.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
alert(1);

alert(2);

alert(3);
1 change: 1 addition & 0 deletions tests/require/test-1.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
alert(2);
6 changes: 6 additions & 0 deletions tests/require/test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
alert(1);

// @decl test-1.js
// @require test-1.js

alert(3);