Skip to content
Permalink

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also or learn more about diff comparisons.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also . Learn more about diff comparisons here.
base repository: prettier/eslint-plugin-prettier
Failed to load repositories. Confirm that selected base ref is valid, then try again.
Loading
base: e53f23b890c9c6e73ba6e58db0d05eb91d962b71
Choose a base ref
...
head repository: prettier/eslint-plugin-prettier
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: v4.0.0
Choose a head ref
  • 4 commits
  • 13 files changed
  • 3 contributors

Commits on Aug 21, 2021

  1. Unverified

    This user has not yet uploaded their public signing key.
    Copy the full SHA
    402a0b8 View commit details

Commits on Aug 30, 2021

  1. v4 - Drop support for eslint 5/6, prettier 1, node 6/8 (#429)

    * Update required versions of eslint and prettier
    
    Now requires node 12, eslint >7.28.0 and prettier > 2.0.0
    
    - prettier v2 changed some defaults, so tests needed to be slightly
      tweaked
    - dropping support for old eslint/node means we remove them from CI test
      matrixes and always run the graphql plugin tests
    
    * Enable trailing commas in prettier
    
    * Rework graphql test
    
    - No need to check if the depenency is installed anymore
    - Move it into its own ruletester
    - Add invalid test
    
    * Remove check that is unneeded now we only support prettier v2
    
    * Use getPhysicalFilename
    BPScott authored Aug 30, 2021

    Unverified

    This user has not yet uploaded their public signing key.
    Copy the full SHA
    acb56f3 View commit details
  2. changelog

    BPScott committed Aug 30, 2021
    Copy the full SHA
    9d386f2 View commit details
  3. v4.0.0

    BPScott committed Aug 30, 2021
    Copy the full SHA
    a3d6a22 View commit details
Showing with 1,096 additions and 186 deletions.
  1. +21 −0 .editorconfig
  2. +3 −3 .eslintrc.js
  3. +2 −24 .github/workflows/ci.yml
  4. +8 −0 CHANGELOG.md
  5. +22 −55 eslint-plugin-prettier.js
  6. +7 −5 package.json
  7. +2 −2 test/invalid/10.txt
  8. +3 −3 test/invalid/11-a.txt
  9. +3 −3 test/invalid/11-b.txt
  10. +20 −0 test/invalid/graphql.txt
  11. +62 −57 test/prettier.js
  12. +0 −1 test/prettierrc/no-semi/.prettierrc
  13. +943 −33 yarn.lock
21 changes: 21 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# editorconfig.org
root = true

[*]
charset = utf-8
end_of_line = lf
indent_size = 2
indent_style = space
insert_final_newline = true
trim_trailing_whitespace = true

# Markdown syntax specifies that trailing whitespaces can be meaningful,
# so let’s not trim those. e.g. 2 trailing spaces = linebreak (<br />)
# See https://daringfireball.net/projects/markdown/syntax#p
[*.md]
trim_trailing_whitespace = false

# Disable trailing whitespace removal in diff files,
# where whitespace is meaningful
[*.diff]
trim_trailing_whitespace = false
6 changes: 3 additions & 3 deletions .eslintrc.js
Original file line number Diff line number Diff line change
@@ -6,12 +6,12 @@ module.exports = {
'not-an-aardvark/node',
'plugin:node/recommended',
'plugin:eslint-plugin/recommended',
'prettier'
'prettier',
],
env: { mocha: true },
root: true,
rules: {
'self/prettier': ['error'],
'eslint-plugin/report-message-format': ['error', '^[^a-z].*\\.$']
}
'eslint-plugin/report-message-format': ['error', '^[^a-z].*\\.$'],
},
};
26 changes: 2 additions & 24 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -11,27 +11,8 @@ jobs:
runs-on: ubuntu-latest
strategy:
matrix:
eslint-version: [7.x, 6.x, 5.x]
node-version: [16.x, 14.x, 12.x, 10.x, 8.x, 6.x]
test-graphql: [true, false]

