What is standard-engine?
The standard-engine npm package is a tool for creating JavaScript style guides and enforcing code style rules. It provides a way to define and apply custom linting rules, ensuring code consistency and quality across projects.
What are standard-engine's main functionalities?
Linting
This feature allows you to create a custom linter using standard-engine. You can define your own linting rules and apply them to your project files.
const standardEngine = require('standard-engine');
const opts = {
cmd: 'my-linter',
version: '1.0.0',
homepage: 'https://example.com',
bugs: 'https://example.com/issues',
eslintConfig: {
configFile: 'path/to/eslint/config'
}
};
const linter = standardEngine.linter(opts);
linter.lintFiles(['src/**/*.js'], (err, results) => {
if (err) throw err;
console.log(results);
});
Fixing
This feature allows you to automatically fix linting errors in your code. By setting the `fix` option to true, standard-engine will attempt to correct any issues it finds.
const standardEngine = require('standard-engine');
const opts = {
cmd: 'my-linter',
version: '1.0.0',
homepage: 'https://example.com',
bugs: 'https://example.com/issues',
eslintConfig: {
configFile: 'path/to/eslint/config'
}
};
const linter = standardEngine.linter(opts);
linter.lintFiles(['src/**/*.js'], { fix: true }, (err, results) => {
if (err) throw err;
console.log(results);
});
Custom Reporters
This feature allows you to use custom reporters to format the linting results. You can define your own formatter and pass it to standard-engine.
const standardEngine = require('standard-engine');
const opts = {
cmd: 'my-linter',
version: '1.0.0',
homepage: 'https://example.com',
bugs: 'https://example.com/issues',
eslintConfig: {
configFile: 'path/to/eslint/config'
},
customFormatter: require('my-custom-formatter')
};
const linter = standardEngine.linter(opts);
linter.lintFiles(['src/**/*.js'], (err, results) => {
if (err) throw err;
console.log(results);
});
Other packages similar to standard-engine
eslint
ESLint is a widely-used JavaScript linter that helps you find and fix problems in your JavaScript code. It is highly configurable and supports custom rules, plugins, and configurations. Compared to standard-engine, ESLint is more flexible and has a larger community and ecosystem.
prettier
Prettier is an opinionated code formatter that enforces a consistent style by parsing your code and re-printing it with its own rules. While it focuses more on code formatting rather than linting, it can be used in conjunction with linters like ESLint. Compared to standard-engine, Prettier is more focused on code formatting rather than enforcing specific linting rules.
jshint
JSHint is a static code analysis tool used to detect errors and potential problems in JavaScript code. It is less configurable than ESLint but still provides a good set of default rules. Compared to standard-engine, JSHint is simpler and less flexible but can be easier to set up for basic linting needs.
Standard Engine
Overview
Wrap your own eslint rules in a easy-to-use command line tool and/or a JS module.
Install
npm install standard-engine
Who is using standard-engine
?
Here is a list of packages using standard-engine
. Dive into them for ideas!
Did you make your own? Create a pull request and we will add it to the README!
Usage
Create the files below and fill in your own values for options.js
.
index.js
var Linter = require('standard-engine').linter
var opts = require('./options.js')
module.exports = new Linter(opts)
cli.js
#!/usr/bin/env node
var opts = require('../options.js')
require('standard-engine').cli(opts)
options.js
var eslint = require('eslint')
var path = require('path')
var pkg = require('./package.json')
module.exports = {
version: pkg.version,
homepage: pkg.homepage,
bugs: pkg.bugs.url,
eslint: eslint,
cmd: 'pocketlint',
tagline: 'Live by your own standards!',
eslintConfig: {
configFile: path.join(__dirname, 'eslintrc.json')
},
cwd: ''
}
eslintrc.json
Put all your .eslintrc rules in this file. A good practice is to create an ESLint Shareable Config and extend it, but its not required:
{
"extends": ["pocketlint"]
}
Take a look at eslint-config-standard as an example, or if you want to extend/mutate standard
, see eslint-config-semistandard.
Engine Features
Ignoring Files
The paths node_modules/**
, *.min.js
, bundle.js
, coverage/**
, hidden files/folders
(beginning with .
), and all patterns in a project's root .gitignore
file are
automatically ignored.
Sometimes you need to ignore additional folders or specific minfied files. To do that, add
a ignore
property to package.json
:
"pocketlint": {
"ignore": [
"**/out/",
"/lib/select2/",
"/lib/ckeditor/",
"tmp.js"
]
}
Hiding Warnings
Since standard-engine
uses eslint
under-the-hood, you can
hide warnings as you normally would if you used eslint
directly.
To get verbose output (so you can find the particular rule name to ignore), run:
$ pocketlint --verbose
Error: Live by your own standards!
routes/error.js:20:36: 'file' was used before it was defined. (no-use-before-define)
Disable all rules on a specific line:
file = 'I know what I am doing'
Or, disable only the "no-use-before-define"
rule:
file = 'I know what I am doing'
Or, disable the "no-use-before-define"
rule for multiple lines:
Defining Globals in a project's package.json
standard-engine
will also look in a project's package.json
and respect any global variables defined like so:
{
"pocketlint": {
"global": [ "myVar1", "myVar2" ]
}
}
Custom JS parsers for bleeding-edge ES6 or ES7 support?
standard-engine
supports custom JS parsers. To use a custom parser, install it from npm
(example: npm install babel-eslint
) and add this to your package.json
:
{
"pocketlint": {
"parser": "babel-eslint"
}
}
If you're using your custom linter globally (you installed it with -g
), then you also need to
install babel-eslint
globally with npm install babel-eslint -g
.
API Usage
standardEngine.lintText(text, [opts], callback)
Lint the provided source text
to enforce your defined style. An opts
object may
be provided:
{
globals: [],
parser: '',
fix: false
}
The callback
will be called with an Error
and results
object:
{
results: [
{
filePath: '',
messages: [
{ ruleId: '', message: '', line: 0, column: 0 }
],
errorCount: 0,
warningCount: 0,
output: ''
}
],
errorCount: 0,
warningCount: 0
}
standardEngine.lintFiles(files, [opts], callback)
Lint the provided files
globs. An opts
object may be provided:
{
globals: [],
parser: '',
ignore: [],
cwd: '',
fix: false
}
The callback
will be called with an Error
and results
object (same as above).