Skip to content
Merged
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
28 changes: 24 additions & 4 deletions src/core.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Create a map.

// Whether verbose mode is on.
this.verbose = false;
}
Expand All @@ -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]);
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add keys & values to it.

}
}

for (let i = 0, l = this.plugins.length; i < l; i++) {
let plugin = this.plugins[i];
let name = plugin.name;
Expand Down Expand Up @@ -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});
});
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -375,22 +383,34 @@ 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
* is present in `exclude` list. True if everything is ok.
*/
_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))
return false;

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;
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Get values.

}
}

module.exports = Comb;
22 changes: 22 additions & 0 deletions test/core.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Check it size and values.

});

it('should not configure plugins that have not been added', function() {
var config = {animal: 'panda'};
comb.configure(config);
Expand Down Expand Up @@ -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() {
Expand Down
12 changes: 12 additions & 0 deletions test/helpers/plugin_syntax_map.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
module.exports = {
name: 'syntax-map',
syntax: ['scss'],
accepts: {
boolean: [true, false]
},
lint: function() {
return [{
}];
},
process: function() {}
};
5 changes: 5 additions & 0 deletions test/helpers/scss.tcss
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
.foo {
.bar {
color: red;
}
}