Skip to content

Commit

Permalink
fix(4.3.4|4.3.5): パラグラフにCodeブロックを含む時の問題を修正 (#90)
Browse files Browse the repository at this point in the history
* パラグラフにCodeブロックを含む時の問題を修正

* カラムがズレる問題を修正しました。

* Str nodeのみをpushする方針で再実装してみました。

* 助言に従いリファクタリング

* loopの判定条件のバグを修正
  • Loading branch information
ksato9700 committed May 11, 2021
1 parent 3f428de commit 3c90be6
Show file tree
Hide file tree
Showing 6 changed files with 127 additions and 32 deletions.
90 changes: 59 additions & 31 deletions src/util/pair-checker.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,40 +12,69 @@ import { RuleHelper } from "textlint-rule-helper";
export function checkPair(context, { left, right }) {
assert(left);
assert(right);
let { Syntax, RuleError, report, getSource } = context;
let helper = new RuleHelper(context);
const { Syntax, RuleError, report, getSource } = context;
const helper = new RuleHelper(context);
let isInParagraph = false;
let currentStrInParagraph = [];
/**
* `Str` nodeの配列を受け取り、pairが見つからないnodeを返す
* @param {Object} currentStrInParagraph
* @returns {{node, index}[]}
*/
const findAllSymbolLocations = (symbol, text) => {
let index = 0;
const symbolLocations = [];
while (index < text.length) {
index = text.indexOf(symbol, index);
if (index < 0) break;
symbolLocations.push({
index,
symbol
});
index += 1;
}
return symbolLocations;
};
const foundMissingPairNodes = (currentStrInParagraph) => {
let foundLeft = false;
let matchParentheses = [];
currentStrInParagraph.forEach((node) => {
const text = getSource(node);
// left を探す
let leftIndex = -1;
if (!foundLeft) {
leftIndex = text.indexOf(left);
if (leftIndex !== -1) {
matchParentheses.push({
node,
index: leftIndex
});
foundLeft = true;
}
let matchParentheses = currentStrInParagraph
.map((node) => {
let text = getSource(node);
const leftSymbolLocations = findAllSymbolLocations(left, text);
const rightSymbolLocations = left !== right ? findAllSymbolLocations(right, text) : [];
const allSymbolLocations = [...leftSymbolLocations, ...rightSymbolLocations].sort(
(a, b) => a.index - b.index
);
return allSymbolLocations.map((loc) => ({ ...loc, ...{ node } }));
})
.flat();
if (left === right) {
const isCompletedParentheses = matchParentheses.length % 2 == 0;
if (isCompletedParentheses) {
return [];
} else {
return [matchParentheses[matchParentheses.length - 1]];
}
// right を探す
let pairIndex = text.indexOf(right, leftIndex + 1);
if (pairIndex !== -1) {
matchParentheses.pop();
foundLeft = false;
} else {
const lastUnmatchParences = [];
while (matchParentheses.length > 0) {
const item = matchParentheses.shift();
if (item.symbol == left) {
lastUnmatchParences.push(item);
} else {
// right
const last = lastUnmatchParences.pop();
if (last) {
if (last.symbol == right) {
lastUnmatchParences.push(last);
lastUnmatchParences.push(item);
}
} else {
lastUnmatchParences.push(item);
}
}
}
});
return matchParentheses;
return lastUnmatchParences;
}
};
return {
[Syntax.Paragraph](node) {
Expand All @@ -69,13 +98,12 @@ export function checkPair(context, { left, right }) {
if (missingPairList.length === 0) {
return;
}
missingPairList.forEach(({ node, index }) => {
report(
node,
new RuleError(`${left}の対となる${right}が見つかりません。${left}${right}`, {
index
})
);
missingPairList.forEach(({ index, node, symbol }) => {
let message =
symbol === left
? `${left}の対となる${right}が見つかりません。${left}${right}`
: `${right}の対となる${left}が見つかりません。${left}${right}`;
report(node, new RuleError(message, { index }));
});
}
};
Expand Down
4 changes: 4 additions & 0 deletions test/4.3.3-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,10 @@ tester.run("4.3.3.かぎかっこ「」", rule, {
{
message: "「の対となる」が見つかりません。「」",
column: 1
},
{
message: "」の対となる「が見つかりません。「」",
column: 3
}
]
},
Expand Down
44 changes: 44 additions & 0 deletions test/4.3.4-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,10 @@ tester.run("4.3.4.二重かぎかっこ『』", rule, {
{
message: "『の対となる』が見つかりません。『』",
column: 1
},
{
message: "』の対となる『が見つかりません。『』",
column: 3
}
]
},
Expand All @@ -45,6 +49,46 @@ tester.run("4.3.4.二重かぎかっこ『』", rule, {
column: 3
}
]
},
{
text: "開くかっこがない文章です』",
errors: [
{
message: "』の対となる『が見つかりません。『』",
column: 13
}
]
},
{
text: "『多重で『閉じる』かっこが足りない文章です",
errors: [
{
message: "『の対となる』が見つかりません。『』",
column: 1
}
]
},
{
text: "多重で『開く』かっこが足りない文章です』",
errors: [
{
message: "』の対となる『が見つかりません。『』",
column: 20
}
]
},
{
text: "』開くかっこが『複数』足りない文章です』",
errors: [
{
message: "』の対となる『が見つかりません。『』",
column: 1
},
{
message: "』の対となる『が見つかりません。『』",
column: 20
}
]
}
]
});
13 changes: 12 additions & 1 deletion test/4.3.5-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@ tester.run('4.3.5.二重引用符""', rule, {
彼は"×××"を参照してくださいと言った。
`,
'- 彼は"×××"を参照してくださいと言った。',
'いわゆる"スマート"な都市'
'いわゆる"スマート"な都市',
'彼は"xxx" "`a=1;`"を参照してくださいと言った。',
'彼は"xxx" "`a="x;`"を参照してくださいと言った。コード内の不一定は"無視"されます。'
],
invalid: [
{
Expand Down Expand Up @@ -49,6 +51,15 @@ tester.run('4.3.5.二重引用符""', rule, {
column: 3
}
]
},
{
text: '彼は"xxx" "`a="x";`" "yyy を参照してくださいと言った。',
errors: [
{
message: '"の対となる"が見つかりません。""',
column: 20
}
]
}
]
});
4 changes: 4 additions & 0 deletions test/4.3.6-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@ tester.run("4.3.6.中かっこ{ }", rule, {
{
message: "{の対となる}が見つかりません。{}",
column: 1
},
{
message: "}の対となる{が見つかりません。{}",
column: 3
}
]
},
Expand Down
4 changes: 4 additions & 0 deletions test/4.3.7-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,10 @@ tester.run("4.3.7.山かっこ<>", rule, {
{
message: "<の対となる>が見つかりません。<>",
column: 1
},
{
message: ">の対となる<が見つかりません。<>",
column: 3
}
]
},
Expand Down

0 comments on commit 3c90be6

Please sign in to comment.