Skip to content

Commit

Permalink
Merge with eslint-plugin-prettify (#21)
Browse files Browse the repository at this point in the history
* Chore: Use eslint-plugin-prettier without symlink

* Chore: Lint .eslintrc.js via CLI automatically

* Chore: Update remaining repo links

* New: Replace implementation with line-by-line reporting (fixes #17)

This code is originally from zertosh/eslint-plugin-prettify@2fb5dfb. It's has been adjusted to conform to the existing ESLint style.

* Fix: Use non-global line ending regex

* Fix: Use unicode literals instead of escapes in showInvisibles

* Fix: Use documented range form when calling insertTextAfterRange

* Fix: Un-shebang test file

* Fix: ESLint 4.x compat (handle shebangs and no deprecated APIs)
  • Loading branch information
zertosh authored May 16, 2017
1 parent 6b69492 commit 6de494f
Show file tree
Hide file tree
Showing 33 changed files with 881 additions and 221 deletions.
1 change: 1 addition & 0 deletions .eslintignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
!.eslintrc.js
14 changes: 4 additions & 10 deletions .eslintrc.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,11 @@
'use strict';

const fs = require('fs');
const path = require('path');
const PACKAGE_NAME = require('./package').name;
const SYMLINK_LOCATION = path.join(__dirname, 'node_modules', PACKAGE_NAME);

// Symlink node_modules/{package name} to this directory so that ESLint resolves this plugin name correctly.
if (!fs.existsSync(SYMLINK_LOCATION)) {
fs.symlinkSync(__dirname, SYMLINK_LOCATION);
}
// Register ourselves as a plugin to avoid `node_modules` trickery.
const Plugins = require('eslint/lib/config/plugins');
Plugins.define('prettier', require('.'));

module.exports = {
plugins: ['node', 'eslint-plugin', PACKAGE_NAME],
plugins: ['node', 'eslint-plugin', 'prettier'],
extends: [
'not-an-aardvark/node',
'plugin:node/recommended',
Expand Down
2 changes: 0 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,3 @@
* Breaking: Make prettier a peerDependency ([#1](https://github.com/not-an-aardvark/eslint-plugin-prettier/issues/1)) ([d8a8992](https://github.com/not-an-aardvark/eslint-plugin-prettier/commit/d8a89922ddc6b747c474b62a0948deba6ea2657d))
* Docs: add repo url to package.json ([2474bc9](https://github.com/not-an-aardvark/eslint-plugin-prettier/commit/2474bc9dd3f05dbd0b1fec38e27bc91a9cb0f1c7))
* Docs: suggest prettier-eslint if eslint rules disagree with prettier ([3414437](https://github.com/not-an-aardvark/eslint-plugin-prettier/commit/341443754ae231a17d82f037f8b35663257d282a))


2 changes: 1 addition & 1 deletion CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,4 @@ npm test

This is an [ESLint](http://eslint.org) plugin. Documentation for the APIs that it uses can be found on ESLint's [Working with Plugins](http://eslint.org/docs/developer-guide/working-with-plugins) page.

This plugin is used to lint itself. The style is checked when `npm test` is run, and the build will fail if there are any linting errors. You can use `npm run lint -- --fix` to fix some linting errors. To run the tests without running the linter, you can use `node_modules/.bin/mocha tests --recursive`.
This plugin is used to lint itself. The style is checked when `npm test` is run, and the build will fail if there are any linting errors. You can use `npm run lint -- --fix` to fix some linting errors. To run the tests without running the linter, you can use `node_modules/.bin/mocha`.
2 changes: 1 addition & 1 deletion LICENSE.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
The MIT License (MIT)
=====================

Copyright © 2017 Teddy Katz
Copyright © 2017 Andres Suarez and Teddy Katz

Permission is hereby granted, free of charge, to any person
obtaining a copy of this software and associated documentation
Expand Down
130 changes: 85 additions & 45 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,78 +1,118 @@
# eslint-plugin-prettier [![Build Status](https://travis-ci.org/prettier/eslint-plugin-prettier.svg?branch=master)](https://travis-ci.org/prettier/eslint-plugin-prettier)

Runs [prettier](https://github.com/jlongster/prettier) as an eslint rule
Runs [Prettier](https://github.com/prettier/prettier) as an [ESLint](http://eslint.org) rule and reports differences as individual ESLint issues.

## Installation
## Sample

You'll first need to install [ESLint](http://eslint.org):
```js
error: Insert `,` (prettier/prettier) at pkg/commons-atom/ActiveEditorRegistry.js:22:25:
20 | import {
21 | observeActiveEditorsDebounced,
> 22 | editorChangesDebounced
| ^
23 | } from './debounced';;
24 |
25 | import {observableFromSubscribeFunction} from '../commons-node/event';

```
$ npm install eslint --save-dev
```

Next, install `prettier`:
error: Delete `;` (prettier/prettier) at pkg/commons-atom/ActiveEditorRegistry.js:23:21:
21 | observeActiveEditorsDebounced,
22 | editorChangesDebounced
> 23 | } from './debounced';;
| ^
24 |
25 | import {observableFromSubscribeFunction} from '../commons-node/event';
26 | import {cacheWhileSubscribed} from '../commons-node/observable';

```
$ npm install prettier --save-dev

2 errors found.
```

Finally, install `eslint-plugin-prettier`:
> `./node_modules/.bin/eslint --format codeframe pkg/commons-atom/ActiveEditorRegistry.js` (code from [nuclide](https://github.com/facebook/nuclide)).
```
$ npm install eslint-plugin-prettier --save-dev
```
## Installation

**Note:** If you installed ESLint globally (using the `-g` flag) then you must also install `eslint-plugin-prettier` globally.
```sh
npm install --save-dev prettier eslint-plugin-prettier
```

## Usage
**_`eslint-plugin-prettier` does not install Prettier or ESLint for you._** _You must install these yourself._

Add `prettier` to the plugins section of your `.eslintrc` configuration file. You can omit the `eslint-plugin-` prefix:
Then, in your `.eslintrc`:

```json
{
"plugins": [
"prettier"
]
"plugins": [
"prettier"
],
"rules": {
"prettier/prettier": "error"
}
}
```

## Options

Then configure the `prettier` rule under the `rules` section:
* The first option:

```json
{
"rules": {
"prettier/prettier": "error"
}
}
```
- Objects are passed directly to Prettier as [options](https://github.com/prettier/prettier#api). Example:

```json
"prettier/prettier": ["error", {"singleQuote": true, "parser": "flow"}]
```

You can also pass [`prettier` configuration](https://github.com/prettier/prettier#api) as an option:
- Or the string `"fb"` may be used to set "Facebook style" defaults:

```json
{
"rules": {
"prettier/prettier": ["error", {"trailingComma": "es5", "singleQuote": true}]
}
}
```
```json
"prettier/prettier": ["error", "fb"]
```

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.
Equivalent to:

---
```json
"prettier/prettier": ["error", {
"singleQuote": true,
"trailingComma": "all",
"bracketSpacing": false,
"jsxBracketSameLine": true,
"parser": "flow"
}]
```

This plugin works best if you disable all other ESLint rules relating to code formatting, and only enable rules that detect patterns in the AST. (If another active ESLint rule disagrees with `prettier` about how code should be formatted, it will be impossible to avoid lint errors.) You can use [eslint-config-prettier](https://github.com/lydell/eslint-config-prettier) to disable all formatting-related ESLint rules. If your desired formatting does not match the `prettier` output, you should use a different tool such as [prettier-eslint](https://github.com/kentcdodds/prettier-eslint) instead.
* The second option:

## Migrating to 2.0.0
- A string with a pragma that triggers this rule. By default, this rule applies to all files. However, if you set a pragma (this option), only files with that pragma in the heading docblock will be checked. All pragmas must start with `@`. Example:

Starting in 2.0.0, `prettier` is a peerDependency of this module, rather than a dependency. This means that you can use any version of `prettier` with this plugin, but it also means that you need to install it separately and include it as a devDependency in your project.
```json
"prettier/prettier": ["error", null, "@prettier"]
```

To install `prettier`, use:
Only files with `@prettier` in the heading docblock will be checked:

```bash
npm install prettier --save-dev
```
```js
/** @prettier */

console.log(1 + 2 + 3);
```

Or:

```js
/**
* @prettier
*/

console.log(4 + 5 + 6);
```

_This option is useful if you're migrating a large codebase and already use pragmas like `@flow`._

* The rule is autofixable -- if you run `eslint` with the `--fix` flag, your code will be formatted according to `prettier` style.

---

This plugin works best if you disable all other ESLint rules relating to code formatting, and only enable rules that detect patterns in the AST. (If another active ESLint rule disagrees with `prettier` about how code should be formatted, it will be impossible to avoid lint errors.) You can use [eslint-config-prettier](https://github.com/prettier/eslint-config-prettier) to disable all formatting-related ESLint rules. If your desired formatting does not match the `prettier` output, you should use a different tool such as [prettier-eslint](https://github.com/prettier/prettier-eslint) instead.

## Contributing

See [CONTRIBUTING.md](https://github.com/not-an-aardvark/eslint-plugin-prettier/blob/master/CONTRIBUTING.md)
See [CONTRIBUTING.md](https://github.com/prettier/eslint-plugin-prettier/blob/master/CONTRIBUTING.md)
Loading

0 comments on commit 6de494f

Please sign in to comment.