textlint-rule-max-comma
Advanced tools
Comparing version 1.0.4 to 2.0.0
// LICENSE : MIT | ||
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { | ||
value: true | ||
}); | ||
exports.default = _default; | ||
var _unistUtilFilter = _interopRequireDefault(require("unist-util-filter")); | ||
var _sentenceSplitter = require("sentence-splitter"); | ||
function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; } | ||
var _textlintUtilToString = require("textlint-util-to-string"); | ||
var filter = require("unist-util-filter"); | ||
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } | ||
function countOfComma(text) { | ||
return text.split(",").length - 1; | ||
return text.split(",").length - 1; | ||
} | ||
var defaultOptions = { | ||
// default: max comma count is 4 | ||
max: 4 | ||
// default: allowed command count | ||
max: 4 | ||
}; | ||
module.exports = function (context) { | ||
var options = arguments.length <= 1 || arguments[1] === undefined ? defaultOptions : arguments[1]; | ||
var maxComma = options.max || defaultOptions.max; | ||
var Syntax = context.Syntax; | ||
var RuleError = context.RuleError; | ||
var report = context.report; | ||
var getSource = context.getSource; | ||
function _default(context) { | ||
var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : defaultOptions; | ||
var maxComma = options.max || defaultOptions.max; | ||
var { | ||
Syntax, | ||
RuleError, | ||
report, | ||
getSource | ||
} = context; | ||
return { | ||
[Syntax.Paragraph](node) { | ||
var paragraphSentence = (0, _sentenceSplitter.splitAST)(node); // Remove Code node for avoiding false-positive in `CODE` | ||
return _defineProperty({}, Syntax.Paragraph, function (node) { | ||
var nodeWithoutCode = filter(node, function (node) { | ||
return node.type !== Syntax.Code; | ||
}); | ||
if (!nodeWithoutCode) { | ||
return; | ||
var paragraphSentenceWithoutNode = (0, _unistUtilFilter.default)(paragraphSentence, node => { | ||
return node.type !== Syntax.Code; | ||
}); // This `sum(0,1,2,3,4,5,6,7,8,9,10)` is ok | ||
// → This is ok | ||
var sentencesWithoutCode = paragraphSentenceWithoutNode.children.filter(node => node.type === _sentenceSplitter.Syntax.Sentence); | ||
sentencesWithoutCode.forEach(sentence => { | ||
var source = new _textlintUtilToString.StringSource(sentence); | ||
var sentenceValue = source.toString(); | ||
var count = countOfComma(sentenceValue); | ||
if (count > maxComma) { | ||
var lastCommandIndex = sentenceValue.lastIndexOf(","); | ||
report(node, new RuleError("This sentence exceeds the maximum count of comma. Maximum is ".concat(maxComma, "."), { | ||
index: source.originalIndexFromIndex(lastCommandIndex) | ||
})); | ||
} | ||
var texts = Array.isArray(nodeWithoutCode.children) ? nodeWithoutCode.children.map(function (child) { | ||
return getSource(child); | ||
}) : []; | ||
var text = texts.join(""); | ||
var sentences = (0, _sentenceSplitter.split)(text).filter(function (node) { | ||
return node.type === _sentenceSplitter.Syntax.Sentence; | ||
}); | ||
sentences.forEach(function (sentence) { | ||
var sentenceValue = sentence.value; | ||
var count = countOfComma(sentenceValue); | ||
if (count > maxComma) { | ||
var paddingStart = { | ||
line: sentence.loc.start.line - 1, | ||
column: sentence.loc.start.column | ||
}; | ||
report(node, new RuleError("This sentence exceeds the maximum count of comma. Maximum is " + maxComma + ".", paddingStart)); | ||
} | ||
}); | ||
}); | ||
}; | ||
}); | ||
} | ||
}; | ||
} | ||
//# sourceMappingURL=textlint-rule-max-comma.js.map |
{ | ||
"name": "textlint-rule-max-comma", | ||
"version": "1.0.4", | ||
"version": "2.0.0", | ||
"description": "textlint rule that limit maxinum comma(,) count of sentence.", | ||
@@ -14,6 +14,6 @@ "main": "lib/textlint-rule-max-comma.js", | ||
"scripts": { | ||
"build": "NODE_ENV=production babel src --out-dir lib --source-maps", | ||
"watch": "babel src --out-dir lib --watch --source-maps", | ||
"build": "textlint-scripts build", | ||
"watch": "textlint-scripts build --watch", | ||
"prepublish": "npm run --if-present build", | ||
"test": "mocha" | ||
"test": "textlint-scripts test" | ||
}, | ||
@@ -35,12 +35,10 @@ "repository": { | ||
"devDependencies": { | ||
"babel-cli": "^6.7.7", | ||
"babel-preset-es2015": "^6.6.0", | ||
"babel-register": "^6.7.2", | ||
"mocha": "^3.0.2", | ||
"textlint-tester": "^2.0.0" | ||
"textlint-scripts": "^3.0.0", | ||
"textlint-tester": "^5.3.4" | ||
}, | ||
"dependencies": { | ||
"sentence-splitter": "^2.0.0", | ||
"unist-util-filter": "^0.2.1" | ||
"sentence-splitter": "^3.2.1", | ||
"textlint-util-to-string": "^3.1.1", | ||
"unist-util-filter": "^2.0.3" | ||
} | ||
} |
@@ -1,2 +0,2 @@ | ||
# textlint-rule-max-comma [](https://travis-ci.org/azu/textlint-rule-max-comma) | ||
# textlint-rule-max-comma [](https://github.com/azu/textlint-rule-max-comma/actions?query=workflow%3A"test") | ||
@@ -15,5 +15,7 @@ [textlint](http://textlint.github.io/ "textlint") rule is that limit maximum comma(,) count of sentence. | ||
## Configure | ||
## Options | ||
Configure the maximum number of "," allowed in a sentence. The default is `4` | ||
- `max`: maximum number of "," | ||
- Default: `4` | ||
- It means that report an error if the sentence include 5 or more `,` | ||
@@ -26,3 +28,3 @@ Configure `"max"` value of the `.textlintrc` file. | ||
"max-comma": { | ||
"max" : 3 | ||
"max" : 4 | ||
} | ||
@@ -39,3 +41,4 @@ } | ||
- [azu/textlint-rule-max-ten: textlint rule that limit maxinum ten(、) count of sentence.](https://github.com/azu/textlint-rule-max-ten "azu/textlint-rule-max-ten: textlint rule that limit maxinum ten(、) count of sentence.") | ||
- [textlint-rule-max-ten](https://github.com/textlint-ja/textlint-rule-max-ten) | ||
- Japanese comma(`、`) edition | ||
@@ -52,2 +55,2 @@ ## Contributing | ||
MIT | ||
MIT |
// LICENSE : MIT | ||
"use strict"; | ||
import {split, Syntax as SentenceSyntax} from "sentence-splitter"; | ||
const filter = require("unist-util-filter"); | ||
import filter from "unist-util-filter"; | ||
import { splitAST, Syntax as SentenceSyntax } from "sentence-splitter"; | ||
import { StringSource } from "textlint-util-to-string" | ||
function countOfComma(text) { | ||
return text.split(",").length - 1; | ||
} | ||
const defaultOptions = { | ||
// default: max comma count is 4 | ||
// default: allowed command count | ||
max: 4 | ||
}; | ||
module.exports = function(context, options = defaultOptions) { | ||
export default function (context, options = defaultOptions) { | ||
const maxComma = options.max || defaultOptions.max; | ||
const {Syntax, RuleError, report, getSource} = context; | ||
const { Syntax, RuleError, report, getSource } = context; | ||
return { | ||
[Syntax.Paragraph](node){ | ||
const nodeWithoutCode = filter(node, (node) => { | ||
[Syntax.Paragraph](node) { | ||
const paragraphSentence = splitAST(node) | ||
// Remove Code node for avoiding false-positive in `CODE` | ||
const paragraphSentenceWithoutNode = filter(paragraphSentence, (node) => { | ||
return node.type !== Syntax.Code; | ||
}); | ||
if (!nodeWithoutCode) { | ||
return; | ||
} | ||
const texts = Array.isArray(nodeWithoutCode.children) ? nodeWithoutCode.children.map(child => { | ||
return getSource(child); | ||
}) : []; | ||
const text = texts.join(""); | ||
const sentences = split(text).filter(node => node.type === SentenceSyntax.Sentence); | ||
sentences.forEach(sentence => { | ||
const sentenceValue = sentence.value; | ||
// This `sum(0,1,2,3,4,5,6,7,8,9,10)` is ok | ||
// → This is ok | ||
const sentencesWithoutCode = paragraphSentenceWithoutNode | ||
.children | ||
.filter(node => node.type === SentenceSyntax.Sentence); | ||
sentencesWithoutCode.forEach(sentence => { | ||
const source = new StringSource(sentence); | ||
const sentenceValue = source.toString(); | ||
const count = countOfComma(sentenceValue); | ||
if (count > maxComma) { | ||
const paddingStart = { | ||
line: sentence.loc.start.line - 1, | ||
column: sentence.loc.start.column | ||
}; | ||
report(node, new RuleError(`This sentence exceeds the maximum count of comma. Maximum is ${maxComma}.`, paddingStart)); | ||
const lastCommandIndex = sentenceValue.lastIndexOf(","); | ||
report(node, new RuleError(`This sentence exceeds the maximum count of comma. Maximum is ${maxComma}.`, { | ||
index: source.originalIndexFromIndex(lastCommandIndex) | ||
})); | ||
} | ||
@@ -41,2 +43,2 @@ }); | ||
} | ||
}; | ||
} |
Sorry, the diff of this file is not supported yet
10637
2
89
53
3
+ Added@textlint/ast-node-types@13.4.14.4.3(transitive)
+ Added@types/unist@2.0.11(transitive)
+ Addedbail@1.0.5(transitive)
+ Addedboundary@2.0.0(transitive)
+ Addedccount@1.1.0(transitive)
+ Addedcomma-separated-tokens@1.0.8(transitive)
+ Addedconcat-stream@2.0.0(transitive)
+ Addedextend@3.0.2(transitive)
+ Addedhast-util-from-parse5@5.0.3(transitive)
+ Addedhast-util-parse-selector@2.2.5(transitive)
+ Addedhastscript@5.1.2(transitive)
+ Addedis-buffer@2.0.5(transitive)
+ Addedis-plain-obj@2.1.0(transitive)
+ Addedobject_values@0.1.2(transitive)
+ Addedparse5@5.1.1(transitive)
+ Addedproperty-information@5.6.0(transitive)
+ Addedreadable-stream@3.6.2(transitive)
+ Addedrehype-parse@6.0.2(transitive)
+ Addedsafe-buffer@5.2.1(transitive)
+ Addedsentence-splitter@3.2.3(transitive)
+ Addedspace-separated-tokens@1.1.5(transitive)
+ Addedstring_decoder@1.3.0(transitive)
+ Addedstructured-source@4.0.0(transitive)
+ Addedtextlint-util-to-string@3.3.4(transitive)
+ Addedtrough@1.0.5(transitive)
+ Addedunified@8.4.2(transitive)
+ Addedunist-util-filter@2.0.3(transitive)
+ Addedunist-util-is@4.1.0(transitive)
+ Addedunist-util-stringify-position@2.0.3(transitive)
+ Addedvfile@4.2.1(transitive)
+ Addedvfile-message@2.0.4(transitive)
+ Addedweb-namespaces@1.1.4(transitive)
+ Addedxtend@4.0.2(transitive)
- Removedconcat-stream@1.6.2(transitive)
- Removedcore-util-is@1.0.3(transitive)
- Removedflatmap@0.0.3(transitive)
- Removedisarray@1.0.0(transitive)
- Removedprocess-nextick-args@2.0.1(transitive)
- Removedreadable-stream@2.3.8(transitive)
- Removedsafe-buffer@5.1.2(transitive)
- Removedsentence-splitter@2.3.2(transitive)
- Removedstring_decoder@1.1.1(transitive)
- Removedunist-util-filter@0.2.1(transitive)
- Removedunist-util-is@1.0.0(transitive)
Updatedsentence-splitter@^3.2.1
Updatedunist-util-filter@^2.0.3