Skip to content

Commit 4d5f555

Browse files
committed
Merge pull request #1 from azu/fix-empty-reduce
Fix "TypeError: Reduce of empty array with no initial value"
2 parents d5d8c31 + 444949d commit 4d5f555

File tree

5 files changed

+33
-18
lines changed

5 files changed

+33
-18
lines changed

.babelrc

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,15 @@
11
{
2-
"presets": ["es2015"],
3-
"plugins": ["add-module-exports"]
2+
"presets": [
3+
"es2015"
4+
],
5+
"plugins": [
6+
"add-module-exports"
7+
],
8+
"env": {
9+
"development": {
10+
"presets": [
11+
"power-assert"
12+
]
13+
}
14+
}
415
}

package.json

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,22 +9,23 @@
99
},
1010
"dependencies": {
1111
"kuromojin": "^1.1.0",
12-
"sentence-splitter": "^1.2.0",
12+
"sentence-splitter": "^2.0.0",
1313
"textlint-rule-helper": "^1.1.5",
1414
"textlint-util-to-string": "^1.2.0"
1515
},
1616
"devDependencies": {
1717
"babel-cli": "^6.5.1",
18-
"babel-plugin-add-module-exports": "^0.1.2",
18+
"babel-plugin-add-module-exports": "^0.2.0",
1919
"babel-preset-es2015": "^6.5.0",
20-
"espower-babel": "^4.0.1",
20+
"babel-preset-power-assert": "^1.0.0",
21+
"babel-register": "^6.8.0",
2122
"mocha": "^2.4.5",
22-
"power-assert": "^1.2.0",
23-
"textlint": "^5.2.1",
24-
"textlint-tester": "^0.4.1"
23+
"power-assert": "^1.4.0",
24+
"textlint": "^6.6.0",
25+
"textlint-tester": "^1.2.0"
2526
},
2627
"scripts": {
27-
"build": "babel src --out-dir lib --source-maps",
28+
"build": "NODE_ENV=production babel src --out-dir lib --source-maps",
2829
"watch": "babel src --out-dir lib --watch --source-maps",
2930
"prepublish": "npm run --if-present build",
3031
"test": "mocha",

src/no-doubled-conjunction.js

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"use strict";
33
import {RuleHelper} from "textlint-rule-helper";
44
import {getTokenizer} from "kuromojin";
5-
import splitSentences, {Syntax as SentenceSyntax} from "sentence-splitter";
5+
import {split as splitSentences, Syntax as SentenceSyntax} from "sentence-splitter";
66
import StringSource from "textlint-util-to-string";
77

88
/*
@@ -27,6 +27,11 @@ export default function (context, options = {}) {
2727
let sentences = splitSentences(text, {
2828
charRegExp: /[\?\!]/
2929
}).filter(isSentenceNode);
30+
// if not have a sentence, early return
31+
// It is for avoiding error of emptyArray.reduce().
32+
if(sentences.length === 0) {
33+
return;
34+
}
3035
return getTokenizer().then(tokenizer => {
3136
const selectConjenction = (sentence) => {
3237
let tokens = tokenizer.tokenizeForSentence(sentence.raw);
@@ -43,16 +48,13 @@ export default function (context, options = {}) {
4348
}
4449
if (current_tokens.length > 0) {
4550
if (token && current_tokens[0].surface_form === token.surface_form) {
46-
let originalPosition = source.originalPositionFor({
51+
let originalIndex = source.originalIndexFromPosition({
4752
line: sentence.loc.start.line,
4853
column: sentence.loc.start.column + (current_tokens[0].word_position - 1)
4954
});
5055
// padding position
5156
var padding = {
52-
line: originalPosition.line - 1,
53-
// matchLastToken.word_position start with 1
54-
// this is padding column start with 0 (== -1)
55-
column: originalPosition.column
57+
index: originalIndex
5658
};
5759
report(node, new RuleError(`同じ接続詞が連続して使われています。`, padding));
5860
}

test/mocha.opts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
--compilers js:espower-babel/guess
1+
--compilers js:babel-register

test/no-doubled-conjunction.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
1-
import assert from "power-assert";
21
import rule from "../src/no-doubled-conjunction";
32
import TextLintTester from "textlint-tester";
43
var tester = new TextLintTester();
54
tester.run("no-doubled-conjunction", rule, {
65
valid: [
76
"朝起きた。そして、夜に寝た。",
8-
"そして朝起きた。けれど昼は仕事をした。そして夜に寝た。"
7+
"そして朝起きた。けれど昼は仕事をした。そして夜に寝た。",
8+
"``", // empty Paragraph,
9+
"![](path/to/image.png)"// empty image label
910
],
1011
invalid: [
1112
{

0 commit comments

Comments
 (0)