-
Notifications
You must be signed in to change notification settings - Fork 5
Let define syntax for extensions; Fix #6 #7
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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]); | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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; | ||
|
|
@@ -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,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; | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Get values. |
||
| } | ||
| } | ||
|
|
||
| module.exports = Comb; | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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'); | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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); | ||
|
|
@@ -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() { | ||
|
|
||
| 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() {} | ||
| }; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| .foo { | ||
| .bar { | ||
| color: red; | ||
| } | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Create a map.