Skip to content

Commit 003ead3

Browse files
committed
refactor: migrateion to TypeScrtipt
1 parent 7de7d95 commit 003ead3

File tree

7 files changed

+652
-142
lines changed

7 files changed

+652
-142
lines changed

package.json

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,11 @@
3333
"textlintrule"
3434
],
3535
"devDependencies": {
36-
"textlint-scripts": "^2.1.0"
36+
"@types/node": "^12.0.12",
37+
"@textlint/types": "^1.1.5",
38+
"textlint-scripts": "^3.0.0-beta.1",
39+
"ts-node": "^8.3.0",
40+
"typescript": "^3.5.2"
3741
},
3842
"dependencies": {
3943
"kuromojin": "^1.2.1",

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

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,17 @@
11
// LICENSE : MIT
22
"use strict";
33
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";
4+
import {splitAST as splitSentences, Syntax as SentenceSyntax, SentenceNode} from "sentence-splitter";
5+
const StringSource = require("textlint-util-to-string");
6+
const {getTokenizer} = require("kuromojin");
77
import {
88
is助詞Token, is読点Token,
99
concatJoishiTokens,
1010
createKeyFromKey,
11-
restoreToSurfaceFromKey
11+
restoreToSurfaceFromKey, Token
1212
} from "./token-utils";
13+
import {TextlintRuleModule} from "@textlint/types";
14+
import {TxtNode} from "@textlint/types/lib/ast-node-types/src";
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: Token[]) {
2325
// 助詞のみを対象とする
2426
return tokens.filter(is助詞Token).reduce((keyMap, token) => {
2527
// "は:助詞.係助詞" : [token]
@@ -32,7 +34,7 @@ function createSurfaceKeyMap(tokens) {
3234
}, {});
3335
}
3436

35-
function matchExceptionRule(tokens) {
37+
function matchExceptionRule(tokens: Token[]) {
3638
let token = tokens[0];
3739
// "の" の重なりは例外
3840
if (token.pos_detail_1 === "連体化") {
@@ -73,20 +75,19 @@ module.exports = function (context, options = {}) {
7375
const minInterval = options.min_interval || defaultOptions.min_interval;
7476
const isStrict = options.strict || defaultOptions.strict;
7577
const allow = options.allow || defaultOptions.allow;
76-
const separatorChars = options.separatorChars || defaultOptions.separatorChars;
7778
const {Syntax, report, RuleError} = context;
7879
return {
7980
[Syntax.Paragraph](node) {
8081
if (helper.isChildNode(node, [Syntax.Link, Syntax.Image, Syntax.BlockQuote, Syntax.Emphasis])) {
8182
return;
8283
}
83-
const isSentenceNode = node => {
84+
const isSentenceNode = (node: TxtNode): node is SentenceNode => {
8485
return node.type === SentenceSyntax.Sentence;
8586
};
8687
const txtParentNode = splitSentences(node);
8788
const sentences = txtParentNode.children.filter(isSentenceNode);
88-
return getTokenizer().then(tokenizer => {
89-
const checkSentence = (sentence) => {
89+
return getTokenizer().then((tokenizer: any) => {
90+
const checkSentence = (sentence: SentenceNode) => {
9091
const sentenceSource = new StringSource(sentence);
9192
const text = sentenceSource.toString();
9293
const tokens = tokenizer.tokenizeForSentence(text);
@@ -115,7 +116,7 @@ module.exports = function (context, options = {}) {
115116
}
116117
*/
117118
Object.keys(joshiTokenSurfaceKeyMap).forEach(key => {
118-
const tokens = joshiTokenSurfaceKeyMap[key];
119+
const tokens: Token[] = joshiTokenSurfaceKeyMap[key];
119120
const joshiName = restoreToSurfaceFromKey(key);
120121
// check allow
121122
if (allow.indexOf(joshiName) >= 0) {
@@ -148,8 +149,8 @@ module.exports = function (context, options = {}) {
148149
});
149150
});
150151
};
151-
sentences.forEach(checkSentence);
152+
sentences.forEach(node => checkSentence(node))
152153
});
153154
}
154155
}
155-
};
156+
} as TextlintRuleModule;

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

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

9-
export const is読点Token = (token) => {
12+
export const is読点Token = (token: Token) => {
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: Token, bToken: Token) => {
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: Token[]) => {
33+
const newTokens: Token[] = [];
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: Token) => {
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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
--require textlint-scripts/register
1+
--require textlint-scripts/register-ts
File renamed without changes.

tsconfig.json

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

0 commit comments

Comments
 (0)