Skip to content

Commit 0e46d6a

Browse files
authored
Merge pull request #24 from textlint-ja/textlint-scripts-typescript
refactor: migrate to TypeScrtipt
2 parents 7de7d95 + 6d9d5f7 commit 0e46d6a

7 files changed

+1035
-596
lines changed

package.json

+8-4
Original file line numberDiff line numberDiff line change
@@ -33,12 +33,16 @@
3333
"textlintrule"
3434
],
3535
"devDependencies": {
36-
"textlint-scripts": "^2.1.0"
36+
"@textlint/types": "^1.2.2",
37+
"@types/node": "^12.11.1",
38+
"textlint-scripts": "^3.0.0",
39+
"ts-node": "^8.4.1",
40+
"typescript": "^3.6.4"
3741
},
3842
"dependencies": {
39-
"kuromojin": "^1.2.1",
40-
"sentence-splitter": "^3.0.11",
43+
"kuromojin": "^2.0.0",
44+
"sentence-splitter": "^3.1.0",
4145
"textlint-rule-helper": "^2.1.1",
42-
"textlint-util-to-string": "^2.1.1"
46+
"textlint-util-to-string": "^3.0.0"
4347
}
4448
}

src/no-doubled-joshi.js renamed to src/no-doubled-joshi.ts

+24-14
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,17 @@
11
// LICENSE : MIT
22
"use strict";
3-
import {RuleHelper} from "textlint-rule-helper";
4-
import {getTokenizer} from "kuromojin";
5-
import {splitAST as splitSentences, Syntax as SentenceSyntax} from "sentence-splitter";
6-
import StringSource from "textlint-util-to-string";
3+
import { RuleHelper } from "textlint-rule-helper";
4+
import { splitAST as splitSentences, Syntax as SentenceSyntax, SentenceNode } from "sentence-splitter";
5+
import { getTokenizer, KuromojiToken } from "kuromojin";
76
import {
87
is助詞Token, is読点Token,
98
concatJoishiTokens,
109
createKeyFromKey,
1110
restoreToSurfaceFromKey
1211
} from "./token-utils";
12+
import { TxtNode } from "@textlint/ast-node-types";
13+
import { TextlintRuleModule } from "@textlint/types";
14+
import { StringSource } from "textlint-util-to-string";
1315

1416
/**
1517
* Create token map object
@@ -19,7 +21,7 @@ import {
1921
* @param tokens
2022
* @returns {*}
2123
*/
22-
function createSurfaceKeyMap(tokens) {
24+
function createSurfaceKeyMap(tokens: KuromojiToken[]): { [index: string]: KuromojiToken[] } {
2325
// 助詞のみを対象とする
2426
return tokens.filter(is助詞Token).reduce((keyMap, token) => {
2527
// "は:助詞.係助詞" : [token]
@@ -29,10 +31,10 @@ function createSurfaceKeyMap(tokens) {
2931
}
3032
keyMap[tokenKey].push(token);
3133
return keyMap;
32-
}, {});
34+
}, {} as { [index: string]: KuromojiToken[] });
3335
}
3436

