lint: Add yoda rule with range exception (#2433)
This required a small refactoring, but the code is less nested as a result. See discussion at https://github.com/prettier/prettier/pull/2401#discussion_r125750068 http://eslint.org/docs/rules/yoda#exceptrangemaster
parent
1b4846b67d
commit
0fb332b2f9
|
@ -25,7 +25,8 @@ module.exports = {
|
|||
"prettier/prettier": "error",
|
||||
"react/no-deprecated": "off",
|
||||
strict: "error",
|
||||
"symbol-description": "error"
|
||||
"symbol-description": "error",
|
||||
yoda: ["error", "never", { exceptRange: true }]
|
||||
},
|
||||
parserOptions: {
|
||||
ecmaFeatures: {
|
||||
|
|
66
index.js
66
index.js
|
@ -282,39 +282,41 @@ function calculateRange(text, opts, ast) {
|
|||
}
|
||||
|
||||
function formatRange(text, opts, ast) {
|
||||
if (0 < opts.rangeStart || opts.rangeEnd < text.length) {
|
||||
const range = calculateRange(text, opts, ast);
|
||||
const rangeStart = range.rangeStart;
|
||||
const rangeEnd = range.rangeEnd;
|
||||
const rangeString = text.slice(rangeStart, rangeEnd);
|
||||
|
||||
// Try to extend the range backwards to the beginning of the line.
|
||||
// This is so we can detect indentation correctly and restore it.
|
||||
// Use `Math.min` since `lastIndexOf` returns 0 when `rangeStart` is 0
|
||||
const rangeStart2 = Math.min(
|
||||
rangeStart,
|
||||
text.lastIndexOf("\n", rangeStart) + 1
|
||||
);
|
||||
const indentString = text.slice(rangeStart2, rangeStart);
|
||||
|
||||
const alignmentSize = util.getAlignmentSize(indentString, opts.tabWidth);
|
||||
|
||||
const rangeFormatted = format(
|
||||
rangeString,
|
||||
Object.assign({}, opts, {
|
||||
rangeStart: 0,
|
||||
rangeEnd: Infinity,
|
||||
printWidth: opts.printWidth - alignmentSize
|
||||
}),
|
||||
alignmentSize
|
||||
);
|
||||
|
||||
// Since the range contracts to avoid trailing whitespace,
|
||||
// we need to remove the newline that was inserted by the `format` call.
|
||||
const rangeTrimmed = rangeFormatted.trimRight();
|
||||
|
||||
return text.slice(0, rangeStart) + rangeTrimmed + text.slice(rangeEnd);
|
||||
if (opts.rangeStart <= 0 && text.length <= opts.rangeEnd) {
|
||||
return;
|
||||
}
|
||||
|
||||
const range = calculateRange(text, opts, ast);
|
||||
const rangeStart = range.rangeStart;
|
||||
const rangeEnd = range.rangeEnd;
|
||||
const rangeString = text.slice(rangeStart, rangeEnd);
|
||||
|
||||
// Try to extend the range backwards to the beginning of the line.
|
||||
// This is so we can detect indentation correctly and restore it.
|
||||
// Use `Math.min` since `lastIndexOf` returns 0 when `rangeStart` is 0
|
||||
const rangeStart2 = Math.min(
|
||||
rangeStart,
|
||||
text.lastIndexOf("\n", rangeStart) + 1
|
||||
);
|
||||
const indentString = text.slice(rangeStart2, rangeStart);
|
||||
|
||||
const alignmentSize = util.getAlignmentSize(indentString, opts.tabWidth);
|
||||
|
||||
const rangeFormatted = format(
|
||||
rangeString,
|
||||
Object.assign({}, opts, {
|
||||
rangeStart: 0,
|
||||
rangeEnd: Infinity,
|
||||
printWidth: opts.printWidth - alignmentSize
|
||||
}),
|
||||
alignmentSize
|
||||
);
|
||||
|
||||
// Since the range contracts to avoid trailing whitespace,
|
||||
// we need to remove the newline that was inserted by the `format` call.
|
||||
const rangeTrimmed = rangeFormatted.trimRight();
|
||||
|
||||
return text.slice(0, rangeStart) + rangeTrimmed + text.slice(rangeEnd);
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
|
|
Loading…
Reference in New Issue