Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix conflicts with arrow-body-style #379

Draft
wants to merge 3 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
67 changes: 54 additions & 13 deletions eslint-plugin-prettier.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,22 +37,42 @@ let prettier;
* @param {import('prettier-linter-helpers').Difference} difference - The difference object.
* @returns {void}
*/
function reportDifference(context, difference) {
function reportDifference(context, difference, fixAll) {
const { operation, offset, deleteText = '', insertText = '' } = difference;
const range = [offset, offset + deleteText.length];
const [start, end] = range.map(index =>
context.getSourceCode().getLocFromIndex(index)
);

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

if (fixAll) {
problem.data = {
...data,
extraMessage:
'(Apply fix in editor will fix all prettier/prettier errors)'
};
problem.fix = fixAll;
problem.suggest = [
{
messageId: operation,
data,
fix
}
];
}

context.report(problem);
}

// ------------------------------------------------------------------------------
Expand Down Expand Up @@ -94,22 +114,29 @@ module.exports = {
type: 'object',
properties: {},
additionalProperties: true
},
experimentalFix: {
type: 'boolean',
default: false
}
},
additionalProperties: true
}
],
messages: {
[INSERT]: 'Insert `{{ insertText }}`',
[DELETE]: 'Delete `{{ deleteText }}`',
[REPLACE]: 'Replace `{{ deleteText }}` with `{{ insertText }}`'
[INSERT]: 'Insert `{{ insertText }}`{{extraMessage}}',
[DELETE]: 'Delete `{{ deleteText }}`{{extraMessage}}',
[REPLACE]:
'Replace `{{ deleteText }}` with `{{ insertText }}`{{extraMessage}}'
}
},
create(context) {
const usePrettierrc =
!context.options[1] || context.options[1].usePrettierrc !== false;
const eslintFileInfoOptions =
(context.options[1] && context.options[1].fileInfoOptions) || {};
const experimentalFix =
context.options[1] && context.options[1].experimentalFix === true;
const sourceCode = context.getSourceCode();
const filepath = context.getFilename();
const source = sourceCode.text;
Expand Down Expand Up @@ -220,11 +247,25 @@ module.exports = {
return;
}

let fix;
if (experimentalFix) {
let result;
fix = fixer => {
if (!result) {
result = fixer.replaceTextRange(
[0, source.length],
prettierSource
);
}
return result;
};
}

if (source !== prettierSource) {
const differences = generateDifferences(source, prettierSource);

for (const difference of differences) {
reportDifference(context, difference);
reportDifference(context, difference, fix);
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@
}
},
"engines": {
"node": ">=6.0.0"
"node": ">=8.3.0"
},
"license": "MIT"
}
39 changes: 39 additions & 0 deletions test/invalid/19.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
CODE:
a = 1.0000;
b = 2.00000000;

OUTPUT:
a = 1.0;
b = 2.0;

OPTIONS:
[{}, {experimentalFix: true}]

ERRORS:
[
{
message: 'Delete `000`(Apply fix in editor will fix all prettier/prettier errors)',
line: 1, column: 8, endLine: 1, endColumn: 11,
suggestions: [
{
desc: 'Delete `000`',
output: 'a = 1.0;\nb = 2.00000000;\n'
}
]
},
{
message: 'Delete `0000000`(Apply fix in editor will fix all prettier/prettier errors)',
line: 2, column: 7, endLine: 2, endColumn: 14,
suggestions: [
{
desc: 'Delete `0000000`',
output: 'a = 1.0000;\nb = 2.0;\n'
}
]
},
]


FILENAME:
experimental-fix.js

61 changes: 59 additions & 2 deletions test/prettier.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,13 @@

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

const eslintPluginPrettier = require('..');

const rule = eslintPluginPrettier.rules.prettier;
const RuleTester = require('eslint').RuleTester;
const eslint = require('eslint');
const RuleTester = eslint.RuleTester;

// ------------------------------------------------------------------------------
// Tests
Expand Down Expand Up @@ -87,7 +89,8 @@ ruleTester.run('prettier', rule, {
'15',
'16',
'17',
'18'
'18',
'19'
].map(loadInvalidFixture)
});

Expand All @@ -112,6 +115,60 @@ vueRuleTester.run('prettier', rule, {
]
});

it('Should fix conflicts with `arrow-body-style` rule', () => {
const linter = new eslint.Linter();
linter.defineRules({
'arrow-body-style': require('eslint/lib/rules/arrow-body-style'),
'prettier/prettier': rule
});
const code = `
const foo = a => a.map(b => {
return b.map(
c => {
return {};
}
);
});
`;
const output =
`
const foo = a =>
a.map(b =>
b.map(c => ({})
);
`.trim() + '\n';
const outputWithExperimentalFix =
`
const foo = a => a.map(b => b.map(c => ({})));
`.trim() + '\n';
const [result, resultWithExperimentalFix] = [false, true].map(
experimentalFix =>
linter.verifyAndFix(code, {
parserOptions: { ecmaVersion: 2015 },
rules: {
'arrow-body-style': ['error'],
'prettier/prettier': ['error', {}, { experimentalFix }]
}
})
);
assert.strictEqual(result.output, output);
assert.deepStrictEqual(result.messages, [
{
column: 4,
fatal: true,
line: 4,
message: 'Parsing error: Unexpected token ;',
ruleId: null,
severity: 2
}
]);
assert.strictEqual(
resultWithExperimentalFix.output,
outputWithExperimentalFix
);
assert.deepStrictEqual(resultWithExperimentalFix.messages, []);
});

// ------------------------------------------------------------------------------
// Helpers
// ------------------------------------------------------------------------------
Expand Down