Skip to content

Commit b68bf12

Browse files
New: Initial commit
0 parents  commit b68bf12

File tree

9 files changed

+281
-0
lines changed

9 files changed

+281
-0
lines changed

.eslintrc.js

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
'use strict';
2+
const fs = require('fs');
3+
const path = require('path');
4+
const PACKAGE_NAME = require('./package').name;
5+
const SYMLINK_LOCATION = path.join(__dirname, 'node_modules', PACKAGE_NAME);
6+
7+
// Symlink node_modules/{package name} to this directory so that ESLint resolves this plugin name correctly.
8+
if (!fs.existsSync(SYMLINK_LOCATION)) {
9+
fs.symlinkSync(__dirname, SYMLINK_LOCATION);
10+
}
11+
12+
module.exports = {
13+
plugins: [ 'node', 'eslint-plugin', PACKAGE_NAME ],
14+
extends: [
15+
'not-an-aardvark/node',
16+
'plugin:node/recommended',
17+
'plugin:eslint-plugin/recommended'
18+
],
19+
root: true,
20+
rules: {
21+
'prettier/prettier': [ 'error', { singleQuote: true } ],
22+
'eslint-plugin/report-message-format': [ 'error', '^[^a-z].*\\.$' ],
23+
'array-bracket-spacing': 'off',
24+
'comma-dangle': 'off',
25+
'lines-around-directive': 'off',
26+
'space-before-function-paren': 'off'
27+
}
28+
};

.gitignore

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
node_modules/
2+
npm-debug.log

.travis.yml

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
language: node_js
2+
node_js:
3+
- '4'
4+
- '6'
5+
- '7'

LICENSE.md

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
The MIT License (MIT)
2+
=====================
3+
4+
Copyright © 2017 Teddy Katz
5+
6+
Permission is hereby granted, free of charge, to any person
7+
obtaining a copy of this software and associated documentation
8+
files (the “Software”), to deal in the Software without
9+
restriction, including without limitation the rights to use,
10+
copy, modify, merge, publish, distribute, sublicense, and/or sell
11+
copies of the Software, and to permit persons to whom the
12+
Software is furnished to do so, subject to the following
13+
conditions:
14+
15+
The above copyright notice and this permission notice shall be
16+
included in all copies or substantial portions of the Software.
17+
18+
THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND,
19+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
20+
OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
21+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
22+
HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
23+
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
24+
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
25+
OTHER DEALINGS IN THE SOFTWARE.

README.md

