Skip to content

Commit

Permalink
fix: add validation to preferIn option (#45)
Browse files Browse the repository at this point in the history
* fix: add prefer option validation

* fix
  • Loading branch information
azu authored Jan 1, 2024
1 parent c4862f2 commit 8ed6a42
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 2 deletions.
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

0 comments on commit 8ed6a42

Please sign in to comment.