Skip to content

Commit

Permalink
fix(rule): fix a list that include nested paragraph (#7)
Browse files Browse the repository at this point in the history
fix #3
  • Loading branch information
azu authored Jul 10, 2019
1 parent 2cdfff2 commit aeb82e3
Show file tree
Hide file tree
Showing 2 changed files with 120 additions and 61 deletions.
114 changes: 56 additions & 58 deletions src/textlint-rule-period-in-list-item.js
Original file line number Diff line number Diff line change
Expand Up @@ -82,80 +82,78 @@ const reporter = (context, options = {}) => {
: defaultOptions.forceAppendPeriod;
return {
[Syntax.ListItem](node) {
// Skip Ordered List item if option is enabled
if (allowOrderedList && isItemNodeInOrderedList(node)) {
return;
}
// A ListItem should includes child nodes.
// https://github.com/textlint-rule/textlint-rule-period-in-list-item/issues/3
const PragraphNodes = node.children;
if (!Array.isArray(PragraphNodes)){
const paragraphNodes = node.children.filter(node => node.type === Syntax.Paragraph);
const firstParagraphNode = paragraphNodes[0];
if (!firstParagraphNode) {
return;
}
// Skip Ordered List item if option is enabled
if (allowOrderedList && isItemNodeInOrderedList(node)) {
const text = getSource(firstParagraphNode);
// Prefer no needed period, but exist period
if (isNotNeededPeriodMark) {
const {valid, periodMark, index} = checkEndsWithoutPeriodMark(text, periodMarks);
if (valid) {
return;
}
// should be remove period mark
report(firstParagraphNode, new RuleError(`Should remove period mark("${periodMark}") at end of list item.`, {
index,
fix: fixer.replaceTextRange([index, index + periodMark.length], "")
}));
return;
}
PragraphNodes.forEach(ParagraphNode => {
const text = getSource(ParagraphNode);
// Prefer no needed period, but exist period
if (isNotNeededPeriodMark) {
const { valid, periodMark, index } = checkEndsWithoutPeriodMark(text, periodMarks);
if (valid) {
return;
}
// should be remove period mark
report(ParagraphNode, new RuleError(`Should remove period mark("${periodMark}") at end of list item.`, {
index,
fix: fixer.replaceTextRange([index, index + periodMark.length], "")
}));
// - [link](http://example)
// should be ignored
if (ignoreLinkEnd) {
const linkNodes = firstParagraphNode.children;
if (linkNodes.length === 1 && linkNodes[0].type === Syntax.Link) {
return;
}
// - [link](http://example)
// should be ignored
if (ignoreLinkEnd) {
const linkNodes = ParagraphNode.children;
if (linkNodes.length === 1 && linkNodes[0].type === Syntax.Link) {
return;
}
}
const {valid, periodMark, index} = checkEndsWithPeriod(text, {
periodMarks,
allowPeriodMarks,
allowEmoji,
});
// Prefer to use period
if (valid) {
// but exist difference period
const isPeriodMarkAtEnd = periodMarks.indexOf(periodMark) !== -1;
// exception case that should not report
// !?
if (!isPeriodMarkAtEnd) {
return;
}
const { valid, periodMark, index } = checkEndsWithPeriod(text, {
periodMarks,
allowPeriodMarks,
allowEmoji,
});
// Prefer to use period
if (valid) {
// but exist difference period
const isPeriodMarkAtEnd = periodMarks.indexOf(periodMark) !== -1;
// exception case that should not report
// !?
if (!isPeriodMarkAtEnd) {
return;
}
// periodMark is expected, then exit
if (periodMark === preferPeriodMark) {
return;
}
report(ParagraphNode, new RuleError(`Prefer to use period mark("${preferPeriodMark}") at end of list item.`, {
// periodMark is expected, then exit
if (periodMark === preferPeriodMark) {
return;
}
report(firstParagraphNode, new RuleError(`Prefer to use period mark("${preferPeriodMark}") at end of list item.`, {
index,
fix: fixer.replaceTextRange([index, index + periodMark.length], preferPeriodMark)
}));
} else {
// but not exist period
if (forceAppendPeriod) {
report(firstParagraphNode, new RuleError(`Not exist period mark("${preferPeriodMark}") at end of list item.`, {
index,
fix: fixer.replaceTextRange([index, index + periodMark.length], preferPeriodMark)
fix: fixer.replaceTextRange([index + 1, index + 1], preferPeriodMark)
}));
return;
} else {
// but not exist period
if (forceAppendPeriod) {
report(ParagraphNode, new RuleError(`Not exist period mark("${preferPeriodMark}") at end of list item.`, {
index,
fix: fixer.replaceTextRange([index + 1, index + 1], preferPeriodMark)
}));
} else {
report(ParagraphNode, new RuleError(`Not exist period mark("${preferPeriodMark}") at end of list item.`, {
index
}))
}
report(firstParagraphNode, new RuleError(`Not exist period mark("${preferPeriodMark}") at end of list item.`, {
index
}))
}
})
}
}
}
};
module.exports = {
linter: reporter,
fixer: reporter
};
};
67 changes: 64 additions & 3 deletions test/textlint-rule-period-in-list-item-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,7 @@ tester.run("textlint-rule-period-in-list-item", rule, {
},
// https://github.com/textlint-rule/textlint-rule-period-in-list-item/issues/3
{
text: `
text: `ABC
- list item 1
A paragraph describing this list item 1.
Expand All @@ -219,7 +219,68 @@ tester.run("textlint-rule-period-in-list-item", rule, {
options: {
periodMark: "."
},
errors: [{ line: 2, column: 13 }, { line: 6, column: 13 }, { line: 10, column: 13 }]
errors: [{line: 2, column: 13}, {line: 6, column: 13}, {line: 10, column: 13}]
},
{
text: `DEF
- list item 1
A paragraph describing this list item 1.
- list item 2
A paragraph describing this list item 2.
- list item 3
A paragraph describing this list item 3.
`,
output: `DEF
- list item 1.
A paragraph describing this list item 1.
- list item 2.
A paragraph describing this list item 2.
- list item 3.
A paragraph describing this list item 3.
`,
options: {
periodMark: ".",
forceAppendPeriod: true
},
errors: [{line: 2, column: 13}, {line: 6, column: 13}, {line: 10, column: 13}]
},
{
text:
`- list item 1
- A paragraph describing this list item 1
- list item 2
- A paragraph describing this list item 2
- list item 3
- A paragraph describing this list item 3`,
output:
`- list item 1.
- A paragraph describing this list item 1.
- list item 2.
- A paragraph describing this list item 2.
- list item 3.
- A paragraph describing this list item 3.`,
options: {
periodMark: ".",
forceAppendPeriod: true
},
errors: [
{line: 1, column: 13},
{line: 2, column: 45},
{line: 3, column: 13},
{line: 4, column: 45},
{line: 5, column: 13},
{line: 6, column: 45}
]
},
]
});
});

0 comments on commit aeb82e3

Please sign in to comment.