-
-
Notifications
You must be signed in to change notification settings - Fork 2.8k
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
New rule: jsx-one-expression-per-line #1497
Merged
ljharb
merged 14 commits into
jsx-eslint:master
from
Vydia:new-rule-one-element-per-line
Oct 30, 2017
Merged
Changes from 12 commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
618bdb7
new rule: one-element-per-line
TSMMark 7f6784e
require jsx-one-element-per-line in index.js
TSMMark 256e5bc
fix sibling reporting
TSMMark 3893c3d
short-circuit childrenOnLine finder when a child is found
TSMMark 652683d
refactor rule to simply check parent line instead of all children
TSMMark a45e9c4
credit where credit is due
TSMMark 8e81d90
fix always reporting new line required for all siblings
TSMMark e51c4cf
add an invalid test case using html tags
TSMMark 799dfa6
failing tests describing how to handle non-element jsx nodes
TSMMark 7bdc856
rework internals to check children of JSXElement
TSMMark cb5f447
handle certain cases in one pass
TSMMark a18c30b
rename to jsx-one-expression-per-line
TSMMark 1344f1d
strip spaces but leave line returns
TSMMark cd3648c
use better practices
TSMMark File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,176 @@ | ||
/** | ||
* @fileoverview Limit to one element tag per line in JSX | ||
* @author Mark Ivan Allen <Vydia.com> | ||
*/ | ||
|
||
'use strict'; | ||
|
||
// ------------------------------------------------------------------------------ | ||
// Rule Definition | ||
// ------------------------------------------------------------------------------ | ||
|
||
module.exports = { | ||
meta: { | ||
docs: { | ||
description: 'Limit to one element tag per line in JSX', | ||
category: 'Stylistic Issues', | ||
recommended: false | ||
}, | ||
fixable: 'whitespace', | ||
schema: [] | ||
}, | ||
|
||
create: function (context) { | ||
const sourceCode = context.getSourceCode(); | ||
|
||
function nodeKey (node) { | ||
return `${node.loc.start.line},${node.loc.start.column}`; | ||
} | ||
|
||
function nodeDescriptor (n) { | ||
return n.openingElement ? n.openingElement.name.name : sourceCode.getText(n).replace(/\n/g, ''); | ||
} | ||
|
||
return { | ||
JSXElement: function (node) { | ||
const children = node.children; | ||
|
||
if (!children || !children.length) { | ||
return; | ||
} | ||
|
||
const openingElement = node.openingElement; | ||
const closingElement = node.closingElement; | ||
const openingElementEndLine = openingElement.loc.end.line; | ||
const closingElementStartLine = closingElement.loc.start.line; | ||
|
||
const childrenGroupedByLine = {}; | ||
const fixDetailsByNode = {}; | ||
|
||
children.forEach(child => { | ||
let countNewLinesBeforeContent = 0; | ||
let countNewLinesAfterContent = 0; | ||
|
||
if (child.type === 'Literal') { | ||
if (child.value.match(/^\s*$/)) { | ||
return; | ||
} | ||
|
||
countNewLinesBeforeContent = (child.raw.match(/^ *\n/g) || []).length; | ||
countNewLinesAfterContent = (child.raw.match(/\n *$/g) || []).length; | ||
} | ||
|
||
const startLine = child.loc.start.line + countNewLinesBeforeContent; | ||
const endLine = child.loc.end.line - countNewLinesAfterContent; | ||
|
||
if (startLine === endLine) { | ||
if (!childrenGroupedByLine[startLine]) { | ||
childrenGroupedByLine[startLine] = []; | ||
} | ||
childrenGroupedByLine[startLine].push(child); | ||
} else { | ||
if (!childrenGroupedByLine[startLine]) { | ||
childrenGroupedByLine[startLine] = []; | ||
} | ||
childrenGroupedByLine[startLine].push(child); | ||
if (!childrenGroupedByLine[endLine]) { | ||
childrenGroupedByLine[endLine] = []; | ||
} | ||
childrenGroupedByLine[endLine].push(child); | ||
} | ||
}); | ||
|
||
Object.keys(childrenGroupedByLine).forEach(line => { | ||
line = parseInt(line, 10); | ||
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. Instead of reassigning the argument here, can we use a new variable? |
||
const firstIndex = 0; | ||
const lastIndex = childrenGroupedByLine[line].length - 1; | ||
|
||
childrenGroupedByLine[line].forEach((child, i) => { | ||
let prevChild; | ||
if (i === firstIndex) { | ||
if (line === openingElementEndLine) { | ||
prevChild = openingElement; | ||
} | ||
} else { | ||
prevChild = childrenGroupedByLine[line][i - 1]; | ||
} | ||
let nextChild; | ||
if (i === lastIndex) { | ||
if (line === closingElementStartLine) { | ||
nextChild = closingElement; | ||
} | ||
} else { | ||
// We don't need to append a trailing because the next child will prepend a leading. | ||
// nextChild = childrenGroupedByLine[line][i + 1]; | ||
} | ||
|
||
function spaceBetweenPrev () { | ||
return (prevChild.type === 'Literal' && prevChild.raw.match(/ $/)) || | ||
(child.type === 'Literal' && child.raw.match(/^ /)) || | ||
sourceCode.isSpaceBetweenTokens(prevChild, child); | ||
} | ||
function spaceBetweenNext () { | ||
return (nextChild.type === 'Literal' && nextChild.raw.match(/^ /)) || | ||
(child.type === 'Literal' && child.raw.match(/ $/)) || | ||
sourceCode.isSpaceBetweenTokens(child, nextChild); | ||
} | ||
|
||
if (!prevChild && !nextChild) { | ||
return; | ||
} | ||
|
||
const source = sourceCode.getText(child); | ||
const leadingSpace = !!(prevChild && spaceBetweenPrev()); | ||
const trailingSpace = !!(nextChild && spaceBetweenNext()); | ||
const leadingNewLine = !!(prevChild); | ||
const trailingNewLine = !!(nextChild); | ||
|
||
const key = nodeKey(child); | ||
|
||
if (!fixDetailsByNode[key]) { | ||
fixDetailsByNode[key] = { | ||
node: child, | ||
source: source, | ||
descriptor: nodeDescriptor(child) | ||
}; | ||
} | ||
|
||
if (leadingSpace) { | ||
fixDetailsByNode[key].leadingSpace = true; | ||
} | ||
if (leadingNewLine) { | ||
fixDetailsByNode[key].leadingNewLine = true; | ||
} | ||
if (trailingNewLine) { | ||
fixDetailsByNode[key].trailingNewLine = true; | ||
} | ||
if (trailingSpace) { | ||
fixDetailsByNode[key].trailingSpace = true; | ||
} | ||
}); | ||
}); | ||
|
||
Object.keys(fixDetailsByNode).forEach(key => { | ||
const details = fixDetailsByNode[key]; | ||
|
||
const nodeToReport = details.node; | ||
const descriptor = details.descriptor; | ||
const source = details.source; | ||
|
||
const leadingSpaceString = details.leadingSpace ? '\n{\' \'}' : ''; | ||
const trailingSpaceString = details.trailingSpace ? '{\' \'}\n' : ''; | ||
const leadingNewLineString = details.leadingNewLine ? '\n' : ''; | ||
const trailingNewLineString = details.trailingNewLine ? '\n' : ''; | ||
|
||
context.report({ | ||
node: nodeToReport, | ||
message: `\`${descriptor}\` must be placed on a new line`, | ||
fix: function (fixer) { | ||
return fixer.replaceText(nodeToReport, `${leadingSpaceString}${leadingNewLineString}${source}${trailingNewLineString}${trailingSpaceString}`); | ||
} | ||
}); | ||
}); | ||
} | ||
}; | ||
} | ||
}; |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
This should probably use
regex.test(str)
instead of match (here and elsewhere).