From d3794db473cfb792ab1bb65c3973f942e52261d3 Mon Sep 17 00:00:00 2001 From: isqua Date: Sun, 27 Sep 2015 17:21:33 +0300 Subject: [PATCH 1/2] Core: Let define syntax for extensions --- src/core.js | 28 ++++++++++++++++++++++++---- 1 file changed, 24 insertions(+), 4 deletions(-) diff --git a/src/core.js b/src/core.js index 543d25f..f2f7d1c 100644 --- a/src/core.js +++ b/src/core.js @@ -21,6 +21,8 @@ class Comb { this.pluginsDependencies = {}; // List of supported syntaxes. this.supportedSyntaxes = new Set(); + // Mapping file extensions to syntax + this.syntaxMap = new Map(); // Whether verbose mode is on. this.verbose = false; } @@ -37,6 +39,12 @@ class Comb { return new minimatch.Minimatch(pattern); }); + if (config.syntax) { + for (let key in config.syntax) { + this.syntaxMap.set(key, config.syntax[key]); + } + } + for (let i = 0, l = this.plugins.length; i < l; i++) { let plugin = this.plugins[i]; let name = plugin.name; @@ -69,7 +77,7 @@ class Comb { * @returns {Promise} */ lintFile(path) { - let syntax = path.split('.').pop(); + let syntax = this._extractSyntax(path); return this._readFile(path).then((string) => { return this.lintString(string, {syntax: syntax, filename: path}); }); @@ -132,7 +140,7 @@ class Comb { if (!this._shouldProcessFile(path)) return; return vfs.read(path, 'utf8').then(function(data) { - let syntax = path.split('.').pop(); + let syntax = that._extractSyntax(path); that.processString(data, { syntax: syntax, filename: path @@ -375,7 +383,7 @@ class Comb { /** * Checks if specified path is not present in `exclude` list and it has one of - * acceptable extensions. + * acceptable syntaxes. * * @param {String} path * @returns {Boolean} False if the path either has unacceptable extension or @@ -383,7 +391,7 @@ class Comb { */ _shouldProcessFile(path) { // Get file's extension: - var syntax = path.split('.').pop(); + var syntax = this._extractSyntax(path); // Check if syntax is supported. If not, ignore the file: if (!this.supportedSyntaxes.has(syntax)) @@ -391,6 +399,18 @@ class Comb { return this._shouldProcess(path); } + + /** + * Extract syntax by file path + * + * @param {String} path + * @returns {String} syntax + */ + _extractSyntax(path) { + var extension = path.split('.').pop(); + + return this.syntaxMap.get('.' + extension) || extension; + } } module.exports = Comb; From 4f18208dab304a816bb24649007651c994886cc4 Mon Sep 17 00:00:00 2001 From: isqua Date: Sun, 27 Sep 2015 18:08:42 +0300 Subject: [PATCH 2/2] Tests: Let define syntax for extensions --- test/core.js | 22 ++++++++++++++++++++++ test/helpers/plugin_syntax_map.js | 12 ++++++++++++ test/helpers/scss.tcss | 5 +++++ 3 files changed, 39 insertions(+) create mode 100644 test/helpers/plugin_syntax_map.js create mode 100644 test/helpers/scss.tcss diff --git a/test/core.js b/test/core.js index ceaf523..3b9dd0a 100644 --- a/test/core.js +++ b/test/core.js @@ -38,6 +38,14 @@ describe('Core methods', function() { assert.deepEqual(comb.exclude, [minimatchedPattern]); }); + it('should create syntax map', function() { + var config = {syntax: {'.css': 'scss'}}; + + comb.configure(config); + assert.equal(comb.syntaxMap.size, 1); + assert.equal(comb.syntaxMap.get('.css'), 'scss'); + }); + it('should not configure plugins that have not been added', function() { var config = {animal: 'panda'}; comb.configure(config); @@ -95,6 +103,20 @@ describe('Core methods', function() { done(); }); }); + + it('should use syntax from config', function(done) { + comb.use(require('./helpers/plugin_syntax_map')) + .configure({ + 'syntax-map': true, + syntax: { + '.tcss': 'scss' + } + }) + .lintFile(__dirname + '/helpers/scss.tcss') + .then(function() { + done(); + }); + }) }); describe('lintPath', function() { diff --git a/test/helpers/plugin_syntax_map.js b/test/helpers/plugin_syntax_map.js new file mode 100644 index 0000000..499767a --- /dev/null +++ b/test/helpers/plugin_syntax_map.js @@ -0,0 +1,12 @@ +module.exports = { + name: 'syntax-map', + syntax: ['scss'], + accepts: { + boolean: [true, false] + }, + lint: function() { + return [{ + }]; + }, + process: function() {} +}; diff --git a/test/helpers/scss.tcss b/test/helpers/scss.tcss new file mode 100644 index 0000000..2ee3981 --- /dev/null +++ b/test/helpers/scss.tcss @@ -0,0 +1,5 @@ +.foo { + .bar { + color: red; + } +}