npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

gulp-patternlint

v0.1.4

Published

A generic pattern lint plugin for gulp

Downloads

27

Readme

gulp-patternlint NPM version Build status

Pattern Lint is a generic plugin for linting any set of files for patterns, specified as either strings or regular expressions.

This can be used to enforce project-specific guidelines or team-specific practices on files in your projects, or anything that's too specific for more general, filetype-specific linters.

Usage

First, install gulp-patternlint as a development dependency:

npm install --save-dev gulp-patternlint

Then, add it to your gulpfile.js:

var patternlint = require('gulp-patternlint');

gulp.task('js', function () {
	gulp.src('src/js/*.js')
		.pipe(patternlint())
		.pipe(patternlint.reporter());
});

API

Rule Configuration

You can pass rules as an array of objects.

gulp.task('javascript', function () {
	return gulp.src('src/js/main.js')
	.pipe(patternLint([{
		message: 'Don\'t leave any console statements in your JS.',
		pattern: 'console'
	}, {
		message: 'Use the protocol-independent // instead of http or https.',
		regexp: '(http://|https://)'
	}]))
	...
});

Strings

Strings should be specified as the pattern property.

Regular Expressions

Regular expressions should be specified as the regexp property. Please leave off the opening and closing slashes.

You can also add a flags property to specify additional regular expression flags. There is no need to add g: all rules get searched for globally.

gulp.task('javascript', function () {
	return gulp.src('src/js/main.js')
	.pipe(patternLint({
		message: 'Use the protocol-independent // instead of http or https.',
		regexp: '(http://|https://)',
		flags: 'i' // make the search case-insensitive
	}))
	...
});

patternlintrc

Type: String

If no rules array is passed in, Pattern Lint will look for .patternlintrc in the root of your project, where you can specify the rules as JSON (double-quotes, please). You can pass in a string to use a different rules file.

gulp.src('client/css/*.css')
	.pipe(patternlint('bestpractices.json'))
	.pipe(patternlint.reporter());

Results

Adds the following properties to the file object:

file.patternlint.success = true; // or false
file.patternlint.errorCount = 0; // number of errors returned by PatternLint
file.patternlint.results = []; // PatternLint errors
file.patternlint.rules = {}; // The rules you passed to PatternLint

Custom Reporters

Pattern Lint has a lovely default reporter that sends output to your gulp process with information about linting problems. But if you'd like to make your own, custom reporter functions can be passed as patternlint.reporter(reporterFunc). The reporter function will be called for each linted file and passed the file object as described above.

var patternlint = require('gulp-patternlint');
var gutil = require('gulp-util');

var customReporter = function (file) {
	gutil.log(gutil.colors.cyan(file.patternlint.errorCount) + ' errors in ' + gutil.colors.magenta(file.path));

	file.patternlint.results.forEach(function (result) {
		/*
		 * The error object has these properties:
		 * line: the line number of the error
		 * col: the column number of the error
		 * pre: an excerpt of the file that preceded the error,
		   no longer than 25 characters
		 * match: the text of the file that produced the error
		 * post: an excerpt of the file that followed the error,
		   no longer than 25 characters
		 * message: the rule's message (ie, "Don't use the word
		   irregardless. Because it's not a word.")
		 */
		gutil.log(result.error.message + ' on line ' + result.error.line);
	});
};

gulp.task('lint', function () {
	gulp.files('lib/*.css')
		.pipe(patternlint())
		.pipe(patternlint.reporter(customReporter));
});

Acknowledgements

This plugin is almost a fork of @lazd's gulp-csslint plugin, which I used as a model to learn how to write a Gulp plugin.