From fbcb0b99ac7bbacd5e52b0d149253a11b1dfcf1e Mon Sep 17 00:00:00 2001 From: Richard Brown Date: Fri, 31 Aug 2012 16:48:17 +0100 Subject: [PATCH] less: Add a linter script to avoid compiling files. Using lessc to check a less file results in a NameError if the file references a variable defined in a file that inherits it. This commit adds a new linter that just calls the less parser instead. The variable g:syntastic_less_use_less_lint should be set to use the linter over lessc. --- syntax_checkers/less-lint.coffee | 41 +++++++++++++++++++++++ syntax_checkers/less-lint.js | 57 ++++++++++++++++++++++++++++++++ syntax_checkers/less.vim | 17 +++++++++- 3 files changed, 114 insertions(+), 1 deletion(-) create mode 100644 syntax_checkers/less-lint.coffee create mode 100644 syntax_checkers/less-lint.js diff --git a/syntax_checkers/less-lint.coffee b/syntax_checkers/less-lint.coffee new file mode 100644 index 00000000..0b05e4a7 --- /dev/null +++ b/syntax_checkers/less-lint.coffee @@ -0,0 +1,41 @@ +#!/usr/bin/env node + +fs = require 'fs' +less = require 'less' +args = process.argv.slice(1) +options = {} + +args = args.filter (arg) -> + match = arg.match(/^-I(.+)$/) + if match + options.paths.push(match[1]); + return false + + match = arg.match(/^--?([a-z][\-0-9a-z]*)(?:=([^\s]+))?$/i) + if match + arg = match[1] + else + return arg + + switch arg + when 'strict-imports' then options.strictImports = true + when 'include-path' + options.paths = match[2].split(if os.type().match(/Windows/) then ';' else ':') + .map (p) -> + if p + return path.resolve(process.cwd(), p) + when 'O0' then options.optimization = 0 + when 'O1' then options.optimization = 1 + when 'O2' then options.optimization = 2 + +options.filename = args[1] + +parser = new(less.Parser) options + +fs.readFile(options.filename, 'utf-8', (err,data) -> + parser.parse(data, (err, tree) -> + if err + less.writeError err + process.exit(1) + ) +) diff --git a/syntax_checkers/less-lint.js b/syntax_checkers/less-lint.js new file mode 100644 index 00000000..5abc653c --- /dev/null +++ b/syntax_checkers/less-lint.js @@ -0,0 +1,57 @@ +// Generated by CoffeeScript 1.3.3 +(function() { + var args, fs, less, options, parser; + + fs = require('fs'); + + less = require('less'); + + args = process.argv.slice(1); + + options = {}; + + args = args.filter(function(arg) { + var match; + match = arg.match(/^-I(.+)$/); + if (match) { + options.paths.push(match[1]); + return false; + } + match = arg.match(/^--?([a-z][\-0-9a-z]*)(?:=([^\s]+))?$/i); + if (match) { + arg = match[1]; + } else { + return arg; + } + switch (arg) { + case 'strict-imports': + return options.strictImports = true; + case 'include-path': + return options.paths = match[2].split(os.type().match(/Windows/) ? ';' : ':').map(function(p) { + if (p) { + return path.resolve(process.cwd(), p); + } + }); + case 'O0': + return options.optimization = 0; + case 'O1': + return options.optimization = 1; + case 'O2': + return options.optimization = 2; + } + }); + + options.filename = args[1]; + + parser = new less.Parser(options); + + fs.readFile(options.filename, 'utf-8', function(err, data) { + return parser.parse(data, function(err, tree) { + if (err) { + less.writeError(err); + return process.exit(1); + } + }); + }); + +}).call(this); diff --git a/syntax_checkers/less.vim b/syntax_checkers/less.vim index 1338ffda..af122424 100644 --- a/syntax_checkers/less.vim +++ b/syntax_checkers/less.vim @@ -9,6 +9,14 @@ " See http://sam.zoy.org/wtfpl/COPYING for more details. " "============================================================================ + +" To send additional options to less use the variable g:syntastic_less_options. +" The default is +" let g:syntastic_less_options = "--no-color" +" +" To use less-lint instead of less set the variable +" g:syntastic_less_use_less_lint. + if exists("loaded_less_syntax_checker") finish endif @@ -23,8 +31,15 @@ if !exists("g:syntastic_less_options") let g:syntastic_less_options = "--no-color" endif +if !exists("g:syntastic_less_use_less_lint") || g:syntastic_less_use_less_lint == 0 + let s:check_file = 'lessc' +else + let s:check_file = 'node ' . expand(':p:h') . '/less-lint.js' +end + function! SyntaxCheckers_less_GetLocList() - let makeprg = 'lessc '. g:syntastic_less_options .' '. shellescape(expand('%')) . ' /dev/null' + let makeprg = s:check_file . ' ' . g:syntastic_less_options . ' ' . + \ shellescape(expand('%')) . ' /dev/null' "lessc >= 1.2 let errorformat = '%m in %f:%l:%c'