Skip to content

Commit 16ab0bb

Browse files
committed
Fix "TypeError: Reduce of empty array with no initial value"
When passed Paragraph node that is empty value, throw "TypeError: Reduce of empty array with no initial value".
1 parent 9240152 commit 16ab0bb

File tree

2 files changed

+10
-8
lines changed

2 files changed

+10
-8
lines changed

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/no-doubled-conjunction.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
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
99
],
1010
invalid: [
1111
{

0 commit comments

Comments
 (0)