Socket
Socket
Sign inDemoInstall

eslint-plugin-prettier

Package Overview
Dependencies
94
Maintainers
6
Versions
40
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

Comparing version 4.2.1 to 5.0.0-alpha.0

eslint-plugin-prettier.d.ts

189

eslint-plugin-prettier.js

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

// @ts-check
/**
* @typedef {import('eslint').AST.Range} Range
* @typedef {import('eslint').AST.SourceLocation} SourceLocation
* @typedef {import('eslint').ESLint.Plugin} Plugin
* @typedef {import('prettier').FileInfoOptions} FileInfoOptions
* @typedef {import('prettier').Options & { onDiskFilepath: string, parserPath: string, usePrettierrc?: boolean }} Options
*/
'use strict';

@@ -30,5 +40,5 @@

/**
* @type {import('prettier')}
* @type {(source: string, options: Options, fileInfoOptions: FileInfoOptions) => string}
*/
let prettier;
let prettierFormat;

@@ -48,3 +58,3 @@ // ------------------------------------------------------------------------------

const { operation, offset, deleteText = '', insertText = '' } = difference;
const range = [offset, offset + deleteText.length];
const range = /** @type {Range} */ ([offset, offset + deleteText.length]);
const [start, end] = range.map(index =>

@@ -69,3 +79,6 @@ context.getSourceCode().getLocFromIndex(index),

module.exports = {
/**
* @type {Plugin}
*/
const eslintPluginPrettier = {
configs: {

@@ -119,3 +132,6 @@ recommended: {

!context.options[1] || context.options[1].usePrettierrc !== false;
const eslintFileInfoOptions =
/**
* @type {FileInfoOptions}
*/
const fileInfoOptions =
(context.options[1] && context.options[1].fileInfoOptions) || {};

@@ -133,130 +149,15 @@ const sourceCode = context.getSourceCode();

return {
// eslint-disable-next-line sonarjs/cognitive-complexity
Program() {
if (!prettier) {
if (!prettierFormat) {
// Prettier is expensive to load, so only load it if needed.
prettier = require('prettier');
prettierFormat = require('synckit').createSyncFn(
require.resolve('./worker'),
);
}
/**
* @type {{}}
*/
const eslintPrettierOptions = context.options[0] || {};
const prettierRcOptions = usePrettierrc
? prettier.resolveConfig.sync(onDiskFilepath, {
editorconfig: true,
})
: null;
const { ignored, inferredParser } = prettier.getFileInfo.sync(
onDiskFilepath,
{
resolveConfig: false,
withNodeModules: false,
ignorePath: '.prettierignore',
plugins: prettierRcOptions ? prettierRcOptions.plugins : null,
...eslintFileInfoOptions,
},
);
// Skip if file is ignored using a .prettierignore file
if (ignored) {
return;
}
const initialOptions = {};
// ESLint supports processors that let you extract and lint JS
// fragments within a non-JS language. In the cases where prettier
// supports the same language as a processor, we want to process
// the provided source code as javascript (as ESLint provides the
// rules with fragments of JS) instead of guessing the parser
// based off the filename. Otherwise, for instance, on a .md file we
// end up trying to run prettier over a fragment of JS using the
// markdown parser, which throws an error.
// Processors may set virtual filenames for these extracted blocks.
// If they do so then we want to trust the file extension they
// provide, and no override is needed.
// If the processor does not set any virtual filename (signified by
// `filepath` and `onDiskFilepath` being equal) AND we can't
// infer the parser from the filename, either because no filename
// was provided or because there is no parser found for the
// filename, use javascript.
// This is added to the options first, so that
// prettierRcOptions and eslintPrettierOptions can still override
// the parser.
//
// `parserBlocklist` should contain the list of prettier parser
// names for file types where:
// * Prettier supports parsing the file type
// * There is an ESLint processor that extracts JavaScript snippets
// from the file type.
if (filepath === onDiskFilepath) {
// The following list means the plugin process source into js content
// but with same filename, so we need to change the parser to `babel`
// by default.
// Related ESLint plugins are:
// 1. `eslint-plugin-graphql` (replacement: `@graphql-eslint/eslint-plugin`)
// 2. `eslint-plugin-html`
// 3. `eslint-plugin-markdown@1` (replacement: `eslint-plugin-markdown@2+`)
// 4. `eslint-plugin-svelte3` (replacement: `eslint-plugin-svelte@2+`)
const parserBlocklist = [null, 'markdown', 'html'];
let inferParserToBabel = parserBlocklist.includes(inferredParser);
switch (inferredParser) {
// it could be processed by `@graphql-eslint/eslint-plugin` or `eslint-plugin-graphql`
case 'graphql': {
if (
// for `eslint-plugin-graphql`, see https://github.com/apollographql/eslint-plugin-graphql/blob/master/src/index.js#L416
source.startsWith('ESLintPluginGraphQLFile`')
) {
inferParserToBabel = true;
}
break;
}
// it could be processed by `@ota-meshi/eslint-plugin-svelte`, `eslint-plugin-svelte` or `eslint-plugin-svelte3`
case 'svelte': {
// The `source` would be modified by `eslint-plugin-svelte3`
if (!context.parserPath.includes('svelte-eslint-parser')) {
// We do not support `eslint-plugin-svelte3`,
// the users should run `prettier` on `.svelte` files manually
return;
}
}
}
if (inferParserToBabel) {
initialOptions.parser = 'babel';
}
} else {
// Similar to https://github.com/prettier/stylelint-prettier/pull/22
// In all of the following cases ESLint extracts a part of a file to
// be formatted and there exists a prettier parser for the whole file.
// If you're interested in prettier you'll want a fully formatted file so
// you're about to run prettier over the whole file anyway.
// Therefore running prettier over just the style section is wasteful, so
// skip it.
const parserBlocklist = [
'babel',
'babylon',
'flow',
'typescript',
'vue',
'markdown',
'html',
'mdx',
'angular',
'svelte',
];
if (parserBlocklist.includes(inferredParser)) {
return;
}
}
const prettierOptions = {
...initialOptions,
...prettierRcOptions,
...eslintPrettierOptions,
filepath,
};
// prettier.format() may throw a SyntaxError if it cannot parse the

@@ -270,5 +171,18 @@ // source code it is given. Usually for JS files this isn't a

// point at which parsing failed.
/**
* @type {string}
*/
let prettierSource;
try {
prettierSource = prettier.format(source, prettierOptions);
prettierSource = prettierFormat(
source,
{
...eslintPrettierOptions,
filepath,
onDiskFilepath,
parserPath: context.parserPath,
usePrettierrc,
},
fileInfoOptions,
);
} catch (err) {

@@ -281,2 +195,7 @@ if (!(err instanceof SyntaxError)) {

const error =
/** @type {SyntaxError & {codeFrame: string; loc: SourceLocation}} */ (
err
);
// Prettier's message contains a codeframe style preview of the

@@ -286,10 +205,10 @@ // invalid code and the line/column at which the error occurred.

// remove them from the message
if (err.codeFrame) {
message = message.replace(`\n${err.codeFrame}`, '');
if (error.codeFrame) {
message = message.replace(`\n${error.codeFrame}`, '');
}
if (err.loc) {
if (error.loc) {
message = message.replace(/ \(\d+:\d+\)$/, '');
}
context.report({ message, loc: err.loc });
context.report({ message, loc: error.loc });

@@ -299,2 +218,6 @@ return;

if (prettierSource == null) {
return;
}
if (source !== prettierSource) {

@@ -313,1 +236,3 @@ const differences = generateDifferences(source, prettierSource);

};
module.exports = eslintPluginPrettier;
{
"name": "eslint-plugin-prettier",
"version": "4.2.1",
"version": "5.0.0-alpha.0",
"description": "Runs prettier as an eslint rule",

@@ -11,9 +11,14 @@ "repository": "git+https://github.com/prettier/eslint-plugin-prettier.git",

],
"funding": "https://opencollective.com/prettier",
"license": "MIT",
"packageManager": "yarn@1.22.19",
"engines": {
"node": ">=12.0.0"
"node": "^14.18.0 || >=16.0.0"
},
"main": "eslint-plugin-prettier.js",
"types": "eslint-plugin-prettier.d.ts",
"files": [
"eslint-plugin-prettier.js"
"eslint-plugin-prettier.d.ts",
"eslint-plugin-prettier.js",
"worker.js"
],

@@ -29,3 +34,3 @@ "keywords": [

"lint": "eslint . --cache -f friendly --max-warnings 10",
"prepare": "patch-package && simple-git-hooks && yarn-deduplicate --strategy fewer || exit 0",
"prepare": "simple-git-hooks && yarn-deduplicate --strategy fewer || exit 0",
"prerelease": "yarn format && yarn test",

@@ -36,6 +41,10 @@ "release": "changeset publish",

"peerDependencies": {
"eslint": ">=7.28.0",
"prettier": ">=2.0.0"
"@types/eslint": ">=8.0.0",
"eslint": ">=8.0.0",
"prettier": ">=3.0.0"
},
"peerDependenciesMeta": {
"@types/eslint": {
"optional": true
},
"eslint-config-prettier": {

@@ -49,27 +58,27 @@ "optional": true

"devDependencies": {
"@1stg/common-config": "~3.0.0",
"@1stg/eslint-config": "~3.0.0",
"@changesets/changelog-github": "^0.4.5",
"@changesets/cli": "^2.23.0",
"@graphql-eslint/eslint-plugin": "^2.5.0",
"@ota-meshi/eslint-plugin-svelte": "^0.34.1",
"@typescript-eslint/parser": "^5.29.0",
"@1stg/common-config": "^7.1.1",
"@changesets/changelog-github": "^0.4.6",
"@changesets/cli": "^2.24.4",
"@graphql-eslint/eslint-plugin": "^3.10.7",
"@types/eslint": "^8.4.6",
"@types/prettier": "^2.7.0",
"@types/prettier-linter-helpers": "^1.0.1",
"@typescript-eslint/parser": "^5.36.1",
"eslint-config-prettier": "^8.5.0",
"eslint-mdx": "^1.17.0",
"eslint-plugin-eslint-plugin": "^4.3.0",
"eslint-plugin-mdx": "^1.17.0",
"eslint-mdx": "^2.0.2",
"eslint-plugin-eslint-plugin": "^5.0.6",
"eslint-plugin-mdx": "^2.0.2",
"eslint-plugin-self": "^1.2.1",
"eslint-plugin-utils": "^0.1.0",
"graphql": "^16.5.0",
"mocha": "^9.2.2",
"patch-package": "^6.4.7",
"svelte": "^3.48.0",
"vue-eslint-parser": "^8.3.0"
"eslint-plugin-svelte": "^2.7.0",
"eslint-plugin-svelte3": "^4.0.0",
"graphql": "^16.6.0",
"mocha": "^10.0.0",
"svelte": "^3.49.0",
"vue-eslint-parser": "^9.0.3",
"yarn-deduplicate": "^6.0.0"
},
"resolutions": {
"@babel/traverse": "^7.18.5",
"eslint-plugin-prettier": "link:.",
"prettier": "^2.7.1"
},
"packageManager": "yarn@1.22.19"
"prettier": "^3.0.0-alpha.0"
}
}

@@ -100,4 +100,8 @@ # eslint-plugin-prettier [![Build Status](https://github.com/prettier/eslint-plugin-prettier/workflows/CI/badge.svg?branch=master)](https://github.com/prettier/eslint-plugin-prettier/actions?query=workflow%3ACI+branch%3Amaster)

We recommend to use [`eslint-plugin-svelte`](https://github.com/ota-meshi/eslint-plugin-svelte) instead of [`eslint-plugin-svelte3`](https://github.com/sveltejs/eslint-plugin-svelte3) because `eslint-plugin-svelte` has a correct [`eslint-svelte-parser`](https://github.com/ota-meshi/svelte-eslint-parser) instead of hacking, when use with `eslint-plugin-svelte3`, `eslint-plugin-prettier` will just ignore the text passed by `eslint-plugin-svelte3`, because the text they has been modified.
We recommend to use [`eslint-plugin-svelte`](https://github.com/ota-meshi/eslint-plugin-svelte) instead of [`eslint-plugin-svelte3`](https://github.com/sveltejs/eslint-plugin-svelte3) because `eslint-plugin-svelte` has a correct [`eslint-svelte-parser`](https://github.com/ota-meshi/svelte-eslint-parser) instead of hacking.
When use with `eslint-plugin-svelte3`, `eslint-plugin-prettier` will just ignore the text passed by `eslint-plugin-svelte3`, because the text has been modified.
If you still decide to use `eslint-plugin-svelte3`, you'll need to run `prettier --write *.svelte` manually.
## `arrow-body-style` and `prefer-arrow-callback` issue

@@ -123,3 +127,9 @@

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

@@ -134,3 +144,3 @@ ```

- `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.
- `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. And also, it is possible to run prettier without loading the prettierrc config file [via the CLI's --no-config option](https://prettier.io/docs/en/cli.html#--no-config) or through the API by [calling prettier.format() without passing through the options generated by calling resolveConfig](https://prettier.io/docs/en/api.html#prettierresolveconfigfilepath--options).

@@ -137,0 +147,0 @@ ```json

SocketSocket SOC 2 Logo

Product

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

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc