Skip to content

Commit 3756dd2

Browse files
authored
fix(deps): update to kuromojin@3 (#29)
* fix(deps): update to kuromojin@3 * style: apply prettier
1 parent 19fccda commit 3756dd2

File tree

11 files changed

+2071
-1918
lines changed

11 files changed

+2071
-1918
lines changed

.githook/pre-commit

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
#!/bin/sh
2+
npx --no-install lint-staged

.github/workflows/test.yml

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
name: test
2+
on: [push, pull_request]
3+
jobs:
4+
test:
5+
name: "Test on Node.js ${{ matrix.node-version }}"
6+
runs-on: ubuntu-latest
7+
strategy:
8+
matrix:
9+
node-version: [12, 14]
10+
steps:
11+
- name: checkout
12+
uses: actions/checkout@v2
13+
- name: setup Node.js ${{ matrix.node-version }}
14+
uses: actions/setup-node@v2
15+
with:
16+
node-version: ${{ matrix.node-version }}
17+
- name: Install
18+
run: yarn install
19+
- name: Test
20+
run: yarn test

.travis.yml

Lines changed: 0 additions & 3 deletions
This file was deleted.

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# textlint-rule-ja-no-redundant-expression [![Build Status](https://travis-ci.org/textlint-ja/textlint-rule-ja-no-redundant-expression.svg?branch=master)](https://travis-ci.org/textlint-ja/textlint-rule-ja-no-redundant-expression) [![textlint rule](https://img.shields.io/badge/textlint-fixable-green.svg?style=social)](https://textlint.github.io/)
1+
# textlint-rule-ja-no-redundant-expression [![Actions Status: test](https://github.com/textlint-ja/textlint-rule-ja-no-redundant-expression.svg?branch=master)](https://travis-ci.org/textlint-ja/textlint-rule-ja-no-redundant-expression) [![textlint rule](https://img.shields.io/badge/textlint-fixable-green/workflows/test/badge.svg)](https://github.com/textlint-ja/textlint-rule-ja-no-redundant-expression.svg?branch=master)](https://travis-ci.org/textlint-ja/textlint-rule-ja-no-redundant-expression) [![textlint rule](https://img.shields.io/badge/textlint-fixable-green/actions?query=workflow%3A"test")
22

33
冗長な表現を禁止するtextlintルールです。
44

package.json

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,8 @@
1919
"prepublish": "npm run docs && npm run --if-present build",
2020
"build": "textlint-scripts build",
2121
"watch": "textlint-scripts build --watch",
22-
"prettier": "prettier --write \"**/*.{js,jsx,ts,tsx,css}\""
22+
"prettier": "prettier --write \"**/*.{js,jsx,ts,tsx,css}\"",
23+
"prepare": "git config --local core.hooksPath .githook"
2324
},
2425
"repository": {
2526
"type": "git",
@@ -30,20 +31,20 @@
3031
},
3132
"homepage": "https://github.com/textlint-ja/textlint-rule-ja-no-redundant-expression",
3233
"devDependencies": {
33-
"@textlint/types": "^1.2.2",
34-
"@types/mocha": "^5.2.7",
35-
"@types/node": "^12.11.1",
34+
"@textlint/types": "^1.5.4",
35+
"@types/mocha": "^8.2.2",
36+
"@types/node": "^14.14.41",
3637
"add-text-to-markdown": "^2.0.0",
3738
"husky": "^3.0.9",
38-
"lint-staged": "^9.4.2",
39-
"prettier": "^1.18.2",
39+
"lint-staged": "^10.5.4",
40+
"prettier": "^2.2.1",
4041
"textlint-scripts": "^3.0.0",
41-
"ts-node": "^8.10.2",
42-
"typescript": "^3.9.7"
42+
"ts-node": "^9.1.1",
43+
"typescript": "^4.2.4"
4344
},
4445
"dependencies": {
4546
"@textlint/regexp-string-matcher": "^1.1.0",
46-
"kuromojin": "^2.0.0",
47+
"kuromojin": "^3.0.0",
4748
"morpheme-match": "^2.0.4",
4849
"morpheme-match-all": "^2.0.5",
4950
"textlint-rule-helper": "^2.1.1",
@@ -55,7 +56,8 @@
5556
"prettier": {
5657
"singleQuote": false,
5758
"printWidth": 120,
58-
"tabWidth": 4
59+
"tabWidth": 4,
60+
"trailingComma": "none"
5961
},
6062
"husky": {
6163
"hooks": {
@@ -64,8 +66,7 @@
6466
},
6567
"lint-staged": {
6668
"*.{js,jsx,ts,tsx,css}": [
67-
"prettier --write",
68-
"git add"
69+
"prettier --write"
6970
]
7071
}
7172
}

src/dictionary.ts

Lines changed: 41 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -7,58 +7,55 @@ const punctuations = ["、", "、", ",", ","];
77
*/
88
export const ExpectedType = {
99
// Expectedへの変換自体を取りやめる
10-
"STOP_REPLACE": "STOP_REPLACE"
10+
STOP_REPLACE: "STOP_REPLACE"
1111
};
1212

13-
1413
export type ExpectedTokenAdditional = {
1514
_skippable?: boolean;
1615
};
1716

1817
// ExpectedToken is based on Token
1918
// But support array of each properties
2019
export type ExpectedToken = {
21-
// 辞書内での単語ID
22-
word_id?: number | number[];
23-
// 単語タイプ(辞書に登録されている単語ならKNOWN; 未知語ならUNKNOWN)
24-
word_type?: "KNOWN" | "UNKNOWN";
25-
// 表層形
26-
surface_form?: string | string[];
27-
// 品詞
28-
pos?: string | string[];
29-
// 品詞細分類1
30-
pos_detail_1?: string | string[];
31-
// 品詞細分類2
32-
pos_detail_2?: string | string[];
33-
// 品詞細分類3
34-
pos_detail_3?: string | string[];
35-
// 活用型
36-
conjugated_type?: string | string[];
37-
// 活用形
38-
conjugated_form?: string | string[];
39-
// 基本形
40-
basic_form?: string | string[];
41-
// 読み
42-
reading?: string | string[];
43-
// 発音
44-
pronunciation?: string | string[];
45-
// 単語の開始位置
46-
word_position?: number | number[];
47-
}
48-
& ExpectedTokenAdditional
49-
& {
50-
[index: string]: any;
51-
};
20+
// 辞書内での単語ID
21+
word_id?: number | number[];
22+
// 単語タイプ(辞書に登録されている単語ならKNOWN; 未知語ならUNKNOWN)
23+
word_type?: "KNOWN" | "UNKNOWN";
24+
// 表層形
25+
surface_form?: string | string[];
26+
// 品詞
27+
pos?: string | string[];
28+
// 品詞細分類1
29+
pos_detail_1?: string | string[];
30+
// 品詞細分類2
31+
pos_detail_2?: string | string[];
32+
// 品詞細分類3
33+
pos_detail_3?: string | string[];
34+
// 活用型
35+
conjugated_type?: string | string[];
36+
// 活用形
37+
conjugated_form?: string | string[];
38+
// 基本形
39+
basic_form?: string | string[];
40+
// 読み
41+
reading?: string | string[];
42+
// 発音
43+
pronunciation?: string | string[];
44+
// 単語の開始位置
45+
word_position?: number | number[];
46+
} & ExpectedTokenAdditional & {
47+
[index: string]: any;
48+
};
5249
export type ExpectedDictionary = {
5350
id: string;
54-
disabled: boolean
51+
disabled: boolean;
5552
allows: string[];
56-
message: string
53+
message: string;
5754
url: string;
5855
expected?: string;
5956
description?: string;
60-
tokens: ExpectedToken[]
61-
}
57+
tokens: ExpectedToken[];
58+
};
6259

