Skip to content

Commit

Permalink
fix: update libraries and improve analysis
Browse files Browse the repository at this point in the history
  • Loading branch information
azu committed Apr 25, 2021
1 parent 7f0cb2c commit cea56af
Show file tree
Hide file tree
Showing 9 changed files with 4,269 additions and 46 deletions.
5 changes: 0 additions & 5 deletions .babelrc

This file was deleted.

20 changes: 20 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
name: test
on: [push, pull_request]
jobs:
test:
name: "Test on Node.js ${{ matrix.node-version }}"
runs-on: ubuntu-latest
strategy:
matrix:
node-version: [12, 14]
steps:
- name: checkout
uses: actions/checkout@v2
- name: setup Node.js ${{ matrix.node-version }}
uses: actions/setup-node@v2
with:
node-version: ${{ matrix.node-version }}
- name: Install
run: yarn install
- name: Test
run: yarn test
3 changes: 0 additions & 3 deletions .travis.yml

This file was deleted.

2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# textlint-rule-max-comma [![Build Status](https://travis-ci.org/azu/textlint-rule-max-comma.svg?branch=master)](https://travis-ci.org/azu/textlint-rule-max-comma)
# textlint-rule-max-comma [![Actions Status: test](https://github.com/azu/textlint-rule-max-comma/workflows/test/badge.svg)](https://github.com/azu/textlint-rule-max-comma/actions?query=workflow%3A"test")

[textlint](http://textlint.github.io/ "textlint") rule is that limit maximum comma(,) count of sentence.

Expand Down
18 changes: 8 additions & 10 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@
"test": "test"
},
"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"
},
"repository": {
"type": "git",
Expand All @@ -31,14 +31,12 @@
},
"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": "^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"
}
}
34 changes: 17 additions & 17 deletions src/textlint-rule-max-comma.js
Original file line number Diff line number Diff line change
@@ -1,41 +1,41 @@
// 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
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){
[Syntax.Paragraph](node) {
const nodeWithoutCode = filter(node, (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);
const sentences = splitAST(nodeWithoutCode).children.filter(node => node.type === SentenceSyntax.Sentence);
sentences.forEach(sentence => {
const sentenceValue = sentence.value;
const source = new StringSource(sentence);
const sentenceValue = source.toString();
console.log(sentenceValue);
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)
}));
}
});
}
}
};
}
1 change: 0 additions & 1 deletion test/mocha.opts

This file was deleted.

18 changes: 9 additions & 9 deletions test/textlint-rule-max-comma-test.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
const TextLintTester = require("textlint-tester");
import TextLintTester from "textlint-tester";
import rule from "../src/textlint-rule-max-comma";
import fs from "fs";

const tester = new TextLintTester();
const rule = require("../src/textlint-rule-max-comma");
const fs = require("fs");
// ruleName, rule, { valid, invalid }
tester.run("no-todo", rule, {
valid: [
Expand All @@ -11,10 +12,10 @@ tester.run("no-todo", rule, {
"0, 1, 2, 3",
"This `sum(0,1,2,3,4,5,6,7,8,9,10)` is ok",
{
text: "0, 1, 2, 3, 4",
options: {
max: 5
},
text: "0, 1, 2, 3, 4"
}
},
// example
fs.readFileSync(__dirname + "/fixtures/README.md", "utf-8")
Expand All @@ -26,7 +27,7 @@ tester.run("no-todo", rule, {
{
message: "This sentence exceeds the maximum count of comma. Maximum is 4.",
line: 1,
column: 1
column: 19
}
]
},
Expand All @@ -37,10 +38,9 @@ Second, this, is, not, ok, sentence.`,
errors: [
{
message: "This sentence exceeds the maximum count of comma. Maximum is 4.",
line: 2,
column: 1
index: 25
}
]
}
]
});
});
Loading

0 comments on commit cea56af

Please sign in to comment.