Skip to content

Commit 8272263

Browse files
authored
refactor(rule): Convert to TypeScript (#2)
* refactor(rule): Convert to TypeScript * chore(deps): update untildify * CI: move to GitHub Actions
1 parent 00b0f3d commit 8272263

File tree

8 files changed

+2346
-1390
lines changed

8 files changed

+2346
-1390
lines changed

.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-ja/textlint-rule-morpheme-match [![Build Status](https://travis-ci.org/textlint-ja/textlint-rule-morpheme-match.svg?branch=master)](https://travis-ci.org/textlint-ja/textlint-rule-morpheme-match)
1+
# @textlint-ja/textlint-rule-morpheme-match [![Actions Status: test](https://github.com/textlint-ja/textlint-rule-morpheme-match/workflows/test/badge.svg)](https://github.com/textlint-ja/textlint-rule-morpheme-match/actions?query=workflow%3A"test")
22

33
形態素解析結果のTokenベースの辞書でマッチするtextlintルール。
44

package.json

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -38,10 +38,14 @@
3838
"tabWidth": 4
3939
},
4040
"devDependencies": {
41+
"@textlint/types": "^1.5.4",
42+
"@types/node": "^14.14.41",
4143
"husky": "^1.2.0",
42-
"lint-staged": "^8.1.0",
43-
"prettier": "^1.15.3",
44-
"textlint-scripts": "^2.1.0"
44+
"lint-staged": "^10.5.4",
45+
"prettier": "^2.2.1",
46+
"textlint-scripts": "^3.0.0",
47+
"ts-node": "^9.1.1",
48+
"typescript": "^4.2.4"
4549
},
4650
"publishConfig": {
4751
"access": "public"
@@ -58,8 +62,8 @@
5862
]
5963
},
6064
"dependencies": {
61-
"kuromojin": "^1.4.0",
62-
"morpheme-match-textlint": "^2.0.0",
63-
"untildify": "^3.0.3"
65+
"kuromojin": "^2.0.0",
66+
"morpheme-match-textlint": "^2.0.3",
67+
"untildify": "^4.0.0"
6468
}
6569
}
Lines changed: 12 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,19 @@
11
"use strict";
2+
import { TextlintRuleModule, TextlintRuleReporter } from "@textlint/types";
23
import { createTextlintMatcher } from "morpheme-match-textlint";
4+
import { tokenize } from "kuromojin";
5+
import path from "path";
6+
import untildify from "untildify";
37

4-
const tokenize = require("kuromojin").tokenize;
5-
const path = require("path");
6-
const untildify = require("untildify");
7-
8-
/**
9-
* Replace text with tokens
10-
* @param text
11-
* @param tokens
12-
* @param actualTokens
13-
* @returns {*}
14-
*/
15-
const replaceWithCaptureTokens = (text, tokens, actualTokens) => {
16-
let resultText = text;
17-
tokens.forEach((token, index) => {
18-
// when the node has not `_capture_`, does not replace it
19-
if (!token._capture) {
20-
return;
21-
}
22-
const actualToken = actualTokens[index];
23-
resultText = resultText.replace(token._capture, actualToken.surface_form);
24-
});
25-
return resultText;
26-
};
27-
28-
const flat = (array) => {
29-
return array.reduce((total, item) => total.concat(item), []);
8+
const flat = <T>(array: T[]): T[] => {
9+
return array.reduce((total, item) => total.concat(item), [] as T[]);
3010
};
3111
/**
3212
* load dictionary file(.js or .json) and return it
3313
* @param {string} baseDirectory base directory
3414
* @param {string} dictionaryFilePath .js or .json path list
3515
*/
36-
const loadDictionary = (baseDirectory, dictionaryFilePath) => {
16+
const loadDictionary = (baseDirectory: string, dictionaryFilePath: string) => {
3717
const untildifiedFilePath = untildify(dictionaryFilePath);
3818
try {
3919
const absoluteFilePath = path.resolve(baseDirectory, untildifiedFilePath);
@@ -48,7 +28,7 @@ const loadDictionary = (baseDirectory, dictionaryFilePath) => {
4828
* @param {string} baseDirectory base directory
4929
* @param {string[]} dictionaryFilePathList dictionary file list
5030
*/
51-
const loadDictionaries = (baseDirectory, dictionaryFilePathList) => {
31+
const loadDictionaries = (baseDirectory: string, dictionaryFilePathList: string[]) => {
5232
const contents = dictionaryFilePathList.map(filePath => loadDictionary(baseDirectory, filePath));
5333
return flat(contents);
5434
};
@@ -58,7 +38,7 @@ const loadDictionaries = (baseDirectory, dictionaryFilePathList) => {
5838
* @param {{dictionaryPathList:string[]}} options
5939
* @returns {*}
6040
*/
61-
const reporter = (context, options) => {
41+
const reporter: TextlintRuleReporter = (context, options = {}) => {
6242
const { Syntax, RuleError, report, fixer, getSource } = context;
6343
if (!options.dictionaryPathList) {
6444
throw new Error(`You should set "dictionaryPathList" options.
@@ -103,7 +83,8 @@ const reporter = (context, options) => {
10383
}
10484
};
10585
};
106-
module.exports = {
86+
87+
export default {
10788
linter: reporter,
10889
fixer: reporter
109-
};
90+
} as TextlintRuleModule;
Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
const path = require("path");
2-
const TextLintTester = require("textlint-tester");
1+
import TextLintTester from "textlint-tester";
2+
import path from "path";
33
const tester = new TextLintTester();
4-
const rule = require("../src/textlint-rule-morpheme-match.js");
4+
import rule from "../src/textlint-rule-morpheme-match";
55
const options = {
66
dictionaryPathList: [path.join(__dirname, "fixtures/dictionary.js")]
77
};
@@ -14,7 +14,7 @@ tester.run("textlint-rule-ja-no-redundant-expression", rule, {
1414
valid: [
1515
{ text: "text", options },
1616
{ text: "長さは可変だ", options: multipleDictOptions },
17-
{ text: "人は1人では育つことができない", options },
17+
{ text: "人は1人では育つことができない", options }
1818
],
1919
invalid: [
2020
{

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)