Socket
Socket
Sign inDemoInstall

eslint-plugin-prettier

Package Overview
Dependencies
Maintainers
2
Versions
41
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

eslint-plugin-prettier - npm Package Compare versions

Comparing version 2.7.0 to 3.0.0

16

CHANGELOG.md
# Changelog
## v3.0.0 (2018-10-01)
* Chore: Add eslint peer-dependency ([d55d79c](https://github.com/prettier/eslint-plugin-prettier/commit/d55d79c6a64f659f405788fc75f344704619979f))
* Breaking: Extract showInvisibles and generateDifferences ([bf7c40c](https://github.com/prettier/eslint-plugin-prettier/commit/bf7c40c240d9833548a7c9d210a28c90a4f3957b))
* Breaking: Defining prettier options must use an object ([478c7e5](https://github.com/prettier/eslint-plugin-prettier/commit/478c7e5d2165f3e67e893c9a317b602159eaff9c))
* Breaking: Drop support for ESLint v3 and v4 ([2326231](https://github.com/prettier/eslint-plugin-prettier/commit/232623179b16b99c0cf89ec9b8ed7660c69b092d))
* Chore: Update dependencies ([1ec94c8](https://github.com/prettier/eslint-plugin-prettier/commit/1ec94c8e3495f6964588da5264b890cb49616fff))
* Chore: remove two unused dependencies ([bfe459c](https://github.com/prettier/eslint-plugin-prettier/commit/bfe459c39b742115137e81278f03f8e6abfd7dcf))
* Chore: Rename test files to keep them sequential ([d38ea52](https://github.com/prettier/eslint-plugin-prettier/commit/d38ea52debdf9da718c60933f42a709fa05f550f))
* Breaking: Remove pragma support ([3af422c](https://github.com/prettier/eslint-plugin-prettier/commit/3af422c8e301978b611cfc665e052d48c102b443))
* Breaking: Update minimum required pretter version to 1.13.0 ([29c0506](https://github.com/prettier/eslint-plugin-prettier/commit/29c050605674fda2975b3b620c89a7eb9332641a))
* Breaking: Drop support for node v4, v7 and v9 ([be460bd](https://github.com/prettier/eslint-plugin-prettier/commit/be460bdd06fafb04442b440efabc7b36b12934a7))
* Chore: Add vscode config to autoformat on save ([9fac6b4](https://github.com/prettier/eslint-plugin-prettier/commit/9fac6b4039c1983b83073fa7af7864f0d7e1f2d3))
* Chore: Improve travis matrix ([46d2444](https://github.com/prettier/eslint-plugin-prettier/commit/46d244409e397ba9ff2dea621e99a4ea90e0585b))
* Chore: Add format script to run prettier ([d46aa6d](https://github.com/prettier/eslint-plugin-prettier/commit/d46aa6dbd8028802121231d3ae0fe3f837bca9ad))
## v2.7.0 (2018-09-26)

@@ -4,0 +20,0 @@

313

eslint-plugin-prettier.js

@@ -12,4 +12,6 @@ /**

const diff = require('fast-diff');
const docblock = require('jest-docblock');
const {
showInvisibles,
generateDifferences
} = require('prettier-linter-helpers');

@@ -20,17 +22,4 @@ // ------------------------------------------------------------------------------

// Preferred Facebook style.
const FB_PRETTIER_OPTIONS = {
singleQuote: true,
trailingComma: 'all',
bracketSpacing: false,
jsxBracketSameLine: true,
parser: 'flow'
};
const { INSERT, DELETE, REPLACE } = generateDifferences;
const LINE_ENDING_RE = /\r\n|[\r\n\u2028\u2029]/;
const OPERATION_INSERT = 'insert';
const OPERATION_DELETE = 'delete';
const OPERATION_REPLACE = 'replace';
// ------------------------------------------------------------------------------

@@ -44,178 +33,2 @@ // Privates

// ------------------------------------------------------------------------------
// Helpers
// ------------------------------------------------------------------------------
/**
* Gets the location of a given index in the source code for a given context.
* @param {RuleContext} context - The ESLint rule context.
* @param {number} index - An index in the source code.
* @returns {Object} An object containing numeric `line` and `column` keys.
*/
function getLocFromIndex(context, index) {
// If `sourceCode.getLocFromIndex` is available from ESLint, use it - added
// in ESLint 3.16.0.
const sourceCode = context.getSourceCode();
if (typeof sourceCode.getLocFromIndex === 'function') {
return sourceCode.getLocFromIndex(index);
}
const text = sourceCode.getText();
if (typeof index !== 'number') {
throw new TypeError('Expected `index` to be a number.');
}
if (index < 0 || index > text.length) {
throw new RangeError('Index out of range.');
}
// Loosely based on
// https://github.com/eslint/eslint/blob/18a519fa/lib/ast-utils.js#L408-L438
const lineEndingPattern = /\r\n|[\r\n\u2028\u2029]/g;
let offset = 0;
let line = 0;
let match;
while ((match = lineEndingPattern.exec(text))) {
const next = match.index + match[0].length;
if (index < next) {
break;
}
line++;
offset = next;
}
return {
line: line + 1,
column: index - offset
};
}
/**
* Converts invisible characters to a commonly recognizable visible form.
* @param {string} str - The string with invisibles to convert.
* @returns {string} The converted string.
*/
function showInvisibles(str) {
let ret = '';
for (let i = 0; i < str.length; i++) {
switch (str[i]) {
case ' ':
ret += '·'; // Middle Dot, \u00B7
break;
case '\n':
ret += '⏎'; // Return Symbol, \u23ce
break;
case '\t':
ret += '↹'; // Left Arrow To Bar Over Right Arrow To Bar, \u21b9
break;
case '\r':
ret += '␍'; // Carriage Return Symbol, \u240D
break;
default:
ret += str[i];
break;
}
}
return ret;
}
/**
* Generate results for differences between source code and formatted version.
* @param {string} source - The original source.
* @param {string} prettierSource - The Prettier formatted source.
* @returns {Array} - An array contains { operation, offset, insertText, deleteText }
*/
function generateDifferences(source, prettierSource) {
// fast-diff returns the differences between two texts as a series of
// INSERT, DELETE or EQUAL operations. The results occur only in these
// sequences:
// /-> INSERT -> EQUAL
// EQUAL | /-> EQUAL
// \-> DELETE |
// \-> INSERT -> EQUAL
// Instead of reporting issues at each INSERT or DELETE, certain sequences
// are batched together and are reported as a friendlier "replace" operation:
// - A DELETE immediately followed by an INSERT.
// - Any number of INSERTs and DELETEs where the joining EQUAL of one's end
// and another's beginning does not have line endings (i.e. issues that occur
// on contiguous lines).
const results = diff(source, prettierSource);
const differences = [];
const batch = [];
let offset = 0; // NOTE: INSERT never advances the offset.
while (results.length) {
const result = results.shift();
const op = result[0];
const text = result[1];
switch (op) {
case diff.INSERT:
case diff.DELETE:
batch.push(result);
break;
case diff.EQUAL:
if (results.length) {
if (batch.length) {
if (LINE_ENDING_RE.test(text)) {
flush();
offset += text.length;
} else {
batch.push(result);
}
} else {
offset += text.length;
}
}
break;
default:
throw new Error(`Unexpected fast-diff operation "${op}"`);
}
if (batch.length && !results.length) {
flush();
}
}
return differences;
function flush() {
let aheadDeleteText = '';
let aheadInsertText = '';
while (batch.length) {
const next = batch.shift();
const op = next[0];
const text = next[1];
switch (op) {
case diff.INSERT:
aheadInsertText += text;
break;
case diff.DELETE:
aheadDeleteText += text;
break;
case diff.EQUAL:
aheadDeleteText += text;
aheadInsertText += text;
break;
}
}
if (aheadDeleteText && aheadInsertText) {
differences.push({
offset,
operation: OPERATION_REPLACE,
insertText: aheadInsertText,
deleteText: aheadDeleteText
});
} else if (!aheadDeleteText && aheadInsertText) {
differences.push({
offset,
operation: OPERATION_INSERT,
insertText: aheadInsertText
});
} else if (aheadDeleteText && !aheadInsertText) {
differences.push({
offset,
operation: OPERATION_DELETE,
deleteText: aheadDeleteText
});
}
offset += aheadDeleteText.length;
}
}
// ------------------------------------------------------------------------------
// Rule Definition

@@ -232,3 +45,3 @@ // ------------------------------------------------------------------------------

function reportInsert(context, offset, text) {
const pos = getLocFromIndex(context, offset);
const pos = context.getSourceCode().getLocFromIndex(offset);
const range = [offset, offset];

@@ -253,4 +66,4 @@ context.report({

function reportDelete(context, offset, text) {
const start = getLocFromIndex(context, offset);
const end = getLocFromIndex(context, offset + text.length);
const start = context.getSourceCode().getLocFromIndex(offset);
const end = context.getSourceCode().getLocFromIndex(offset + text.length);
const range = [offset, offset + text.length];

@@ -277,4 +90,6 @@ context.report({

function reportReplace(context, offset, deleteText, insertText) {
const start = getLocFromIndex(context, offset);
const end = getLocFromIndex(context, offset + deleteText.length);
const start = context.getSourceCode().getLocFromIndex(offset);
const end = context
.getSourceCode()
.getLocFromIndex(offset + deleteText.length);
const range = [offset, offset + deleteText.length];

@@ -294,21 +109,2 @@ context.report({

/**
* Get the pragma from the ESLint rule context.
* @param {RuleContext} context - The ESLint rule context.
* @returns {string|null}
*/
function getPragma(context) {
const pluginOptions = context.options[1];
if (!pluginOptions) {
return null;
}
const pragmaRef =
typeof pluginOptions === 'string' ? pluginOptions : pluginOptions.pragma;
// Remove leading @
return pragmaRef ? pragmaRef.slice(1) : null;
}
// ------------------------------------------------------------------------------

@@ -319,4 +115,2 @@ // Module Definition

module.exports = {
showInvisibles,
generateDifferences,
configs: {

@@ -341,20 +135,12 @@ recommended: {

{
anyOf: [
{ enum: [null, 'fb'] },
{ type: 'object', properties: {}, additionalProperties: true }
]
type: 'object',
properties: {},
additionalProperties: true
},
{
anyOf: [
// Pragma:
{ type: 'string', pattern: '^@\\w+$' },
{
type: 'object',
properties: {
pragma: { type: 'string', pattern: '^@\\w+$' },
usePrettierrc: { type: 'boolean' }
},
additionalProperties: true
}
]
type: 'object',
properties: {
usePrettierrc: { type: 'boolean' }
},
additionalProperties: true
}

@@ -364,3 +150,2 @@ ]

create(context) {
const pragma = getPragma(context);
const usePrettierrc =

@@ -372,26 +157,2 @@ !context.options[1] || context.options[1].usePrettierrc !== false;

// The pragma is only valid if it is found in a block comment at the very
// start of the file.
if (pragma) {
// ESLint 3.x reports the shebang as a "Line" node, while ESLint 4.x
// reports it as a "Shebang" node. This works for both versions:
const hasShebang = source.startsWith('#!');
const allComments = sourceCode.getAllComments();
const firstComment = hasShebang ? allComments[1] : allComments[0];
if (
!(
firstComment &&
firstComment.type === 'Block' &&
firstComment.loc.start.line === (hasShebang ? 2 : 1) &&
firstComment.loc.start.column === 0
)
) {
return {};
}
const parsed = docblock.parse(firstComment.value);
if (parsed[pragma] !== '') {
return {};
}
}
if (prettier && prettier.clearConfigCache) {

@@ -408,23 +169,13 @@ prettier.clearConfigCache();

const eslintPrettierOptions =
context.options[0] === 'fb'
? FB_PRETTIER_OPTIONS
: context.options[0];
const eslintPrettierOptions = context.options[0] || {};
const prettierRcOptions =
usePrettierrc &&
prettier.resolveConfig &&
prettier.resolveConfig.sync
? prettier.resolveConfig.sync(filepath, {
editorconfig: true
})
: null;
const prettierRcOptions = usePrettierrc
? prettier.resolveConfig.sync(filepath, {
editorconfig: true
})
: null;
// prettier.getFileInfo was added in v1.13
const prettierFileInfo =
prettier.getFileInfo && prettier.getFileInfo.sync
? prettier.getFileInfo.sync(filepath, {
ignorePath: '.prettierignore'
})
: { ignored: false, inferredParser: null };
const prettierFileInfo = prettier.getFileInfo.sync(filepath, {
ignorePath: '.prettierignore'
});

@@ -479,3 +230,3 @@ // Skip if file is ignored using a .prettierignore file

switch (difference.operation) {
case OPERATION_INSERT:
case INSERT:
reportInsert(

@@ -487,3 +238,3 @@ context,

break;
case OPERATION_DELETE:
case DELETE:
reportDelete(

@@ -495,3 +246,3 @@ context,

break;
case OPERATION_REPLACE:
case REPLACE:
reportReplace(

@@ -498,0 +249,0 @@ context,

@@ -1,3 +0,2 @@

The MIT License (MIT)
=====================
# The MIT License (MIT)

@@ -4,0 +3,0 @@ Copyright © 2017 Andres Suarez and Teddy Katz

{
"name": "eslint-plugin-prettier",
"version": "2.7.0",
"version": "3.0.0",
"description": "Runs prettier as an eslint rule",

@@ -19,2 +19,3 @@ "keywords": [

"test": "npm run lint && mocha",
"format": "yarn run prettier '**/*.{js,json,md,yml}' --write && yarn run lint --fix",
"generate-release": "node-release-script"

@@ -31,26 +32,24 @@ },

"dependencies": {
"fast-diff": "^1.1.1",
"jest-docblock": "^21.0.0"
"prettier-linter-helpers": "^1.0.0"
},
"peerDependencies": {
"prettier": ">= 0.11.0"
"eslint": ">= 5.0.0",
"prettier": ">= 1.13.0"
},
"devDependencies": {
"@not-an-aardvark/node-release-script": "^0.1.0",
"eslint": "^3.14.1",
"eslint-config-not-an-aardvark": "^2.0.0",
"eslint-config-prettier": "^1.3.0",
"eslint-plugin-eslint-plugin": "^0.7.1",
"eslint-plugin-node": "^4.2.2",
"eslint-plugin-self": "^1.0.1",
"mocha": "^3.1.2",
"moment": "^2.18.1",
"eslint": "^5.6.0",
"eslint-config-not-an-aardvark": "^2.1.0",
"eslint-config-prettier": "^3.1.0",
"eslint-plugin-eslint-plugin": "^1.4.0",
"eslint-plugin-node": "^7.0.1",
"eslint-plugin-self": "^1.1.0",
"mocha": "^5.2.0",
"prettier": "^1.13.0",
"semver": "^5.3.0",
"vue-eslint-parser": "^2.0.2"
"vue-eslint-parser": "^3.2.2"
},
"engines": {
"node": ">=4.0.0"
"node": ">=6.0.0"
},
"license": "MIT"
}

@@ -46,5 +46,3 @@ # eslint-plugin-prettier [![Build Status](https://travis-ci.org/prettier/eslint-plugin-prettier.svg?branch=master)](https://travis-ci.org/prettier/eslint-plugin-prettier)

{
"plugins": [
"prettier"
],
"plugins": ["prettier"],
"rules": {

@@ -66,21 +64,19 @@ "prettier/prettier": "error"

```sh
npm install --save-dev eslint-config-prettier
```
```sh
npm install --save-dev eslint-config-prettier
```
2. Then you need to add `plugin:prettier/recommended` as the last extension in your `.eslintrc.json`:
```json
{
"extends": [
"plugin:prettier/recommended"
]
}
```
```json
{
"extends": ["plugin:prettier/recommended"]
}
```
This does three things:
* Enables `eslint-plugin-prettier`.
* Sets the `prettier/prettier` rule to `"error"`.
* Extends the `eslint-config-prettier` configuration.
- Enables `eslint-plugin-prettier`.
- Sets the `prettier/prettier` rule to `"error"`.
- Extends the `eslint-config-prettier` configuration.

@@ -108,70 +104,20 @@ You can then set Prettier's own options inside a `.prettierrc` file.

* The first option:
- Objects are passed directly to Prettier as [options](https://prettier.io/docs/en/options.html). Example:
```json
"prettier/prettier": ["error", {"singleQuote": true, "parser": "flow"}]
```
- The first option:
- Or the string `"fb"` may be used to set "Facebook style" defaults:
- An object representing [options](https://prettier.io/docs/en/options.html) that will be passed into prettier. Example:
```json
"prettier/prettier": ["error", "fb"]
"prettier/prettier": ["error", {"singleQuote": true, "parser": "flow"}]
```
Equivalent to:
NB: This option will merge and override any config set with `.prettierrc` files
```json
"prettier/prettier": ["error", {
"singleQuote": true,
"trailingComma": "all",
"bracketSpacing": false,
"jsxBracketSameLine": true,
"parser": "flow"
}]
```
NB: This option will merge and override any config set with `.prettierrc` files (for Prettier < 1.7.0, [config files are ignored](https://github.com/prettier/eslint-plugin-prettier/issues/46))
- The second option:
* The second option:
- An object with the following options
- 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:
- `usePrettierrc`: Enables loading of the Prettier configuration file, (default: `true`). May be useful if you are using multiple tools that conflict with each other, or do not wish to mix your ESLint settings with your Prettier configuration.
```json
"prettier/prettier": ["error", null, "@prettier"]
```
Only files with `@prettier` in the heading docblock will be checked:
```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`._
- An object with the following options
- `pragma`: Also sets the aforementioned `pragma`: 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 `@`.
```json
"prettier/prettier": ["error", null, {
"pragma": "@prettier"
}]
```
- `usePrettierrc`: Enables loading of the Prettier configuration file, (default: `true`). May be useful if you are using multiple tools that conflict with each other, or do not wish to mix your ESLint settings with your Prettier configuration.
```json
"prettier/prettier": ["error", null, {
"prettier/prettier": ["error", {}, {
"usePrettierrc": false

@@ -181,3 +127,3 @@ }]

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

@@ -184,0 +130,0 @@ ---

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc