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

fix: add validation to preferIn option #45

Merged
merged 2 commits into from
Jan 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
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
18 changes: 18 additions & 0 deletions src/no-mix-dearu-desumasu.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { RuleHelper, IgnoreNodeManager } from "textlint-rule-helper";
import BodyMixedChecker from "./BodyMixedChecker";
import HeaderMixedChecker from "./HeaderMixedChecker";
import ListMixedChecker from "./ListMixedChecker";

export const PreferTypes = {
DESUMASU: "ですます",
DEARU: "である"
Expand All @@ -18,11 +19,28 @@ const defaultOptions = {
strict: false
};

const allowedTypes = [/*auto*/ "", PreferTypes.DESUMASU, PreferTypes.DEARU];
const assertPreferOption = (preferType) => {
if (!preferType) {
return;
}
if (!allowedTypes.includes(preferType)) {
throw new Error(`preferInHeader, preferInBody, preferInList は ${allowedTypes.map((type) => {
return `"${type}"`;
})} のどれかである必要があります。

実際の値: "${preferType}"`);
}
};

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;
assertPreferOption(options.preferInHeader);
assertPreferOption(options.preferInBody);
assertPreferOption(options.preferInList);
const bodyChecker = new BodyMixedChecker(context, {
preferDesumasu: options.preferInBody === PreferTypes.DESUMASU,
preferDearu: options.preferInBody === PreferTypes.DEARU,
Expand Down
4 changes: 2 additions & 2 deletions test/no-mix-dearu-desumasu-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -465,7 +465,7 @@ CはDです。
一方、AとCは同じものである。
`,
options: {
preferInBody: "です",
preferInBody: "ですます",
strict: false
},
errors: [
Expand All @@ -488,7 +488,7 @@ CはDになります。
一方、AとCは同じものである。
`,
options: {
preferInBody: "です",
preferInBody: "ですます",
strict: false
},
errors: [
Expand Down
Loading