Skip to content

Commit

Permalink
lint: Add yoda rule with range exception (prettier#2433)
Browse files Browse the repository at this point in the history
This required a small refactoring, but the code is less nested as a result.
See discussion at prettier#2401 (comment)

http://eslint.org/docs/rules/yoda#exceptrange
  • Loading branch information
josephfrazier authored and azz committed Jul 8, 2017
1 parent 1b4846b commit 0fb332b
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 33 deletions.
3 changes: 2 additions & 1 deletion .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -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: {
Expand Down
66 changes: 34 additions & 32 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand Down

0 comments on commit 0fb332b

Please sign in to comment.