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

Breaking Change: 文体が統一されていてもpreferIn設定に違反する場合エラーとなるように変更 #40

Merged
merged 4 commits into from
Dec 28, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 16 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,11 @@ textlint --rule no-mix-dearu-desumasu README.md
"preferInBody": "ですます",// "である" or "ですます"
"preferInList": "である", // "である" or "ですます"
// 文末以外でも、敬体(ですます調)と常体(である調)を厳しくチェックするかどうか
"strict": false
"strict": false,
// preferInでの設定を、"優先" ではなく、"強制" するかどうか
// 有効化した場合、被検査テキストの表現が統一されていても、
// preferInオプションで指定された表現に強制する
"enforcePreferences": false
}
}
}
Expand Down Expand Up @@ -98,6 +102,17 @@ textlint --rule no-mix-dearu-desumasu README.md

> NG: 今日はいい天気ですが、明日は悪天候である。

- `enforcePreferences`
- default: `false`
- preferInでの設定を、"優先" ではなく、"強制" するかどうか

例えば、`enforcePreferences:true`かつ、`preferInBody: "ですます"`では、以下のような **"である"** に統一された文章であっても、
`preferInBody`の設定と異なる表現になっているため、エラーとなります。

> NG: 今日はいい天気である。気持ちの良い朝である。

> OK: 今日はいい天気ですね。気持ちの良い朝です。

## Example