exclude:
# eslint 7 does not support node 6 or 8
- eslint-version: 7.x
node-version: 8.x
- eslint-version: 7.x
node-version: 6.x
# eslint 6 does not support node 6
# the version of chalk used in eslint 6 does not support node 8
- eslint-version: 6.x
node-version: 8.x
- eslint-version: 6.x
node-version: 6.x
# the version of graphql-config used in @graphql-eslint/eslint-plugin does not support node 8
- test-graphql: true
node-version: 8.x
- test-graphql: true
node-version: 6.x
eslint-version: [7.x]
node-version: [16.x, 14.x, 12.x]

steps:
- uses: actions/checkout@v2
@@ -47,8 +28,5 @@ jobs:
- name: Install
run: yarn install

- if: matrix.test-graphql == true
run: yarn add -D @graphql-eslint/eslint-plugin graphql

- name: Test
run: yarn run test
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
# Changelog

## v4.0.0 (2021-08-30)

This breaking change drops support for old versions of ESLint, Prettier and
Node. You must use at least ESLint v7.28.0, Prettier v2.0.0 and Node v12.0.0.
Aside from that, usage of this plugin remains identical.

* v4 - Drop support for eslint 5/6, prettier 1, node 6/8 ([#429](git@github.com:prettier/eslint-plugin-prettier/issues/429)) ([acb56f3](git@github.com:prettier/eslint-plugin-prettier/commit/acb56f3b2891b2a6998a75a7d4406183d452ba16))

## v3.4.1 (2021-08-20)

* build(deps): Bump glob-parent from 5.0.0 to 5.1.2 ([#420](git@github.com:prettier/eslint-plugin-prettier/issues/420)) ([b6d075c](git@github.com:prettier/eslint-plugin-prettier/commit/b6d075cf7111468e8af4161c306c7f37f09f220e))
77 changes: 22 additions & 55 deletions eslint-plugin-prettier.js
Original file line number Diff line number Diff line change
@@ -9,12 +9,9 @@
// Requirements
// ------------------------------------------------------------------------------

const fs = require('fs');
const path = require('path');

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

// ------------------------------------------------------------------------------
@@ -46,43 +43,21 @@ let prettier;
function reportDifference(context, difference) {
const { operation, offset, deleteText = '', insertText = '' } = difference;
const range = [offset, offset + deleteText.length];
const [start, end] = range.map(index =>
const [start, end] = range.map((index) =>
context.getSourceCode().getLocFromIndex(index)
);

context.report({
messageId: operation,
data: {
deleteText: showInvisibles(deleteText),
insertText: showInvisibles(insertText)
insertText: showInvisibles(insertText),
},
loc: { start, end },
fix: fixer => fixer.replaceTextRange(range, insertText)
fix: (fixer) => fixer.replaceTextRange(range, insertText),
});
}

/**
* Given a filepath, get the nearest path that is a regular file.
* The filepath provided by eslint may be a virtual filepath rather than a file
* on disk. This attempts to transform a virtual path into an on-disk path
* @param {string} filepath
* @returns {string}
*/
function getOnDiskFilepath(filepath) {
try {
if (fs.statSync(filepath).isFile()) {
return filepath;
}
} catch (err) {
// https://github.com/eslint/eslint/issues/11989
if (err.code === 'ENOTDIR') {
return getOnDiskFilepath(path.dirname(filepath));
}
}

return filepath;
}

// ------------------------------------------------------------------------------
// Module Definition
// ------------------------------------------------------------------------------
@@ -95,15 +70,15 @@ module.exports = {
rules: {
'prettier/prettier': 'error',
'arrow-body-style': 'off',
'prefer-arrow-callback': 'off'
}
}
'prefer-arrow-callback': 'off',
},
},
},
rules: {
prettier: {
meta: {
docs: {
url: 'https://github.com/prettier/eslint-plugin-prettier#options'
url: 'https://github.com/prettier/eslint-plugin-prettier#options',
},
type: 'layout',
fixable: 'code',
@@ -112,7 +87,7 @@ module.exports = {
{
type: 'object',
properties: {},
additionalProperties: true
additionalProperties: true,
},
{
type: 'object',
@@ -121,17 +96,17 @@ module.exports = {
fileInfoOptions: {
type: 'object',
properties: {},
additionalProperties: true
}
additionalProperties: true,
},
},
additionalProperties: true
}
additionalProperties: true,
},
],
messages: {
[INSERT]: 'Insert `{{ insertText }}`',
[DELETE]: 'Delete `{{ deleteText }}`',
[REPLACE]: 'Replace `{{ deleteText }}` with `{{ insertText }}`'
}
[REPLACE]: 'Replace `{{ deleteText }}` with `{{ insertText }}`',
},
},
create(context) {
const usePrettierrc =
@@ -145,9 +120,7 @@ module.exports = {
// file paths. If this is the case then we need to resolve prettier
// config and file info using the on-disk path instead of the virtual
// path.
// See https://github.com/eslint/eslint/issues/11989 for ideas around
// being able to get this value directly from eslint in the future.
const onDiskFilepath = getOnDiskFilepath(filepath);
const onDiskFilepath = context.getPhysicalFilename();
const source = sourceCode.text;

return {
@@ -161,7 +134,7 @@ module.exports = {

const prettierRcOptions = usePrettierrc
? prettier.resolveConfig.sync(onDiskFilepath, {
editorconfig: true
editorconfig: true,
})
: null;

@@ -221,13 +194,7 @@ module.exports = {
}

if (filepath === onDiskFilepath && inferParserToBabel) {
// Prettier v1.16.0 renamed the `babylon` parser to `babel`
// Use the modern name if available
const supportBabelParser = prettier
.getSupportInfo()
.languages.some(language => language.parsers.includes('babel'));

initialOptions.parser = supportBabelParser ? 'babel' : 'babylon';
initialOptions.parser = 'babel';
}

const prettierOptions = Object.assign(
@@ -279,9 +246,9 @@ module.exports = {
reportDifference(context, difference);
}
}
}
},
};
}
}
}
},
},
},
};
12 changes: 7 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "eslint-plugin-prettier",
"version": "3.4.1",
"version": "4.0.0",
"description": "Runs prettier as an eslint rule",
"keywords": [
"eslint",
@@ -31,19 +31,21 @@
"prettier-linter-helpers": "^1.0.0"
},
"peerDependencies": {
"eslint": ">=5.0.0",
"prettier": ">=1.13.0"
"eslint": ">=7.28.0",
"prettier": ">=2.0.0"
},
"devDependencies": {
"@graphql-eslint/eslint-plugin": "^2.0.1",
"@not-an-aardvark/node-release-script": "^0.1.0",
"eslint": "^7.0.0",
"eslint": "^7.28.0",
"eslint-config-not-an-aardvark": "^2.1.0",
"eslint-config-prettier": "^6.0.0",
"eslint-plugin-eslint-plugin": "^2.0.0",
"eslint-plugin-node": "^8.0.0",
"eslint-plugin-self": "^1.1.0",
"graphql": "^15.5.1",
"mocha": "^6.0.0",
"prettier": "^1.15.3",
"prettier": "^2.3.0",
"vue-eslint-parser": "^6.0.0"
},
"peerDependenciesMeta": {
4 changes: 2 additions & 2 deletions test/invalid/10.txt
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
CODE:
var a = {
b: 1
b: 1,
};

OUTPUT:
var a = {
b: 1
b: 1,
};

OPTIONS:
6 changes: 3 additions & 3 deletions test/invalid/11-a.txt
Original file line number Diff line number Diff line change
@@ -5,7 +5,7 @@ var a = {

OUTPUT:
var a = {
b: ""
b: "",
};

OPTIONS:
@@ -14,7 +14,7 @@ OPTIONS:
ERRORS:
[
{
message: 'Replace `\'\',` with `""`',
line: 2, column: 6, endLine: 2, endColumn: 9,
message: 'Replace `\'\'` with `""`',
line: 2, column: 6, endLine: 2, endColumn: 8,
},
]
6 changes: 3 additions & 3 deletions test/invalid/11-b.txt
Original file line number Diff line number Diff line change
@@ -5,7 +5,7 @@ var a = {

OUTPUT:
var a = {
b: ""
b: "",
};

OPTIONS:
@@ -14,7 +14,7 @@ OPTIONS:
ERRORS:
[
{
message: 'Replace `\'\',` with `""`',
line: 2, column: 6, endLine: 2, endColumn: 9,
message: 'Replace `\'\'` with `""`',
line: 2, column: 6, endLine: 2, endColumn: 8,
},
]
20 changes: 20 additions & 0 deletions test/invalid/graphql.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
CODE:
type Query {
foo: String!
}

OUTPUT:
type Query {
foo: String!
}

OPTIONS:
[]

ERRORS:
[
{
message: 'Insert `··`',
line: 2, column: 1, endLine: 2, endColumn: 1,
},
]
119 changes: 62 additions & 57 deletions test/prettier.js
Original file line number Diff line number Diff line change
@@ -26,85 +26,48 @@ const RuleTester = require('eslint').RuleTester;

const ruleTester = new RuleTester();

let graphqlEslintParserPath;

try {
graphqlEslintParserPath = require.resolve('@graphql-eslint/eslint-plugin');
} catch (e) {
// ignore
}

ruleTester.run('prettier', rule, {
valid: [
// Correct style.
// Correct style. Also proves that the plugin works if no filename is provided
{ code: `'';\n` },
// Double quote from .prettierrc.
{ code: '"";\n', filename: getPrettierRcJsFilename('double-quote') },
// Override .prettierrc from object option.
{
code: `var foo = {bar: 0};\n`,
filename: getPrettierRcJsFilename('bracket-spacing'),
options: [{ bracketSpacing: false }]
options: [{ bracketSpacing: false }],
},
// Only use options from plugin, skipping .prettierrc
{
code: `var foo = {bar: 0};\n`,
filename: getPrettierRcJsFilename('bracket-spacing'),
options: [{ bracketSpacing: false }, { usePrettierrc: false }]
options: [{ bracketSpacing: false }, { usePrettierrc: false }],
},
// Ignores filenames in .prettierignore
{
code: `("");\n`,
filename: getPrettierRcJsFilename('single-quote', 'ignore-me.js')
filename: getPrettierRcJsFilename('single-quote', 'ignore-me.js'),
},
// Sets a default parser when it can't be inferred from the file extensions
{
code: `('');\n`,
filename: getPrettierRcJsFilename('single-quote', 'dummy.qqq')
filename: getPrettierRcJsFilename('single-quote', 'dummy.qqq'),
},
// Overwrites the parser for file extensions prettier would try to format
// with not the babylon parser
// In the real world, eslint-plugin-markdown would transform file contents
// into JS snippets that would get passed to ESLint
{
code: `('');\n`,
filename: getPrettierRcJsFilename('single-quote', 'dummy.md')
filename: getPrettierRcJsFilename('single-quote', 'dummy.md'),
},
// Should ignore files from node_modules
{
code: 'a();;;;;;\n',
filename: 'node_modules/dummy.js'
filename: 'node_modules/dummy.js',
},
// ESLint processors can provide virtual filenames. E.g. fenced code blocks
// in a markdown file may be processed with the filenames
// `a-markdown-file.md/1.js` / `a-markdown-file.md/2.js`
// If we try and pass those filenames into prettier's `resolveConfig` and
// `getFileInfo` methods they throw up because the it doesn't like treating
// `markdown-file.md` as a directory.
// Make sure we handle that case internally so this does not crash
{
code: `('');\n`,
filename: path.join(__filename, '0_fake_virtual_name.js')
},
{
code: 'ESLintPluginGraphQLFile`type Query {\n foo: String!\n}`\n',
filename: getPrettierRcJsFilename('no-semi', 'dummy.graphql'),
parserOptions: {
ecmaVersion: 2015
}
}
].concat(
graphqlEslintParserPath
? {
code: `type Query {
foo: String!
}
`,
filename: 'valid.graphql',
parser: graphqlEslintParserPath
}
: []
),
],
invalid: [
'01',
'02',
@@ -124,29 +87,71 @@ ruleTester.run('prettier', rule, {
'15',
'16',
'17',
'18'
].map(loadInvalidFixture)
'18',
].map(loadInvalidFixture),
});

const vueRuleTester = new RuleTester({
parser: require.resolve('vue-eslint-parser')
parser: require.resolve('vue-eslint-parser'),
});

vueRuleTester.run('prettier', rule, {
vueRuleTester.run('vue', rule, {
valid: [
{
code: `<template>\n <div>HI</div>\n</template>\n<script>\n3;\n</script>\n`,
filename: 'valid.vue'
}
filename: 'valid.vue',
},
],
invalid: [
Object.assign(loadInvalidFixture('vue'), {
filename: 'invalid.vue'
filename: 'invalid.vue',
}),
Object.assign(loadInvalidFixture('vue-syntax-error'), {
filename: 'syntax-error.vue'
})
]
filename: 'syntax-error.vue',
}),
],
});

const atGraphqlEslintRuleTester = new RuleTester({
parser: require.resolve('@graphql-eslint/eslint-plugin'),
});

atGraphqlEslintRuleTester.run('@graphql-eslint/eslint-plugin', rule, {
valid: [
{
code: `type Query {\n foo: String!\n}\n`,
filename: 'valid.graphql',
},
],
invalid: [
Object.assign(loadInvalidFixture('graphql'), {
filename: 'invalid.graphql',
}),
],
});

// eslint-plugin-graphql handles literal graphql files by tranforming graphql
// code with a processor, instead of using a parser. Unfortunatly we cant
// specify custom processors in a RuleTester, so instead we have write test code
// that is the result of eslint-plugin-graphql's processing - this is the
// ESLintPluginGraphQLFile tagged template literal. See
// https://github.com/apollographql/eslint-plugin-graphql/blob/c465fedc8fea239ee1731ad4ec3ee1183a3cdddf/src/index.js#L404
// In the future if ESLint supports processors (https://github.com/eslint/rfcs/pull/31)
// we should be define a RuleTester like
// `newRuleTester({processor: require('eslint-plugin-graphql').processor['.graphql']})
// and then pass in pure graphql into the code value.
const eslintPluginGraphqlRuleTester = new RuleTester({
parserOptions: { ecmaVersion: 2015 },
});

eslintPluginGraphqlRuleTester.run('eslint-plugin-graphql', rule, {
valid: [
{
code: 'ESLintPluginGraphQLFile`type Query {\n foo: String!\n}`\n',
filename: getPrettierRcJsFilename('no-semi', 'dummy.graphql'),
},
],
invalid: [],
});

// ------------------------------------------------------------------------------
@@ -166,13 +171,13 @@ function loadInvalidFixture(name) {
const src = fs.readFileSync(filename, 'utf8');
const sections = src
.split(/^[A-Z]+:\n/m)
.map(x => x.replace(/(?=\n)\n$/, ''));
.map((x) => x.replace(/(?=\n)\n$/, ''));
const item = {
code: sections[1],
output: sections[2],
options: eval(sections[3]), // eslint-disable-line no-eval
errors: eval(sections[4]), // eslint-disable-line no-eval
filename: getPrettierRcJsFilename('double-quote', name + '.txt')
filename: getPrettierRcJsFilename('double-quote', name + '.txt'),
};
if (sections.length >= 6) {
item.filename = sections[5];
1 change: 0 additions & 1 deletion test/prettierrc/no-semi/.prettierrc
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
{
"semi": false
}

976 changes: 943 additions & 33 deletions yarn.lock

Large diffs are not rendered by default.