Skip to content

Commit

Permalink
feat(rule): implement rule
Browse files Browse the repository at this point in the history
  • Loading branch information
azu committed May 1, 2016
1 parent a47dfcb commit d26b79f
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 10 deletions.
5 changes: 5 additions & 0 deletions .babelrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"presets": [
"es2015"
]
}
13 changes: 12 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,5 +22,16 @@
"bugs": {
"url": "https://github.com/azu/textlint-rule-max-comma/issues"
},
"homepage": "https://github.com/azu/textlint-rule-max-comma#readme"
"homepage": "https://github.com/azu/textlint-rule-max-comma#readme",
"devDependencies": {
"babel-cli": "^6.7.7",
"babel-preset-es2015": "^6.6.0",
"babel-register": "^6.7.2",
"mocha": "^2.4.5",
"textlint-tester": "^1.2.0"
},
"dependencies": {
"sentence-splitter": "^2.0.0",
"unist-util-filter": "^0.2.1"
}
}
27 changes: 21 additions & 6 deletions src/textlint-rule-max-comma.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,29 @@
// LICENSE : MIT
"use strict";
const splitSentences = require("sentence-splitter").split;
const Syntax = require("sentence-splitter").Syntax;
module.exports = function (context) {
import {split, Syntax as SentenceSyntax} from "sentence-splitter";
const filter = require("unist-util-filter");
function countOfComma(text) {
return text.split(",").length - 1;
}
const defaultOptions = {
// default: max comma count is 4
max: 4
};
module.exports = function (context, options = defaultOptions) {
const maxComma = options.max || defaultOptions.max;
const {Syntax, RuleError, report, getSource} = context;
return {
[Syntax.Paragraph](node){
const text = getSource(node);
const sentences = splitSentences(text).filter(node => node.type === Syntax.Sentence)
const nodeWithoutCode = filter(node, (node) => node.type !== Syntax.Code);
const text = getSource(nodeWithoutCode);
const sentences = split(text).filter(node => node.type === SentenceSyntax.Sentence);
sentences.forEach(sentence => {
const sentenceValue = sentence.value;
const count = countOfComma(sentenceValue);
if(count > maxComma) {
report(node, new RuleError(`This sentence exceeds the maximum count of comma. Maximum is ${maxComma}.`, sentence));
}
});
}

}
};
1 change: 1 addition & 0 deletions test/mocha.opts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
--compilers js:babel-register
9 changes: 6 additions & 3 deletions test/textlint-rule-max-comma-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,14 @@ tester.run("no-todo", rule, {
],
invalid: [
{
text: "t0, t1, t2, t3, t4",
text: "t0, t1, t2, t3, t4, t5",
errors: [
{
message: " exceeds the maximum line length of"
message: "This sentence exceeds the maximum count of comma. Maximum is 4.",
line: 1,
column: 1
}
]
}]
}
]
});

0 comments on commit d26b79f

Please sign in to comment.