詳しくは[example/](example/)を動かして試してみてください。
Expand Down
7 changes: 5 additions & 2 deletions src/MixedChecker.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ export default class MixedChecker {
this.context = context;
/**
* 明示的な優先するタイプの指定
* @type {{preferDearu: boolean, preferDesumasu: boolean, isStrict: boolean}}
* @type {{preferDearu: boolean, preferDesumasu: boolean, isStrict: boolean, isEnforcePreferences: boolean}}
*/
this.options = options;
this.dearuCount = 0;
Expand Down Expand Up @@ -82,7 +82,10 @@ export default class MixedChecker {
}

isOver() {
return this.dearuCount !== 0 && this.desumasuCount !== 0;
return this.options.isEnforcePreferences
? (this.options.preferDesumasu && this.dearuCount !== 0) ||
(this.options.preferDearu && this.desumasuCount !== 0)
: this.dearuCount !== 0 && this.desumasuCount !== 0;
Copy link
Member

Choose a reason for hiding this comment

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

📝 オプションありのままで行く場合のコメント

if (this.options.isEnforcePreferences) { 
  return ...
}

return ...

三項演算子にまとめる必要があまりなさそう(ロジック的に異なるものなのでブロック的にわかれてた方が、後から片方を消しやすい)なので、ifで分けてearly returnした方が良さそう。

}

/**
Expand Down
17 changes: 13 additions & 4 deletions src/no-mix-dearu-desumasu.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,28 +15,37 @@ const defaultOptions = {
preferInBody: "", // "である" or "ですます"
preferInList: "", // "である" or "ですます"
// 文末以外でも、敬体(ですます調)と常体(である調)を厳しくチェックするかどうか
strict: false
strict: false,
// preferInでの設定を、"優先" ではなく、"強制" するかどうか
// 有効化した場合、被検査テキストの表現が統一されていても、
// preferInオプションで指定された表現に強制する
enforcePreferences: false
};

module.exports = function noMixedDearuDesumasu(context, options = defaultOptions) {
const { Syntax, getSource } = context;
const helper = new RuleHelper(context);
const ignoreManager = new IgnoreNodeManager();
const isStrict = options.strict !== undefined ? options.strict : defaultOptions.strict;
const isEnforcePreferences =
options.enforcePreferences !== undefined ? options.enforcePreferences : defaultOptions.enforcePreferences;
const bodyChecker = new BodyMixedChecker(context, {
preferDesumasu: options.preferInBody === PreferTypes.DESUMASU,
preferDearu: options.preferInBody === PreferTypes.DEARU,
isStrict
isStrict,
isEnforcePreferences
});
const headerChecker = new HeaderMixedChecker(context, {
preferDesumasu: options.preferInHeader === PreferTypes.DESUMASU,
preferDearu: options.preferInHeader === PreferTypes.DEARU,
isStrict
isStrict,
isEnforcePreferences
});
const listChecker = new ListMixedChecker(context, {
preferDesumasu: options.preferInList === PreferTypes.DESUMASU,
preferDearu: options.preferInList === PreferTypes.DEARU,
isStrict
isStrict,
isEnforcePreferences
});
return {
// 見出し
Expand Down
153 changes: 153 additions & 0 deletions test/no-mix-dearu-desumasu-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -246,6 +246,7 @@ Total:
}
]
},

// 箇条書き間での混在
{
text: `
Expand Down Expand Up @@ -502,6 +503,158 @@ Total:
column: 12
}
]
},

// 強制オプションを指定した場合
{
text: `今日はいい天気である。
明日はいい天気である。`,
options: {
preferInBody: "ですます",
enforcePreferences: true
},
errors: [
// enforcePreferencesオプションにより"ですます"を強制した場合、"である"に統一されていても、"ですます"を優先する
{
message: `本文: "である"調 と "ですます"調 が混在
=> "ですます"調 の文体に、次の "である"調 の箇所があります: "である。"
Total:
である : 2
ですます: 0
`,
line: 1,
column: 8
},
{
message: `本文: "である"調 と "ですます"調 が混在
=> "ですます"調 の文体に、次の "である"調 の箇所があります: "である。"
Total:
である : 2
ですます: 0
`,
line: 2,
column: 8
}
]
},
{
text: `今日はいい天気ですね。
明日はいい天気ですね。`,
options: {
preferInBody: "である",
enforcePreferences: true
},
errors: [
// enforcePreferencesオプションにより"である"を強制した場合、"ですます"に統一されていても、"である"を優先する
{
message: `本文: "である"調 と "ですます"調 が混在
=> "である"調 の文体に、次の "ですます"調 の箇所があります: "ですね。"
Total:
である : 0
ですます: 2
`,
line: 1,
column: 8
},
{
message: `本文: "である"調 と "ですます"調 が混在
=> "である"調 の文体に、次の "ですます"調 の箇所があります: "ですね。"
Total:
である : 0
ですます: 2
`,
line: 2,
column: 8
}
]
},
{
text: `
# 今日はいい天気である
`,
options: {
preferInHeader: "ですます",
enforcePreferences: true
},
errors: [
// enforcePreferencesオプションにより"ですます"を強制した場合、"である"に統一されていても、"ですます"を優先する
{
message: `見出し: "である"調 と "ですます"調 が混在
=> "ですます"調 の文体に、次の "である"調 の箇所があります: "である"
Total:
である : 1
ですます: 0
`,
line: 2,
column: 10
}
]
},
{
text: `
# 今日はいい天気になりますね
`,
options: {
preferInHeader: "である",
enforcePreferences: true
},
errors: [
// enforcePreferencesオプションにより"である"を強制した場合、"ですます"に統一されていても、"である"を優先する
{
message: `見出し: "である"調 と "ですます"調 が混在
=> "である"調 の文体に、次の "ですます"調 の箇所があります: "ますね"
Total:
である : 0
ですます: 1
`,
line: 2,
column: 13
}
]
},
{
text: `
- 今日はいい天気である
`,
options: {
preferInList: "ですます",
enforcePreferences: true
},
errors: [
// enforcePreferencesオプションにより"ですます"を強制した場合、"である"に統一されていても、"ですます"を優先する
{
message: `箇条書き: "である"調 と "ですます"調 が混在
=> "ですます"調 の文体に、次の "である"調 の箇所があります: "である"
Total:
である : 1
ですます: 0
`,
line: 2,
column: 10
}
]
},
{
text: `
- 今日はいい天気になりますね
`,
options: {
preferInList: "である",
enforcePreferences: true
},
errors: [
// enforcePreferencesオプションにより"である"を強制した場合、"ですます"に統一されていても、"である"を優先する
{
message: `箇条書き: "である"調 と "ですます"調 が混在
=> "である"調 の文体に、次の "ですます"調 の箇所があります: "ますね"
Total:
である : 0
ですます: 1
`,
line: 2,
column: 13
}
]
}
]
});
Loading