Skip to content

Commit ac59cf7

Browse files
committed
feat(rule): support "max" options
1 parent 8cc8e0f commit ac59cf7

File tree

2 files changed

+55
-18
lines changed

2 files changed

+55
-18
lines changed

src/max-ten.js

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,17 @@
11
// LICENSE : MIT
22
"use strict";
33
import {RuleHelper} from "textlint-rule-helper"
4+
5+
const defaultOptions = {max: 3};
46
function countTen(text) {
57
return text.split("、").length - 1;
68
}
79
/**
810
* @param {RuleContext} context
11+
* @param {object} options
912
*/
10-
export default function (context) {
11-
var maxLen = 3;
13+
export default function (context, options = defaultOptions) {
14+
var maxLen = options.max;
1215
const punctuation = /[.]/;
1316
let helper = new RuleHelper(context);
1417
let {Syntax, RuleError, report, getSource} = context;

test/max-ten-test.js

Lines changed: 50 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2,26 +2,60 @@ import {textlint} from "textlint"
22
import rule from "../src/max-ten"
33
import path from "path"
44
import assert from "power-assert"
5+
function textIncludeTen(count) {
6+
return (new Array(count)).join("テスト、") + "です";
7+
}
58
describe("max-ten", function () {
6-
beforeEach(function () {
7-
textlint.setupRules({
8-
"max-ten": rule
9-
});
10-
});
119
afterEach(function () {
1210
textlint.resetRules();
1311
});
14-
it("should report error", function () {
15-
var filePath = path.join(__dirname, "/fixtures/error.md");
16-
var result = textlint.lintFile(filePath);
17-
assert(result.filePath === filePath);
18-
assert(result.messages.length > 0);
19-
assert.equal(result.messages[0].ruleId, "max-ten");
12+
context("when use default option", function () {
13+
beforeEach(function () {
14+
textlint.setupRules({
15+
"max-ten": rule
16+
});
17+
});
18+
it("should report error", function () {
19+
var filePath = path.join(__dirname, "/fixtures/error.md");
20+
var result = textlint.lintFile(filePath);
21+
assert(result.filePath === filePath);
22+
assert(result.messages.length > 0);
23+
assert.equal(result.messages[0].ruleId, "max-ten");
24+
});
25+
it("should not report error", function () {
26+
var filePath = path.join(__dirname, "/fixtures/pass.md");
27+
var result = textlint.lintFile(filePath);
28+
assert(result.filePath === filePath);
29+
assert(result.messages.length === 0);
30+
});
2031
});
21-
it("should not report error", function () {
22-
var filePath = path.join(__dirname, "/fixtures/pass.md");
23-
var result = textlint.lintFile(filePath);
24-
assert(result.filePath === filePath);
25-
assert(result.messages.length === 0);
32+
context("Change options#maxLen", function () {
33+
context("when maxLen is 5, count of `、` < 5", function () {
34+
it("should not report error", ()=> {
35+
textlint.setupRules({
36+
"max-ten": rule
37+
}, {
38+
"max-ten": {
39+
"max": 5
40+
}
41+
});
42+
var result = textlint.lintMarkdown("a、b、c、d、です。");
43+
console.log(result.messages);
44+
assert(result.messages.length === 0);
45+
});
46+
});
47+
context("when maxLen is 5, count of `、` >= 5", function () {
48+
it("should report error", ()=> {
49+
textlint.setupRules({
50+
"max-ten": rule
51+
}, {
52+
"max-ten": {
53+
"max": 5
54+
}
55+
});
56+
var result = textlint.lintMarkdown("a、b、c、d、e、です。");
57+
assert(result.messages.length > 0);
58+
});
59+
});
2660
});
2761
});

0 commit comments

Comments
 (0)