Skip to content
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

パラグラフにCodeブロックを含む時の問題を修正 #90

Merged
merged 5 commits into from
May 11, 2021

Conversation

ksato9700
Copy link
Contributor

#89 の問題を解決してみました。できるだけ既存のコードに手を入れないようにしたので少し冗長な部分もありますが、Codeブロックの中の不一致も含めてチェック出来るようにしたつもりです。

@@ -55,21 +59,35 @@ export function checkPair(context, { left, right }) {
currentStrInParagraph = [];
isInParagraph = true;
},
[Syntax.Code](node) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

この Code nodeでの探索ってコード内の括弧の対応を見ています?

「text

Codeはinvalidなケースが正の場合があるので意図的に無視していました。
間違い例とかを出すケースなどがあるので、オプショナルにしないと行けない感じがします。

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

そうですね。Code内の対応付をとりあえず見ないということであればその部分を削除してしまって良いかと思います。問題なければそれでPR出し直します。

Copy link
Member

@azu azu May 9, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

はい、コード内の括弧チェックは別対応でした方が良いと思います。

test/4.3.5-test.js Show resolved Hide resolved
if (!isInParagraph) {
return;
}
currentStrInParagraph.push(node);
Copy link
Member

@azu azu May 9, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

**Strong** "text

InlineのNodeにはCode以外にもStrongなど色々あるので、他のNodeが挟まると同じ問題が起きそうな気がします。
(全部のNodeを書くとNodeが増えたときに問題があるので、pushするのはStr nodeのみで解決するのが良い気はします)

reportするNodeをStrにして、foundMissingPairNodesで返すindexはそのStr nodeからのrelativeなindexにする(ペアの探索の仕方をちょっと工夫する)
OR
reportするNodeはParagraphにして、Str + Str のようにStr node同士を結合を作るときに、

Str1                   Str1
  • Str1のrangeが[0,4]
  • Str2のrangeが[10,14]
  • 4 - 10 をスペースで埋める

みたいに Strrangeを見て空いている場所は、スペースなどで自動的にpaddingで埋めるとかですかね(今のPRの実装はこれに近い)?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

そうですね。その方向で再実装してみます。コメントありがとうございます。

@ksato9700
Copy link
Contributor Author

やや複雑になってしまったかもですが、再実装してみました。一度全てのleft rightの文字のある場所をリスト化し、leftrightが同じであるか否かによって処理を分けています。カラムは正しいnodeを report()に渡せば計算してくれるようです。

Comment on lines 42 to 51
while (index < text.length) {
index = text.indexOf(right, index);
if (index < 0) break;
symbolLocations.push({
index,
node,
type: "right"
});
index += 1;
}
Copy link
Member

@azu azu May 9, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

この text から 特定のsymbolを探索する処理を leftright でまとめられそうな気がします。
findAllSymbolLocations({ symbol, text }): symbolLocation[] みたいな感じで、特定のsymbolを探して、そのlocationの配列を返す感じですね。
(名前はもっといいのがあるかもしれないですが。tc39/proposal-regexp-match-indices: ECMAScript RegExp Match Indicesとかは近い話かも)

擬似コード的には次のようにまとめられる気がします。

const leftSymbolLocations = findAllSymbolLocations({ symbol: left, text });
const rightSymbolLocations = left !== right ? findAllSymbolLocations({ symbol: right, text }) : [];
const allSymbolLocations = [...leftLocations, ...rightLocations].sort((a, b) => a.index - b.index);

📝 type=bothみたいのを入れるべきかはまよったけど、bothを入れてもそこまでシンプルにはならないのかな。
(後半のチェックの処理でleft !== rightで処理を分けずにやるにはbothがあるといいけど、あんまりシンプルにできないかもと思った)

unmatchParences.push(item);
} else {
// right
let last = unmatchParences.pop();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lastUnmatchParences とかmatchかunmatchが迷うので名前つけたほうが良さそうですね。

} else {
let unmatchParences = [];
while (matchParentheses) {
let item = matchParentheses.shift();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

constで大丈夫なはずですね。

Suggested change
let item = matchParentheses.shift();
const item = matchParentheses.shift();

matchParentheses.pop();
foundLeft = false;
} else {
let unmatchParences = [];
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
let unmatchParences = [];
const unmatchParences = [];

popやshiftなのでconstでいいはず

.flat();

if (left === right) {
if (matchParentheses.length % 2 == 0) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

matchParentheses.length % 2 == 0 を変数にしたいですね。

const isCompletedParentheses = matchParentheses.length % 2 == 0;

みたいな(名前はてきとうですが)

foundLeft = false;
} else {
const lastUnmatchParences = [];
while (matchParentheses) {
Copy link
Member

@azu azu May 10, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

image

matchParentheses って falseになることってありますか? ( break でなんとか抜けているのか)
while(true) になっている気がします。
matchParentheses.length !== 0 が意図した形かもしれないです。

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

仰るとおりです。ご指摘ありがとうございます。

Copy link
Member

@azu azu left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

対応ありがとうございます!

@azu azu merged commit 3c90be6 into textlint-ja:master May 11, 2021
@ksato9700 ksato9700 deleted the doublequote-with-code branch May 12, 2021 01:51
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants