-
Notifications
You must be signed in to change notification settings - Fork 0
Run sindresorhus#1221 on codebase #282
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
base: prefer-ternary-improve
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -173,11 +173,7 @@ function parseTodoMessage(todoString) { | |
// Check if have to skip colon | ||
// @example "TODO [...]: message here" | ||
const dropColon = afterArguments[0] === ':'; | ||
if (dropColon) { | ||
return afterArguments.slice(1).trim(); | ||
} | ||
|
||
return afterArguments; | ||
return dropColon ? afterArguments.slice(1).trim() : afterArguments; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is good |
||
} | ||
|
||
function reachedDate(past) { | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -79,9 +79,7 @@ function getLengthCheckNode(node) { | |
} | ||
|
||
// Non-Zero length check | ||
if ( | ||
// `foo.length !== 0` | ||
isCompareRight(node, '!==', 0) || | ||
return isCompareRight(node, '!==', 0) || | ||
// `foo.length != 0` | ||
isCompareRight(node, '!=', 0) || | ||
// `foo.length > 0` | ||
|
@@ -95,12 +93,7 @@ function getLengthCheckNode(node) { | |
// `0 < foo.length` | ||
isCompareLeft(node, '<', 0) || | ||
// `1 <= foo.length` | ||
isCompareLeft(node, '<=', 1) | ||
) { | ||
return {isZeroLengthCheck: false, node}; | ||
} | ||
|
||
return {}; | ||
isCompareLeft(node, '<=', 1) ? {isZeroLengthCheck: false, node} : {}; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is fine |
||
} | ||
|
||
function create(context) { | ||
|
Original file line number | Diff line number | Diff line change | ||||||||
---|---|---|---|---|---|---|---|---|---|---|
|
@@ -133,31 +133,19 @@ function englishishJoinWords(words) { | |||||||||
return words[0]; | ||||||||||
} | ||||||||||
|
||||||||||
if (words.length === 2) { | ||||||||||
return `${words[0]} or ${words[1]}`; | ||||||||||
} | ||||||||||
|
||||||||||
return `${words.slice(0, -1).join(', ')}, or ${words[words.length - 1]}`; | ||||||||||
return words.length === 2 ? `${words[0]} or ${words[1]}` : `${words.slice(0, -1).join(', ')}, or ${words[words.length - 1]}`; | ||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is fine, although maybe on 3 lines
Suggested change
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. But the old one still better function englishishJoinWords(words) {
if (words.length === 1) {
return words[0];
}
if (words.length === 2) {
return `${words[0]} or ${words[1]}`;
}
return `${words.slice(0, -1).join(', ')}, or ${words[words.length - 1]}`;
} There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Indeed There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Problem is hard to detect this. because maybe some other stuff above |
||||||||||
} | ||||||||||
|
||||||||||
const create = context => { | ||||||||||
const options = context.options[0] || {}; | ||||||||||
const chosenCases = getChosenCases(options); | ||||||||||
const ignore = (options.ignore || []).map(item => { | ||||||||||
if (item instanceof RegExp) { | ||||||||||
return item; | ||||||||||
} | ||||||||||
|
||||||||||
return new RegExp(item, 'u'); | ||||||||||
return item instanceof RegExp ? item : new RegExp(item, 'u'); | ||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is fine |
||||||||||
}); | ||||||||||
const chosenCasesFunctions = chosenCases.map(case_ => ignoreNumbers(cases[case_].fn)); | ||||||||||
const filenameWithExtension = context.getFilename(); | ||||||||||
|
||||||||||
if (filenameWithExtension === '<input>' || filenameWithExtension === '<text>') { | ||||||||||
return {}; | ||||||||||
} | ||||||||||
|
||||||||||
return { | ||||||||||
return filenameWithExtension === '<input>' || filenameWithExtension === '<text>' ? {} : { | ||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is ok, it was more readable before |
||||||||||
Program: node => { | ||||||||||
const extension = path.extname(filenameWithExtension); | ||||||||||
const filename = path.basename(filenameWithExtension, extension); | ||||||||||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -148,11 +148,7 @@ function getFixFunction(callExpression, sourceCode, functionInfo) { | |
return false; | ||
} | ||
|
||
if (callback.body.type !== 'BlockStatement') { | ||
return false; | ||
} | ||
|
||
return true; | ||
return callback.body.type === 'BlockStatement'; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @fregante This is what you want 😄 |
||
}; | ||
|
||
function * removeCallbackParentheses(fixer) { | ||
|
@@ -311,11 +307,7 @@ function isFixable(callExpression, sourceCode, {scope, functionInfo, allIdentifi | |
return false; | ||
} | ||
|
||
if (isFunctionSelfUsedInside(callback, callbackScope)) { | ||
return false; | ||
} | ||
|
||
return true; | ||
return !isFunctionSelfUsedInside(callback, callbackScope); | ||
} | ||
|
||
const ignoredObjects = [ | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -36,11 +36,7 @@ const getIndexIdentifierName = forStatement => { | |
return; | ||
} | ||
|
||
if (variableDeclarator.id.type !== 'Identifier') { | ||
return; | ||
} | ||
|
||
return variableDeclarator.id.name; | ||
return variableDeclarator.id.type !== 'Identifier' ? undefined : variableDeclarator.id.name; | ||
}; | ||
|
||
const getStrictComparisonOperands = binaryExpression => { | ||
|
@@ -83,30 +79,18 @@ const getArrayIdentifierNameFromBinaryExpression = (binaryExpression, indexIdent | |
return; | ||
} | ||
|
||
if (greater.property.name !== 'length') { | ||
return; | ||
} | ||
|
||
return greater.object.name; | ||
return greater.property.name !== 'length' ? undefined : greater.object.name; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It looks like this whole function could be merged into 2 |
||
}; | ||
|
||
const getArrayIdentifierName = (forStatement, indexIdentifierName) => { | ||
const {test} = forStatement; | ||
|
||
if (!test || test.type !== 'BinaryExpression') { | ||
return; | ||
} | ||
|
||
return getArrayIdentifierNameFromBinaryExpression(test, indexIdentifierName); | ||
return !test || test.type !== 'BinaryExpression' ? undefined : getArrayIdentifierNameFromBinaryExpression(test, indexIdentifierName); | ||
}; | ||
|
||
const isLiteralOnePlusIdentifierWithName = (node, identifierName) => { | ||
if (node && node.type === 'BinaryExpression' && node.operator === '+') { | ||
return (isIdentifierWithName(node.left, identifierName) && isLiteralOne(node.right)) || | ||
(isIdentifierWithName(node.right, identifierName) && isLiteralOne(node.left)); | ||
} | ||
|
||
return false; | ||
return node && node.type === 'BinaryExpression' && node.operator === '+' ? (isIdentifierWithName(node.left, identifierName) && isLiteralOne(node.right)) || | ||
(isIdentifierWithName(node.right, identifierName) && isLiteralOne(node.left)) : false; | ||
}; | ||
|
||
const checkUpdateExpression = (forStatement, indexIdentifierName) => { | ||
|
@@ -148,14 +132,8 @@ const isOnlyArrayOfIndexVariableRead = (arrayReferences, indexIdentifierName) => | |
return false; | ||
} | ||
|
||
if ( | ||
node.parent.type === 'AssignmentExpression' && | ||
node.parent.left === node | ||
) { | ||
return false; | ||
} | ||
|
||
return true; | ||
return !(node.parent.type === 'AssignmentExpression' && | ||
node.parent.left === node); | ||
}); | ||
}; | ||
|
||
|
@@ -176,14 +154,10 @@ const getRemovalRange = (node, sourceCode) => { | |
|
||
const index = declarationNode.declarations.indexOf(node); | ||
|
||
if (index === 0) { | ||
return [ | ||
node.range[0], | ||
declarationNode.declarations[1].range[0] | ||
]; | ||
} | ||
|
||
return [ | ||
return index === 0 ? [ | ||
node.range[0], | ||
declarationNode.declarations[1].range[0] | ||
] : [ | ||
declarationNode.declarations[index - 1].range[1], | ||
node.range[1] | ||
]; | ||
|
@@ -235,11 +209,7 @@ const isIndexVariableUsedElsewhereInTheLoopBody = (indexVariable, bodyScope, arr | |
return true; | ||
} | ||
|
||
if (node.object.name !== arrayIdentifierName) { | ||
return true; | ||
} | ||
|
||
return false; | ||
return node.object.name !== arrayIdentifierName; | ||
}); | ||
|
||
return referencesOtherThanArrayAccess.length > 0; | ||
|
@@ -343,11 +313,7 @@ const create = context => { | |
const elementReference = arrayReferences.find(reference => { | ||
const node = reference.identifier.parent; | ||
|
||
if (node.parent.type !== 'VariableDeclarator') { | ||
return false; | ||
} | ||
|
||
return true; | ||
return node.parent.type === 'VariableDeclarator'; | ||
}); | ||
const elementNode = elementReference && elementReference.identifier.parent.parent; | ||
const elementIdentifierName = elementNode && elementNode.id.name; | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Either this should be excluded due its length, or:
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think it shouldn't use the ternary at all:
But it's still not readable as is. Maybe this (I'm not sure if the boolean logic is identical though):
But given the length, once again, it's probably not worth the change