Skip to content

Commit 3c90be6

Browse files
authoredMay 11, 2021
fix(4.3.4|4.3.5): パラグラフにCodeブロックを含む時の問題を修正 (#90)
* パラグラフにCodeブロックを含む時の問題を修正 * カラムがズレる問題を修正しました。 * Str nodeのみをpushする方針で再実装してみました。 * 助言に従いリファクタリング * loopの判定条件のバグを修正
1 parent 3f428de commit 3c90be6

File tree

6 files changed

+127
-32
lines changed

6 files changed

+127
-32
lines changed
 

‎src/util/pair-checker.js

+59-31
Original file line numberDiff line numberDiff line change
@@ -12,40 +12,69 @@ import { RuleHelper } from "textlint-rule-helper";
1212
export function checkPair(context, { left, right }) {
1313
assert(left);
1414
assert(right);
15-
let { Syntax, RuleError, report, getSource } = context;
16-
let helper = new RuleHelper(context);
15+
const { Syntax, RuleError, report, getSource } = context;
16+
const helper = new RuleHelper(context);
1717
let isInParagraph = false;
1818
let currentStrInParagraph = [];
1919
/**
2020
* `Str` nodeの配列を受け取り、pairが見つからないnodeを返す
2121
* @param {Object} currentStrInParagraph
2222
* @returns {{node, index}[]}
2323
*/
24+
const findAllSymbolLocations = (symbol, text) => {
25+
let index = 0;
26+
const symbolLocations = [];
27+
while (index < text.length) {
28+
index = text.indexOf(symbol, index);
29+
if (index < 0) break;
30+
symbolLocations.push({
31+
index,
32+
symbol
33+
});
34+
index += 1;
35+
}
36+
return symbolLocations;
37+
};
2438
const foundMissingPairNodes = (currentStrInParagraph) => {
25-
let foundLeft = false;
26-
let matchParentheses = [];
27-
currentStrInParagraph.forEach((node) => {
28-
const text = getSource(node);
29-
// left を探す
30-
let leftIndex = -1;
31-
if (!foundLeft) {
32-
leftIndex = text.indexOf(left);
33-
if (leftIndex !== -1) {
34-
matchParentheses.push({
35-
node,
36-
index: leftIndex
37-
});
38-
foundLeft = true;
39-
}
39+
let matchParentheses = currentStrInParagraph
40+
.map((node) => {
41+
let text = getSource(node);
42+
const leftSymbolLocations = findAllSymbolLocations(left, text);
43+
const rightSymbolLocations = left !== right ? findAllSymbolLocations(right, text) : [];
44+
const allSymbolLocations = [...leftSymbolLocations, ...rightSymbolLocations].sort(
45+
(a, b) => a.index - b.index
46+
);
47+
return allSymbolLocations.map((loc) => ({ ...loc, ...{ node } }));
48+
})
49+
.flat();
50+
if (left === right) {
51+
const isCompletedParentheses = matchParentheses.length % 2 == 0;
52+
if (isCompletedParentheses) {
53+
return [];
54+
} else {
55+
return [matchParentheses[matchParentheses.length - 1]];
4056
}
41-
// right を探す
42-
let pairIndex = text.indexOf(right, leftIndex + 1);
43-
if (pairIndex !== -1) {
44-
matchParentheses.pop();
45-
foundLeft = false;
57+
} else {
58+
const lastUnmatchParences = [];
59+
while (matchParentheses.length > 0) {
60+
const item = matchParentheses.shift();
61+
if (item.symbol == left) {
62+
lastUnmatchParences.push(item);
63+
} else {
64+
// right
65+
const last = lastUnmatchParences.pop();
66+
if (last) {
67+
if (last.symbol == right) {
68+
lastUnmatchParences.push(last);
69+
lastUnmatchParences.push(item);
70+
}
71+
} else {
72+
lastUnmatchParences.push(item);
73+
}
74+
}
4675
}
47-
});
48-
return matchParentheses;
76+
return lastUnmatchParences;
77+
}
4978
};
5079
return {
5180
[Syntax.Paragraph](node) {
@@ -69,13 +98,12 @@ export function checkPair(context, { left, right }) {
6998
if (missingPairList.length === 0) {
7099
return;
71100
}
72-
missingPairList.forEach(({ node, index }) => {
73-
report(
74-
node,
75-
new RuleError(`${left}の対となる${right}が見つかりません。${left}${right}`, {
76-
index
77-
})
78-
);
101+
missingPairList.forEach(({ index, node, symbol }) => {
102+
let message =
103+
symbol === left
104+
? `${left}の対となる${right}が見つかりません。${left}${right}`
105+
: `${right}の対となる${left}が見つかりません。${left}${right}`;
106+
report(node, new RuleError(message, { index }));
79107
});
80108
}
81109
};

‎test/4.3.3-test.js

+4
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,10 @@ tester.run("4.3.3.かぎかっこ「」", rule, {
3232
{
3333
message: "「の対となる」が見つかりません。「」",
3434
column: 1
35+
},
36+
{
37+
message: "」の対となる「が見つかりません。「」",
38+
column: 3
3539
}
3640
]
3741
},

‎test/4.3.4-test.js

+44
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,10 @@ tester.run("4.3.4.二重かぎかっこ『』", rule, {
3333
{
3434
message: "『の対となる』が見つかりません。『』",
3535
column: 1
36+
},
37+
{
38+
message: "』の対となる『が見つかりません。『』",
39+
column: 3
3640
}
3741
]
3842
},
@@ -45,6 +49,46 @@ tester.run("4.3.4.二重かぎかっこ『』", rule, {
4549
column: 3
4650
}
4751
]
52+
},
53+
{
54+
text: "開くかっこがない文章です』",
55+
errors: [
56+
{
57+
message: "』の対となる『が見つかりません。『』",
58+
column: 13
59+
}
60+
]
61+
},
62+
{
63+
text: "『多重で『閉じる』かっこが足りない文章です",
64+
errors: [
65+
{
66+
message: "『の対となる』が見つかりません。『』",
67+
column: 1
68+
}
69+
]
70+
},
71+
{
72+
text: "多重で『開く』かっこが足りない文章です』",
73+
errors: [
74+
{
75+
message: "』の対となる『が見つかりません。『』",
76+
column: 20
77+
}
78+
]
79+
},
80+
{
81+
text: "』開くかっこが『複数』足りない文章です』",
82+
errors: [
83+
{
84+
message: "』の対となる『が見つかりません。『』",
85+
column: 1
86+
},
87+
{
88+
message: "』の対となる『が見つかりません。『』",
89+
column: 20
90+
}
91+
]
4892
}
4993
]
5094
});

‎test/4.3.5-test.js

+12-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,9 @@ tester.run('4.3.5.二重引用符""', rule, {
1313
彼は"×××"を参照してくださいと言った。
1414
`,
1515
'- 彼は"×××"を参照してくださいと言った。',
16-
'いわゆる"スマート"な都市'
16+
'いわゆる"スマート"な都市',
17+
'彼は"xxx" "`a=1;`"を参照してくださいと言った。',
18+
'彼は"xxx" "`a="x;`"を参照してくださいと言った。コード内の不一定は"無視"されます。'
1719
],
1820
invalid: [
1921
{
@@ -49,6 +51,15 @@ tester.run('4.3.5.二重引用符""', rule, {
4951
column: 3
5052
}
5153
]
54+
},
55+
{
56+
text: '彼は"xxx" "`a="x";`" "yyy を参照してくださいと言った。',
57+
errors: [
58+
{
59+
message: '"の対となる"が見つかりません。""',
60+
column: 20
61+
}
62+
]
5263
}
5364
]
5465
});

‎test/4.3.6-test.js

+4
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,10 @@ tester.run("4.3.6.中かっこ{ }", rule, {
2323
{
2424
message: "{の対となる}が見つかりません。{}",
2525
column: 1
26+
},
27+
{
28+
message: "}の対となる{が見つかりません。{}",
29+
column: 3
2630
}
2731
]
2832
},

‎test/4.3.7-test.js

+4
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,10 @@ tester.run("4.3.7.山かっこ<>", rule, {
2727
{
2828
message: "<の対となる>が見つかりません。<>",
2929
column: 1
30+
},
31+
{
32+
message: ">の対となる<が見つかりません。<>",
33+
column: 3
3034
}
3135
]
3236
},

0 commit comments

Comments
 (0)
Please sign in to comment.