Skip to content

Commit

Permalink
minor tweaks and minor bug fixes. set up stylint command
Browse files Browse the repository at this point in the history
  • Loading branch information
Ross committed Dec 14, 2014
1 parent d204c15 commit defb511
Show file tree
Hide file tree
Showing 7 changed files with 49 additions and 29 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,4 @@ results

npm-debug.log
node_modules
styl
3 changes: 3 additions & 0 deletions bin/stylint
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#!/usr/bin/env node

require("../index.js");
28 changes: 15 additions & 13 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,8 @@ var Lint = (function() {
else if (stats.isDirectory()) {
glob(lintMe + '**/*.styl', {}, function(err, files) {
if (err) { throw err; }
len = files.length - 1;
var i = 0,
len = files.length - 1;

// iterate over every file
lazy(files).each(function(file) {
Expand Down Expand Up @@ -162,7 +163,7 @@ var Lint = (function() {
var output = line.trim();
lineNum += 1;
// run our tests on the line
return Lint.test(line, lineNum, output);
return Lint.test(line, lineNum, output, file);
})
.onComplete(function() {
// are we done yet? only output warnings and errors when on the last file
Expand All @@ -178,9 +179,10 @@ var Lint = (function() {
* @param {string} line [line of stylus to test]
* @param {number} num [the line number]
* @param {string} output [trimmed version of string to output to terminal]
* @param {string} [file] [curr file being linted]
* @return void
*/
test: function(line, num, output) {
test: function(line, num, output, file) {
/**
* first two tests determine if the rest of the tests should run
* if @stylint: off comment found, disable tests until @stylint: on comment found
Expand All @@ -196,49 +198,49 @@ var Lint = (function() {

// check for space after // comment (//comment is invalid)
if (enabled === true && config.comments === true && commentStyleCorrect(line) === false) {
warnings.push('Space after comment is preferred:\nLine: ' + num+ ': ' + output);
warnings.push(chalk.yellow('Space after comment is preferred:') + '\nFile: ' + file + '\nLine: ' + num+ ': ' + output);
}

// check for 0px (margin 0 is preferred over margin 0px)
if (enabled === true && config.unecessaryPX === true && pxStyleCorrect(line) === false) {
warnings.push('0 is preferred over 0px.\nLine: ' + num + ': ' + output);
warnings.push(chalk.yellow('0 is preferred over 0px.') + '\nFile: ' + file + '\nLine: ' + num + ': ' + output);
}

// check for * selector (* is discouraged)
if (enabled === true && config.universal === true && universalSelector(line) === true) {
warnings.push('* selector is slow. Consider a different selector.\nLine: ' + num + ': ' + output);
warnings.push(chalk.yellow('* selector is slow. Consider a different selector.') + '\nFile: ' + file + '\nLine: ' + num + ': ' + output);
}

// check for unecessary : (margin 0 is preferred over margin: 0)
if (enabled === true && config.colons === true && colon(line) === true) {
warnings.push('Unecessary colon found:\nLine: ' + num + ': ' + output);
warnings.push(chalk.yellow('Unecessary colon found:') + '\nFile: ' + file + '\nLine: ' + num + ': ' + output);
}

// check for unecessary ; (margin 0; is invalid)
if (enabled === true && config.semicolons === true && semicolon(line) === true) {
warnings.push('Unecessary semicolon found:\nLine: ' + num + ': ' + output);
warnings.push(chalk.yellow('Unecessary semicolon found:') + '\nFile: ' + file + '\nLine: ' + num + ': ' + output);
}

// check for places where we can be more efficient (margin 0 50px vs margin 0 50px 0 50px)
if (enabled === true && config.efficient === true && notEfficient(line) === true) {
warnings.push('The properties on this line could be more succinct:\nLine: ' + num + ': ' + output);
warnings.push(chalk.yellow('The properties on this line could be more succinct:') + '\nFile: ' + file + '\nLine: ' + num + ': ' + output);
}

// check selector depth
if (enabled === true && config.depth === true) {
// if you're a bad person and you set tabs and spaces to both be true, default to tabs
if (config.tabs === true && config.spaces === true) {
if (tooMuchNest(line, config.depthLimit, 'tabs', config.indent) === true) {
warnings.push('Selector depth greater than 4:\nLine: ' + num + ': ' + output);
warnings.push(chalk.yellow('Selector depth greater than 4:') + '\nFile: ' + file + '\nLine: ' + num + ': ' + output);
}
}
else {
// else check tabs against tabs and spaces against spaces
if (config.tabs === true && tooMuchNest(line, config.depthLimit, 'tabs', config.indent) === true) {
warnings.push('Selector depth greater than 4:\nLine: ' + num + ': ' + output);
warnings.push(chalk.yellow('Selector depth greater than 4:') + '\nFile: ' + file + '\nLine: ' + num + ': ' + output);
}
else if (config.spaces === true && tooMuchNest(line, config.depthLimit, 'spaces', config.indent) === true) {
warnings.push('Selector depth greater than 4:\nLine: ' + num + ': ' + output);
warnings.push(chalk.yellow('Selector depth greater than 4:') + '\nFile: ' + file + '\nLine: ' + num + ': ' + output);
}
}
}
Expand Down Expand Up @@ -289,7 +291,7 @@ var Lint = (function() {
done: function() {
// all warnings
for (var i = 0; i < warnings.length; i++) {
console.log( chalk.yellow('Warning: ' + warnings[i]) + '\n' );
console.log( chalk.yellow('Warning: ') + warnings[i] + '\n' );
}

// if you pass strict it displays a slightly more annoying message (that'll show em!)
Expand Down
10 changes: 8 additions & 2 deletions lib/checkCommentStyle.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,15 @@
const comment = /\/\/\s/;

module.exports = function checkCommentStyle(line) {
console.log(line.indexOf('//'));
// if // is present on line
if (line.indexOf('//') !== -1) {
// check for space after comment, if no space, return warning
if (!comment.test(line)) {
// check for space after comment on it's own line, if no space, return warning
if (line.indexOf('//') === 0 && !comment.test(line)) {
return false;
}
// check for space after comment if on same line, if no space, return warning
else if (line.indexOf('http://') === -1 && !comment.test(line)) {
return false;
}
}
Expand Down
2 changes: 0 additions & 2 deletions lib/checkForEfficiency.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@ module.exports = function checkForEfficiency(line) {
// create an array from the line with all white space removed
arr = arr.filter(function(str) { return str.length > 0; });

console.log(arr);

// if margin or padding we run the tests
if (arr[0] === 'margin' || arr[0] === 'padding' || arr[0] === '\tmargin' || arr[0] === '\tpadding') {
// if line is potentially inefficient it needs to be at least this long
Expand Down
29 changes: 19 additions & 10 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,20 +1,21 @@
{
"name": "stylus-lint",
"version": "1.0.0",
"name": "stylint",
"version": "0.1.0",
"description": "A linter for stylus",
"author": "Ross Patton",
"license": "ISC",
"main": "index.js",
"scripts": {
"start": "node index.js"
},
"repository": {
"type": "git",
"url": "stylus-lint"
"url": "https://github.com/rossPatton/s-linter"
},
"bin": {
"stylint": "./bin/stylint"
},
"keywords": [
"stylint"
"stylus"
],
"author": "Ross Patton",
"license": "ISC",
"preferGlobal": true,
"devDependencies": {
"chalk": "^0.5.1",
"chokidar": "^0.12.3",
Expand All @@ -24,6 +25,14 @@
"yargs": "^1.3.3"
},
"dependencies": {
"log-symbols": "^1.0.1"
"chalk": "^0.5.1",
"chokidar": "^0.12.3",
"glob": "^4.3.1",
"lazy.js": "^0.3.2",
"log-symbols": "^1.0.1",
"yargs": "^1.3.3"
},
"scripts": {
"start": "node index.js"
}
}
5 changes: 3 additions & 2 deletions styl/test2.styl
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@ code,
kbd,
pre,
samp
width 100% //4
width 100% //http://


// images and video won't break their containers
Expand All @@ -192,4 +192,5 @@ video,
object,
figure,
iframe
max-width 100%
max-width 100%
//

0 comments on commit defb511

Please sign in to comment.