+54
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
# eslint-plugin-prettier
2+
3+
Runs [prettier](https://github.com/jlongster/prettier) as an eslint rule
4+
5+
## Installation
6+
7+
You'll first need to install [ESLint](http://eslint.org):
8+
9+
```
10+
$ npm install eslint --save-dev
11+
```
12+
13+
Next, install `eslint-plugin-prettier`:
14+
15+
```
16+
$ npm install eslint-plugin-prettier --save-dev
17+
```
18+
19+
**Note:** If you installed ESLint globally (using the `-g` flag) then you must also install `eslint-plugin-prettier` globally.
20+
21+
## Usage
22+
23+
Add `prettier` to the plugins section of your `.eslintrc` configuration file. You can omit the `eslint-plugin-` prefix:
24+
25+
```json
26+
{
27+
"plugins": [
28+
"prettier"
29+
]
30+
}
31+
```
32+
33+
34+
Then configure the `prettier` rule under the `rules` section:
35+
36+
```json
37+
{
38+
"rules": {
39+
"prettier/prettier": "error"
40+
}
41+
}
42+
```
43+
44+
You can also pass `prettier` configuration as an option:
45+
46+
```json
47+
{
48+
"rules": {
49+
"prettier/prettier": ["error", {"trailingComma": true, "singleQuote": true}]
50+
}
51+
}
52+
```
53+
54+
The rule will report an error if your code does not match `prettier` style. The rule is autofixable -- if you run `eslint` with the `--fix` flag, your code will be formatted according to `prettier` style.

lib/index.js

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
/**
2+
* @fileoverview Runs prettier as an eslint rule
3+
* @author Teddy Katz
4+
*/
5+
'use strict';
6+
// ------------------------------------------------------------------------------
7+
// Requirements
8+
// ------------------------------------------------------------------------------
9+
10+
const requireIndex = require('requireindex');
11+
12+
// ------------------------------------------------------------------------------
13+
// Plugin Definition
14+
// ------------------------------------------------------------------------------
15+
// import all rules in lib/rules
16+
module.exports.rules = requireIndex(__dirname + '/rules');

lib/rules/prettier.js

+58
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
/**
2+
* @fileoverview runs `prettier` as an eslint rule
3+
* @author Teddy Katz
4+
*/
5+
'use strict';
6+
const util = require('util');
7+
const prettier = require('prettier');
8+
const astUtils = require('eslint/lib/ast-utils');
9+
10+
// ------------------------------------------------------------------------------
11+
// Rule Definition
12+
// ------------------------------------------------------------------------------
13+
module.exports = {
14+
meta: { fixable: 'code', schema: [ { type: 'object' } ] },
15+
create(context) {
16+
const sourceCode = context.getSourceCode();
17+
18+
return {
19+
Program() {
20+
// This isn't really very performant (prettier needs to reparse the text).
21+
// However, I don't think it's possible to run `prettier` on an ESTree AST.
22+
const desiredText = prettier.format(
23+
sourceCode.text,
24+
context.options[0]
25+
);
26+
27+
if (sourceCode.text !== desiredText) {
28+
// Find the first character that differs
29+
const firstBadIndex = Array
30+
.from(desiredText)
31+
.findIndex((char, index) => char !== sourceCode.text[index]);
32+
const expectedChar = firstBadIndex === -1
33+
? 'EOF'
34+
: desiredText[firstBadIndex];
35+
const foundChar = firstBadIndex >= sourceCode.text.length
36+
? 'EOF'
37+
: firstBadIndex === -1
38+
? sourceCode.text[desiredText.length]
39+
: sourceCode.text[firstBadIndex];
40+
41+
context.report({
42+
loc: astUtils.getLocationFromRangeIndex(
43+
sourceCode,
44+
firstBadIndex === -1 ? desiredText.length : firstBadIndex
45+
),
46+
message: 'Follow `prettier` formatting (expected {{expectedChar}} but found {{foundChar}}).',
47+
data: {
48+
expectedChar: util.inspect(expectedChar),
49+
foundChar: util.inspect(foundChar)
50+
},
51+
fix: fixer =>
52+
fixer.replaceTextRange([ 0, sourceCode.text.length ], desiredText)
53+
});
54+
}
55+
}
56+
};
57+
}
58+
};

package.json

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
{
2+
"name": "eslint-plugin-prettier",
3+
"version": "1.0.0",
4+
"description": "Runs prettier as an eslint rule",
5+
"keywords": [
6+
"eslint",
7+
"eslintplugin",
8+
"eslint-plugin"
9+
],
10+
"author": "Teddy Katz",
11+
"main": "lib/index.js",
12+
"scripts": {
13+
"lint": "eslint .eslintrc.js lib/ tests/ --ignore-pattern !.eslintrc.js",
14+
"test": "npm run lint && mocha tests --recursive"
15+
},
16+
"dependencies": {
17+
"prettier": "^0.11.0",
18+
"requireindex": "~1.1.0"
19+
},
20+
"peerDependencies": {
21+
"eslint": "^3.14.1"
22+
},
23+
"devDependencies": {
24+
"eslint": "^3.14.1",
25+
"eslint-config-not-an-aardvark": "^2.0.0",
26+
"eslint-plugin-eslint-plugin": "^0.2.1",
27+
"eslint-plugin-node": "^3.0.5",
28+
"mocha": "^3.1.2"
29+
},
30+
"engines": {
31+
"node": ">=4.0.0"
32+
},
33+
"license": "MIT"
34+
}

tests/lib/rules/prettier.js

+59
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
/**
2+
* @fileoverview runs `prettier` as an eslint rule
3+
* @author Teddy Katz
4+
*/
5+
'use strict';
6+
// ------------------------------------------------------------------------------
7+
// Requirements
8+
// ------------------------------------------------------------------------------
9+
10+
const rule = require('../../../lib/rules/prettier');
11+
const RuleTester = require('eslint').RuleTester;
12+
13+
// ------------------------------------------------------------------------------
14+
// Tests
15+
// ------------------------------------------------------------------------------
16+
const ruleTester = new RuleTester();
17+
18+
ruleTester.run('prettier', rule, {
19+
valid: [
20+
'foo(bar);\n',
21+
'foo("bar");\n',
22+
{ code: "foo('bar');\n", options: [ { singleQuote: true } ] }
23+
],
24+
invalid: [
25+
{
26+
code: 'foo(bar )',
27+
output: 'foo(bar);\n',
28+
errors: [
29+
{
30+
line: 1,
31+
column: 8,
32+
message: "Follow `prettier` formatting (expected ')' but found ' ')."
33+
}
34+
]
35+
},
36+
{
37+
code: 'foo(bar);',
38+
output: 'foo(bar);\n',
39+
errors: [
40+
{
41+
line: 1,
42+
column: 10,
43+
message: "Follow `prettier` formatting (expected '\\n' but found 'EOF')."
44+
}
45+
]
46+
},
47+
{
48+
code: 'foo(bar);\n\n',
49+
output: 'foo(bar);\n',
50+
errors: [
51+
{
52+
line: 2,
53+
column: 1,
54+
message: "Follow `prettier` formatting (expected 'EOF' but found '\\n')."
55+
}
56+
]
57+
}
58+
]
59+
});

0 commit comments

Comments
 (0)