From 822946bae07c50446fd59ebe1b4d274351bc07e0 Mon Sep 17 00:00:00 2001 From: Christopher Mardell Date: Sun, 21 Sep 2014 10:03:22 +0930 Subject: [PATCH] Add Coffeelint and literate coffee support --- index.js | 71 +++++++++++++++++++++++++++++++++++++++------------- package.json | 1 + 2 files changed, 55 insertions(+), 17 deletions(-) diff --git a/index.js b/index.js index a6c78ce..8d12cea 100644 --- a/index.js +++ b/index.js @@ -1,26 +1,63 @@ -var Filter = require('broccoli-filter') +/* jshint node: true */ + +var Filter = require('broccoli-filter'); var coffeeScript = require('coffee-script') +var CoffeeLint = require('CoffeeLint'); + +module.exports = CoffeeScriptFilter; +CoffeeScriptFilter.prototype = Object.create(Filter.prototype); +CoffeeScriptFilter.prototype.constructor = CoffeeScriptFilter; -module.exports = CoffeeScriptFilter -CoffeeScriptFilter.prototype = Object.create(Filter.prototype) -CoffeeScriptFilter.prototype.constructor = CoffeeScriptFilter function CoffeeScriptFilter (inputTree, options) { - if (!(this instanceof CoffeeScriptFilter)) return new CoffeeScriptFilter(inputTree, options) - Filter.call(this, inputTree, options) - options = options || {} - this.bare = options.bare + if (!(this instanceof CoffeeScriptFilter)) { + return new CoffeeScriptFilter(inputTree, options); + } + Filter.call(this, inputTree, options); + + this.options = options || {}; + this.bare = options.bare; + this.CoffeeLint = CoffeeLint; + + if (options.coffeelintOptions) { + this.coffeelintOptions = options.coffeelintOptions; + delete this.options.coffeelintOptions; + } else if (!this.coffeelintOptions) { + this.coffeelintOptions = {}; + } + this.logger = options.logger || console.log.bind(console); + } -CoffeeScriptFilter.prototype.extensions = ['coffee'] -CoffeeScriptFilter.prototype.targetExtension = 'js' +CoffeeScriptFilter.prototype.extensions = ['coffee','litcoffee','coffee.md']; +CoffeeScriptFilter.prototype.targetExtension = 'js'; + +CoffeeScriptFilter.prototype.processString = function (string, srcFile) { + console.log('Coffeescript processString'); + // throw new error; + var coffeeScriptOptions = { + bare: this.bare, + literate: coffeeScript.helpers.isLiterate(srcFile) + }; + var errors = this.CoffeeLint.lint(string, this.coffeelintOptions, coffeeScriptOptions.literate); + + if (errors && errors.length) { + this.reportLintErrors(srcFile, errors); + } -CoffeeScriptFilter.prototype.processString = function (string) { - var coffeeScriptOptions = { bare: this.bare } try { - return coffeeScript.compile(string, coffeeScriptOptions) + return coffeeScript.compile(string, coffeeScriptOptions); } catch (err) { - err.line = err.location && err.location.first_line - err.column = err.location && err.location.first_column - throw err + err.line = err.location && err.location.first_line; + err.column = err.location && err.location.first_column; + throw err; } -} +}; + +CoffeeScriptFilter.prototype.reportLintErrors = function CoffeeScriptFilter_reportLintErrors(relativePath, errors) { + var self = this; + + for (var i = 0; i < errors.length; i++) { + var e = errors[i]; + self.logger(relativePath + ':' + e.lineNumber + ': ' + e.message + (e.context? ': ' + e.context : '')); + } +}; \ No newline at end of file diff --git a/package.json b/package.json index bcab4d3..1340211 100644 --- a/package.json +++ b/package.json @@ -16,6 +16,7 @@ ], "dependencies": { "coffee-script": "~1.7.1", + "coffeelint": "^1.6.0", "broccoli-filter": "^0.1.6" } }