6360
export const Dictionary: ExpectedDictionary[] = [
6461
{
@@ -307,13 +304,13 @@ export const Dictionary: ExpectedDictionary[] = [
307304
basic_form: "いる"
308305
},
309306
{
310-
"surface_form": "ます",
311-
"pos": "助動詞",
312-
"conjugated_type": "特殊・マス",
313-
"conjugated_form": "基本形",
314-
"basic_form": "ます",
315-
"reading": "マス",
316-
"pronunciation": "マス",
307+
surface_form: "ます",
308+
pos: "助動詞",
309+
conjugated_type: "特殊・マス",
310+
conjugated_form: "基本形",
311+
basic_form: "ます",
312+
reading: "マス",
313+
pronunciation: "マス",
317314
_skippable: true
318315
}
319316
]

src/index.ts

Lines changed: 41 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ const replaceTokenWith = (matcherToken: any, actualToken: KuromojiToken, special
3636
* @returns {string}
3737
*/
3838
const tokensToString = (tokens: KuromojiToken[]) => {
39-
return tokens.map(token => token.surface_form).join("");
39+
return tokens.map((token) => token.surface_form).join("");
4040
};
4141

4242
/**
@@ -62,8 +62,16 @@ const isTokensAllowed = (tokens: KuromojiToken[], allows: string[]) => {
6262
* @param {*[]} actualTokens
6363
* @returns {null|string}
6464
*/
65-
const createExpected = ({ expected, matcherTokens, skipped, actualTokens }: {
66-
expected?: string, matcherTokens: any[], skipped: boolean[], actualTokens: KuromojiToken[]
65+
const createExpected = ({
66+
expected,
67+
matcherTokens,
68+
skipped,
69+
actualTokens
70+
}: {
71+
expected?: string;
72+
matcherTokens: any[];
73+
skipped: boolean[];
74+
actualTokens: KuromojiToken[];
6775
}): null | string => {
6876
if (!expected) {
6977
return null;
@@ -89,7 +97,19 @@ const createExpected = ({ expected, matcherTokens, skipped, actualTokens }: {
8997
return resultText;
9098
};
9199

92-
const createMessage = ({ id, text, matcherTokens, skipped, actualTokens }: { id: string, text: string, matcherTokens: any[], skipped: boolean[], actualTokens: KuromojiToken[] }) => {
100+
const createMessage = ({
101+
id,
102+
text,
103+
matcherTokens,
104+
skipped,
105+
actualTokens
106+
}: {
107+
id: string;
108+
text: string;
109+
matcherTokens: any[];
110+
skipped: boolean[];
111+
actualTokens: KuromojiToken[];
112+
}) => {
93113
let resultText = text;
94114
let actualTokenIndex = 0;
95115
matcherTokens.forEach((token, index) => {
@@ -115,8 +135,8 @@ export interface Options {
115135
dictOptions?: {
116136
[index: string]: {
117137
disabled?: boolean;
118-
allows?: string[]
119-
}
138+
allows?: string[];
139+
};
120140
};
121141
// - 無視したいNode typeを配列で指定
122142
// - Node typeは <https://textlint.github.io/docs/txtnode.html#type> を参照
@@ -132,7 +152,7 @@ const reporter: TextlintRuleModule<Options> = (context, options = {}) => {
132152
};
133153
const dictOptions = options.dictOptions || DefaultOptions.dictOptions;
134154
// "disabled": trueな辞書は取り除く
135-
const enabledDictionaryList = Dictionary.filter(dict => {
155+
const enabledDictionaryList = Dictionary.filter((dict) => {
136156
const dictOption = dictOptions[dict.id] || {};
137157
const disabled = typeof dictOption.disabled === "boolean" ? dictOption.disabled : dict.disabled;
138158
return !disabled;
@@ -144,14 +164,14 @@ const reporter: TextlintRuleModule<Options> = (context, options = {}) => {
144164
{
145165
ignoreNodeTypes: skipNodeTypes
146166
},
147-
report => {
167+
(report) => {
148168
return {
149169
[Syntax.Paragraph](node) {
150170
const source = new StringSource(node);
151171
const text = source.toString();
152-
return tokenize(text).then(currentTokens => {
172+
return tokenize(text).then((currentTokens) => {
153173
const matchResults = matchAll(currentTokens);
154-
matchResults.forEach(matchResult => {
174+
matchResults.forEach((matchResult) => {
155175
const dictOption = dictOptions[matchResult.dict.id] || {};
156176
// "allows" オプションにマッチした場合はエラーを報告しない
157177
const allows = dictOption.allows || matchResult.dict.allows;
@@ -162,21 +182,18 @@ const reporter: TextlintRuleModule<Options> = (context, options = {}) => {
162182
// エラー報告
163183
const firstToken = matchResult.tokens[0];
164184
const lastToken = matchResult.tokens[matchResult.tokens.length - 1];
165-
const firstWordIndex = source.originalIndexFromIndex(
166-
Math.max(firstToken.word_position - 1, 0)
167-
) || 0;
168-
const lastWordIndex = source.originalIndexFromIndex(
169-
Math.max(lastToken.word_position - 1, 0)
170-
) || 0;
185+
const firstWordIndex =
186+
source.originalIndexFromIndex(Math.max(firstToken.word_position - 1, 0)) || 0;
187+
const lastWordIndex =
188+
source.originalIndexFromIndex(Math.max(lastToken.word_position - 1, 0)) || 0;
171189
// エラーメッセージ
172-
const message =
173-
createMessage({
174-
id: matchResult.dict.id,
175-
text: matchResult.dict.message,
176-
matcherTokens: matchResult.dict.tokens,
177-
skipped: matchResult.skipped,
178-
actualTokens: matchResult.tokens
179-
});
190+
const message = createMessage({
191+
id: matchResult.dict.id,
192+
text: matchResult.dict.message,
193+
matcherTokens: matchResult.dict.tokens,
194+
skipped: matchResult.skipped,
195+
actualTokens: matchResult.tokens
196+
});
180197
// 置換結果
181198
const expected = createExpected({
182199
expected: matchResult.dict.expected,

test/dictionary-test.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,21 +4,21 @@ import assert from "assert";
44

55
import { Dictionary } from "../src/dictionary";
66

7-
describe("dictionary", function() {
7+
describe("dictionary", function () {
88
it("should not have duplicated id", () => {
9-
Dictionary.forEach(item => {
9+
Dictionary.forEach((item) => {
1010
assert.strictEqual(typeof item.id, "string", "should have id property");
11-
const sameIdItems = Dictionary.filter(target => target.id === item.id);
11+
const sameIdItems = Dictionary.filter((target) => target.id === item.id);
1212
assert.ok(sameIdItems.length === 1, "should not have duplicated id item");
1313
});
1414
});
1515
it("should have disabled default value", () => {
16-
Dictionary.forEach(item => {
16+
Dictionary.forEach((item) => {
1717
assert.strictEqual(typeof item.disabled, "boolean", `${item} should have disabled property`);
1818
});
1919
});
2020
it("should have allows default value", () => {
21-
Dictionary.forEach(item => {
21+
Dictionary.forEach((item) => {
2222
assert.ok(Array.isArray(item.allows), `${item}: should have disabled property`);
2323
});
2424
});

test/index-test.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ tester.run("textlint-rule-ja-no-redundant-expression", rule, {
2828
text: "処理を行う",
2929
options: {
3030
dictOptions: {
31-
"dict5": {
31+
dict5: {
3232
allows: ["処理"]
3333
}
3434
}
@@ -39,7 +39,7 @@ tester.run("textlint-rule-ja-no-redundant-expression", rule, {
3939
text: "これは省略することが可能だが、省略しない。",
4040
options: {
4141
dictOptions: {
42-
"dict1": {
42+
dict1: {
4343
disabled: true
4444
}
4545
}
@@ -67,7 +67,7 @@ tester.run("textlint-rule-ja-no-redundant-expression", rule, {
6767
text: "テストを行う",
6868
options: {
6969
dictOptions: {
70-
"dict5": {
70+
dict5: {
7171
allows: []
7272
}
7373
}

0 commit comments

Comments
 (0)