Skip to content

Commit 03487a0

Browse files
authored
refactor(deps): use morpheme-match-textlint (#8)
* refactor(deps): use textlint-scripts and morpheme-match-textlint * chore: unused module
1 parent 48114b8 commit 03487a0

7 files changed

+3888
-58
lines changed

.babelrc

-13
This file was deleted.

package.json

+5-11
Original file line numberDiff line numberDiff line change
@@ -23,28 +23,22 @@
2323
"test": "test"
2424
},
2525
"scripts": {
26-
"test": "mocha test/",
27-
"build": "NODE_ENV=production babel src --out-dir lib --source-maps",
28-
"watch": "babel src --out-dir lib --watch --source-maps",
26+
"test": "textlint-scripts test",
27+
"build": "textlint-scripts build",
28+
"watch": "textlint-scripts build --watch",
2929
"prepublish": "npm run --if-present build"
3030
},
3131
"keywords": [
3232
"textlint",
3333
"textlintrule"
3434
],
3535
"devDependencies": {
36-
"babel-cli": "^6.10.1",
37-
"babel-preset-es2015": "^6.9.0",
38-
"babel-preset-jsdoc-to-assert": "^2.0.1",
39-
"babel-preset-power-assert": "^1.0.0",
40-
"babel-register": "^6.9.0",
41-
"mocha": "^2.5.3",
4236
"power-assert": "^1.4.1",
43-
"textlint-tester": "^1.2.0"
37+
"textlint-scripts": "^2.1.0"
4438
},
4539
"dependencies": {
4640
"kuromojin": "^1.3.1",
47-
"morpheme-match": "^1.0.1",
41+
"morpheme-match-textlint": "^2.0.0",
4842
"textlint-rule-prh": "^3.1.1"
4943
}
5044
}

src/textlint-rule-ja-no-abusage.js

+21-29
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,18 @@
11
// LICENSE : MIT
22
"use strict";
3+
import {createTextlintMatcher} from "morpheme-match-textlint"
4+
35
const tokenize = require("kuromojin").tokenize;
46
const fs = require("fs");
57
const path = require("path");
68
const prh = require("textlint-rule-prh");
79
const dictionaryList = require("./dictionary");
8-
const createTokenMatcher = require("morpheme-match");
10+
911
const reporter = (context) => {
1012
const {Syntax, RuleError, report, fixer, getSource} = context;
11-
const matcherList = dictionaryList.map(dict => {
12-
return {
13-
matcher: createTokenMatcher(dict["tokens"]),
14-
message: dict["message"],
15-
expected: dict["expected"]
16-
};
13+
const matcherList = createTextlintMatcher({
14+
tokenize: tokenize,
15+
dictionaries: dictionaryList
1716
});
1817
const prhLinter = prh.linter;
1918
const prhStr = prhLinter(context, {
@@ -22,29 +21,22 @@ const reporter = (context) => {
2221
]
2322
});
2423
return {
25-
[Syntax.Str](node){
24+
[Syntax.Str](node) {
2625
const text = getSource(node);
2726
prhStr[Syntax.Str](node);
28-
return tokenize(text).then(currentTokens => {
29-
currentTokens.forEach(token => {
30-
matcherList.forEach(({matcher, message, expected}) => {
31-
const {match, tokens} = matcher(token);
32-
if (!match) {
33-
return;
34-
}
35-
const firstToken = tokens[0];
36-
const index = Math.max(firstToken.word_position - 1, 0);
37-
if (expected) {
38-
report(node, new RuleError(message, {
39-
index: index,
40-
fix: fixer.replaceTextRange([index, index + expected.length], expected)
41-
}));
42-
} else {
43-
report(node, new RuleError(message, {
44-
index: index
45-
}));
46-
}
47-
});
27+
return matcherList(text).then(results => {
28+
results.forEach(result => {
29+
if (result.expected) {
30+
report(node, new RuleError(result.message, {
31+
index: result.index,
32+
fix: fixer.replaceTextRange(result.range, result.expected)
33+
}));
34+
} else {
35+
report(node, new RuleError(result.message, {
36+
index: result.index
37+
}));
38+
}
39+
4840
});
4941
});
5042
}
@@ -53,4 +45,4 @@ const reporter = (context) => {
5345
module.exports = {
5446
linter: reporter,
5547
fixer: reporter
56-
};
48+
};

test/mocha.opts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
--compilers js:babel-register
1+
--require textlint-scripts/register

test/no-confusing-adjust-and-apply-test.js

+3-2
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,8 @@ tester.run("textlint-rule-no-confusing-adjust-and-apply", rule, {
3232
output: "`logger`と`crashReporter`のmiddlewareを適用した`createStore`関数を作る",
3333
errors: [
3434
{
35-
message: `"適用"の誤用である可能性があります。適応 => 適用`
35+
message: `"適用"の誤用である可能性があります。適応 => 適用`,
36+
index: 35
3637
}
3738
]
3839
}, {
@@ -77,4 +78,4 @@ tester.run("textlint-rule-no-confusing-adjust-and-apply", rule, {
7778
]
7879
}
7980
]
80-
});
81+
});

test/no-variable-test.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,9 @@ tester.run("可変", rule, {
1515
errors: [
1616
{
1717
message: `「可変する」という使い方は適切ではありません。「可逆」と同じ使い方になります。\nhttp://qiita.com/scivola/items/f02589968a4ca27bc52b`,
18-
index: 8
18+
index: 7
1919
}
2020
]
2121
}
2222
]
23-
});
23+
});

0 commit comments

Comments
 (0)