Skip to content

Commit

Permalink
fix(rule): fix index of match word
Browse files Browse the repository at this point in the history
  • Loading branch information
azu committed Nov 27, 2015
1 parent 6abeaac commit 110b303
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 48 deletions.
7 changes: 5 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,14 @@
"espower-babel": "^3.3.0",
"mocha": "^2.3.0",
"power-assert": "^1.0.0",
"textlint": "^3.2.0",
"textlint-tester": "^0.2.0"
"textlint": "^5.0.3",
"textlint-tester": "^0.4.1"
},
"dependencies": {
"kuromojin": "^1.0.2",
"object-assign": "^4.0.1",
"sentence-splitter": "^1.2.0",
"structured-source": "^3.0.2",
"textlint-rule-helper": "^1.1.3"
}
}
81 changes: 37 additions & 44 deletions src/max-ten.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,69 +2,62 @@
"use strict";
import {RuleHelper} from "textlint-rule-helper"
import ObjectAssign from "object-assign"
import {getTokenizer} from "kuromojin";
import splitSentences from "sentence-splitter";
import Source from "structured-source";
const defaultOptions = {max: 3};
function countTen(text) {
return text.split("、").length - 1;
}
/**
* @param {RuleContext} context
* @param {object} options
*/
export default function (context, options = {}) {
options = ObjectAssign({}, defaultOptions, options);
const maxLen = options.max;
const punctuation = /[]/;
let helper = new RuleHelper(context);
let {Syntax, RuleError, report, getSource} = context;
let currentParagraphTexts = [];
return {
[Syntax.Paragraph](){
currentParagraphTexts = []
},
[Syntax.Str](node){
// ignore text from external factor
if (helper.isChildNode(node, [Syntax.Link, Syntax.Image, Syntax.BlockQuote])) {
[Syntax.Paragraph](node){
if (helper.isChildNode(node, [Syntax.BlockQuote])) {
return;
}
currentParagraphTexts.push(node);
},
[Syntax.Paragraph + ":exit"](){
let currentTenCount = 0;
let sentences = splitSentences(getSource(node), {
charRegExp: /[\?\!]/,
newLineCharacters: "\n\n"
});
/*
<p>
<str><code><img><str>
<str>
</p>
*/
currentParagraphTexts.forEach(strNode => {
let paddingLine = 0;
let paddingColumn = 0;
let text = getSource(strNode);
let characters = text.split("");
characters.forEach(char => {
if (char === "、") {
currentTenCount++;
}
if (char === "。") {
// reset
currentTenCount = 0;
}
// report
if (currentTenCount >= maxLen) {
var ruleError = new context.RuleError(`一つの文で"、"を${maxLen}つ以上使用しています`, {
line: paddingLine,
column: paddingColumn
});
report(strNode, ruleError);
currentTenCount = 0;
}
// calc padding{line,column}
if (char === "\n") {
paddingLine++;
paddingColumn = 0;
} else {
paddingColumn++;
}
return getTokenizer().then(tokenizer => {
sentences.forEach(sentence => {
let text = sentence.value;
let source = new Source(text);
let currentTenCount = 0;
let tokens = tokenizer.tokenizeForSentence(text);
let lastToken = null;
tokens.forEach(token => {
let surface = token.surface_form;
if (surface === "、") {
currentTenCount++;
lastToken = token;
}
if (surface === "。") {
// reset
currentTenCount = 0;
}
// report
if (currentTenCount >= maxLen) {
let position = source.indexToPosition(lastToken.word_position - 1);
let ruleError = new context.RuleError(`一つの文で"、"を${maxLen}つ以上使用しています`, {
line: position.line-1,
column:position.column
});
report(node, ruleError);
currentTenCount = 0;
}
});
});
});
}
Expand Down
4 changes: 2 additions & 2 deletions test/max-ten-test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import rule from "../src/max-ten"
function textIncludeTen(count) {
return (new Array(count + 1)).join("テスト、") + "です";
return (new Array(count + 1)).join("テスト文章において、") + "です";
}
var TextLintTester = require("textlint-tester");
var tester = new TextLintTester();
Expand All @@ -24,7 +24,7 @@ tester.run("max-ten", rule, {
],
invalid: [
{
text: `a、b、 c
text: `これは、これは、これは
、d`
,
errors: [
Expand Down

0 comments on commit 110b303

Please sign in to comment.