Skip to content

Commit

Permalink
fix: update textlint and use locator
Browse files Browse the repository at this point in the history
BREAKING CHANGE: Require:textlint [v12.2.0](https://github.com/textlint/textlint/releases/tag/v12.2.0)+

- Update textlint-rule-helper
- Use locator instead of index
  • Loading branch information
azu committed Jan 28, 2023
1 parent 3b4d3ba commit 094a7fb
Show file tree
Hide file tree
Showing 5 changed files with 2,184 additions and 2,879 deletions.
1 change: 0 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@ textlint --rule no-nfd README.md

`textlint --fix` での[自動修正](https://github.com/textlint/textlint/blob/master/docs/rule-fixer.md)に対応しています。


## Example

゜or `\u309a`
Expand Down
13 changes: 7 additions & 6 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,16 +31,17 @@
"textlint"
],
"devDependencies": {
"@textlint/types": "^1.2.2",
"@types/node": "^12.11.1",
"@textlint/types": "^13.0.2",
"@types/node": "^18.11.18",
"@types/unorm": "^1.3.28",
"power-assert": "^1.3.1",
"textlint-scripts": "^3.0.0",
"ts-node": "^8.4.1",
"typescript": "^3.6.4"
"textlint-scripts": "^13.0.2",
"ts-node": "^10.9.1",
"typescript": "^4.9.4"
},
"dependencies": {
"match-index": "^1.0.3",
"textlint-rule-helper": "^2.1.1",
"textlint-rule-helper": "^2.3.0",
"unorm": "^1.4.1"
}
}
18 changes: 8 additions & 10 deletions src/textlint-rule-no-nfd.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,20 @@
// LICENSE : MIT
"use strict";
import {matchCaptureGroupAll} from "match-index"
import {RuleHelper} from "textlint-rule-helper";
import {TextlintRuleReporter} from "@textlint/types";

const unorm = require("unorm");
import { matchCaptureGroupAll } from "match-index"
import { RuleHelper } from "textlint-rule-helper";
import { TextlintRuleReporter } from "@textlint/types";
import unorm from "unorm";

const reporter: TextlintRuleReporter = function (context) {
const {Syntax, RuleError, report, fixer, getSource} = context;
const { Syntax, RuleError, report, fixer, getSource, locator } = context;
const helper = new RuleHelper(context);
return {
[Syntax.Str](node) {
if (helper.isChildNode(node, [Syntax.Link, Syntax.Image, Syntax.BlockQuote, Syntax.Emphasis])) {
return;
}
const text = getSource(node);
matchCaptureGroupAll(text, /([\u309b\u309c\u309a\u3099])/g).forEach(({index}) => {
matchCaptureGroupAll(text, /([\u309b\u309c\u309a\u3099])/g).forEach(({ index }) => {
if (index === 0) {
return;
}
Expand All @@ -24,16 +23,15 @@ const reporter: TextlintRuleReporter = function (context) {
const nfdlized = dakutenChars.replace("\u309B", "\u3099").replace("\u309C", "\u309A");
const expectedText = unorm.nfc(nfdlized);
const ruleError = new RuleError(`Disallow to use NFD(well-known as UTF8-MAC 濁点): "${dakutenChars}" => "${expectedText}"`, {
index,
padding: locator.at(index),
fix: fixer.replaceTextRange([index - 1, index + 1], expectedText)
});
report(node, ruleError);
});
}
}
};

module.exports = {
export default {
linter: reporter,
fixer: reporter
};
6 changes: 3 additions & 3 deletions test/textlint-rule-no-nfd-test.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
// LICENSE : MIT
"use strict";
const TextLintTester = require("textlint-tester");
const tester = new TextLintTester();
import TextLintTester from "textlint-tester";
// rule
const rule = require("../src/textlint-rule-no-nfd");
import rule from "../src/textlint-rule-no-nfd";
const tester = new TextLintTester();
// ruleName, rule, { valid, invalid }
tester.run("no-nfd", rule, {
valid: [
Expand Down
Loading

0 comments on commit 094a7fb

Please sign in to comment.