35-
function matchExceptionRule(tokens) {
37+
function matchExceptionRule(tokens: KuromojiToken[]) {
3638
let token = tokens[0];
3739
// "の" の重なりは例外
3840
if (token.pos_detail_1 === "連体化") {
@@ -59,6 +61,14 @@ const defaultOptions = {
5961
separatorChars: ["。", "?", "!", "?", "!"]
6062
};
6163

64+
65+
export interface Options {
66+
min_interval?: number;
67+
strict?: boolean;
68+
allow?: string[];
69+
separatorChars?: string[]
70+
}
71+
6272
/*
6373
1. Paragraph Node -> text
6474
2. text -> sentences
@@ -67,26 +77,25 @@ const defaultOptions = {
6777
6878
TODO: need abstraction
6979
*/
70-
module.exports = function (context, options = {}) {
80+
const report: TextlintRuleModule<Options> = function (context, options = {}) {
7181
const helper = new RuleHelper(context);
7282
// 最低間隔値
7383
const minInterval = options.min_interval || defaultOptions.min_interval;
7484
const isStrict = options.strict || defaultOptions.strict;
7585
const allow = options.allow || defaultOptions.allow;
76-
const separatorChars = options.separatorChars || defaultOptions.separatorChars;
7786
const {Syntax, report, RuleError} = context;
7887
return {
7988
[Syntax.Paragraph](node) {
8089
if (helper.isChildNode(node, [Syntax.Link, Syntax.Image, Syntax.BlockQuote, Syntax.Emphasis])) {
8190
return;
8291
}
83-
const isSentenceNode = node => {
92+
const isSentenceNode = (node: TxtNode): node is SentenceNode => {
8493
return node.type === SentenceSyntax.Sentence;
8594
};
8695
const txtParentNode = splitSentences(node);
8796
const sentences = txtParentNode.children.filter(isSentenceNode);
88-
return getTokenizer().then(tokenizer => {
89-
const checkSentence = (sentence) => {
97+
return getTokenizer().then((tokenizer: any) => {
98+
const checkSentence = (sentence: SentenceNode) => {
9099
const sentenceSource = new StringSource(sentence);
91100
const text = sentenceSource.toString();
92101
const tokens = tokenizer.tokenizeForSentence(text);
@@ -115,7 +124,7 @@ module.exports = function (context, options = {}) {
115124
}
116125
*/
117126
Object.keys(joshiTokenSurfaceKeyMap).forEach(key => {
118-
const tokens = joshiTokenSurfaceKeyMap[key];
127+
const tokens: KuromojiToken[] = joshiTokenSurfaceKeyMap[key];
119128
const joshiName = restoreToSurfaceFromKey(key);
120129
// check allow
121130
if (allow.indexOf(joshiName) >= 0) {
@@ -148,8 +157,9 @@ module.exports = function (context, options = {}) {
148157
});
149158
});
150159
};
151-
sentences.forEach(checkSentence);
160+
sentences.forEach(node => checkSentence(node))
152161
});
153162
}
154163
}
155164
};
165+
export default report;

src/token-utils.js renamed to src/token-utils.ts

+11-8
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,15 @@
11
// LICENSE : MIT
22
"use strict";
3+
4+
import { KuromojiToken } from "kuromojin";
5+
36
// 助詞どうか
4-
export const is助詞Token = (token) => {
7+
export const is助詞Token = (token: KuromojiToken) => {
58
// 結合しているtokenは助詞助詞のようになってるため先頭一致で見る
69
return token && /^/.test(token.pos);
710
};
811

9-
export const is読点Token = (token) => {
12+
export const is読点Token = (token: KuromojiToken) => {
1013
return token.surface_form === "、" && token.pos === "名詞";
1114
};
1215
/**
@@ -15,7 +18,7 @@ export const is読点Token = (token) => {
1518
* @param {Object} bToken
1619
* @returns {Object}
1720
*/
18-
const concatToken = (aToken, bToken) => {
21+
const concatToken = (aToken: KuromojiToken, bToken: KuromojiToken) => {
1922
aToken.surface_form += bToken.surface_form;
2023
aToken.pos += bToken.pos;
2124
aToken.pos_detail_1 += bToken.surface_form;
@@ -26,8 +29,8 @@ const concatToken = (aToken, bToken) => {
2629
* @param {Array} tokens
2730
* @returns {Array}
2831
*/
29-
export const concatJoishiTokens = (tokens) => {
30-
const newTokens = [];
32+
export const concatJoishiTokens = (tokens: KuromojiToken[]) => {
33+
const newTokens: KuromojiToken[] = [];
3134
tokens.forEach((token) => {
3235
const prevToken = newTokens[newTokens.length - 1];
3336
if (is助詞Token(token) && is助詞Token(prevToken)) {
@@ -41,11 +44,11 @@ export const concatJoishiTokens = (tokens) => {
4144
// 助詞tokenから品詞細分類1までを元にしたkeyを作る
4245
// http://www.unixuser.org/~euske/doc/postag/index.html#chasen
4346
// http://chasen.naist.jp/snapshot/ipadic/ipadic/doc/ipadic-ja.pdf
44-
export const createKeyFromKey = (token) => {
47+
export const createKeyFromKey = (token: KuromojiToken) => {
4548
// e.g.) "は:助詞.係助詞"
4649
return `${token.surface_form}:${token.pos}.${token.pos_detail_1}`;
4750
};
4851
// keyからsurfaceを取り出す
49-
export const restoreToSurfaceFromKey = (key) => {
52+
export const restoreToSurfaceFromKey = (key: string) => {
5053
return key.split(":")[0];
51-
};
54+
};

test/mocha.opts

-1
This file was deleted.

test/no-doubled-joshi-test.js renamed to test/no-doubled-joshi-test.ts

+2-6
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,8 @@
11
import TextLintTester from "textlint-tester";
2-
const rule = require("../src/no-doubled-joshi");
2+
import rule from "../src/no-doubled-joshi";
3+
34
const tester = new TextLintTester();
4-
/*
5-
`**`のような装飾は取り除かれてから評価されているので、
6-
テストでの強調という意味合いのみで利用する。
7-
*/
85
tester.run("no-double-joshi", rule, {
9-
106
valid: [
117
"私は彼が好きだ",
128
"既存のコードの利用", // "の" の例外

tsconfig.json

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
{
2+
"compilerOptions": {
3+
/* Basic Options */
4+
"module": "commonjs",
5+
"moduleResolution": "node",
6+
"esModuleInterop": true,
7+
"noEmit": true,
8+
"skipLibCheck": true,
9+
"target": "es2015",
10+
/* Strict Type-Checking Options */
11+
"strict": true,
12+
/* Additional Checks */
13+
/* Report errors on unused locals. */
14+
"noUnusedLocals": true,
15+
/* Report errors on unused parameters. */
16+
"noUnusedParameters": true,
17+
/* Report error when not all code paths in function return a value. */
18+
"noImplicitReturns": true,
19+
/* Report errors for fallthrough cases in switch statement. */
20+
"noFallthroughCasesInSwitch": true
21+
}
22+
}

0 commit comments

Comments
 (0)