Skip to content

Commit

Permalink
refactor: update structured-source and use @textlint/kernel (#20)
Browse files Browse the repository at this point in the history
* refactor: update structured-source and use @textlint/kernel

* CI: update node versions

* CI: add .github/release.yml

* test: all convert
  • Loading branch information
azu authored Jan 8, 2023
1 parent c695466 commit 2212f7f
Show file tree
Hide file tree
Showing 10 changed files with 653 additions and 1,146 deletions.
29 changes: 29 additions & 0 deletions .github/release.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
changelog:
exclude:
labels:
- 'Type: Meta'
- 'Type: Question'

categories:
- title: Security Fixes
labels: ['Type: Security']
- title: Breaking Changes
labels: ['Type: Breaking Change']
- title: Features
labels: ['Type: Feature']
- title: Bug Fixes
labels: ['Type: Bug']
- title: Documentation
labels: ['Type: Documentation']
- title: Refactoring
labels: ['Type: Refactoring']
- title: Testing
labels: ['Type: Testing']
- title: Maintenance
labels: ['Type: Maintenance']
- title: CI
labels: ['Type: CI']
- title: Dependency Updates
labels: ['Type: Dependencies', "dependencies"]
- title: Other Changes
labels: ['*']
2 changes: 1 addition & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ jobs:
runs-on: ubuntu-latest
strategy:
matrix:
node-version: [12, 14]
node-version: [ 16, 18 ]
steps:
- name: checkout
uses: actions/checkout@v2
Expand Down
29 changes: 11 additions & 18 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,30 +25,23 @@
"watch": "tsc -p . --watch"
},
"dependencies": {
"structured-source": "^3.0.2",
"unist-util-visit": "^2.0.3",
"@textlint/ast-node-types": "^12.1.0"
"@textlint/ast-node-types": "^12.3.0",
"structured-source": "^4.0.0",
"unist-util-visit": "^2.0.3"
},
"devDependencies": {
"@textlint/types": "^12.1.0",
"@textlint/ast-node-types": "^12.1.0",
"@types/mocha": "^9.0.0",
"@types/node": "^17.0.5",
"@textlint/kernel": "^12.5.0",
"@textlint/textlint-plugin-markdown": "^12.5.0",
"@textlint/types": "^12.5.0",
"@types/mocha": "^10.0.1",
"@types/node": "^18.11.18",
"@types/structured-source": "^3.0.0",
"markdown-to-ast": "^6.0.3",
"mocha": "^9.1.3",
"textlint": "^12.1.0",
"ts-node": "^10.4.0",
"mocha": "^10.2.0",
"ts-node": "^10.9.1",
"ts-node-test-register": "^10.0.0",
"txt-ast-traverse": "^2.0.4",
"typescript": "^4.5.4"
},
"peerDependencies": {
"@textlint/types": "^12.1.0"
},
"peerDependenciesMeta": {
"@textlint/types": {
"optional": true
}
"typescript": "^4.9.4"
}
}
10 changes: 5 additions & 5 deletions src/SourceLocation.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
// LICENSE : MIT
"use strict";
import SourceCode from "structured-source";
import type { TextlintRuleErrorPadding } from "@textlint/types";
import { StructuredSource } from "structured-source";
import type { TextlintRuleErrorDetails } from "@textlint/types";
import { AnyTxtNode } from "@textlint/ast-node-types";

export class SourceLocation {
private source: SourceCode;
private source: StructuredSource;

constructor(text: string) {
this.source = new SourceCode(text);
this.source = new StructuredSource(text);
}

toAbsoluteLocation(node: AnyTxtNode, padding: TextlintRuleErrorPadding) {
toAbsoluteLocation(node: AnyTxtNode, padding: TextlintRuleErrorDetails) {
const nodeRange = node.range;
const line = node.loc.start.line;
const column = node.loc.start.column;
Expand Down
2 changes: 1 addition & 1 deletion src/wrap-report-handler.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// MIT © 2018 azu
"use strict";
import { AnyTxtNode, TxtNodeType } from "@textlint/ast-node-types";
import { TextlintRuleContext, TextlintRuleError, TextlintRuleReportHandler } from "@textlint/types";
import type { TextlintRuleContext, TextlintRuleError, TextlintRuleReportHandler } from "@textlint/types";
import RuleHelper from "./textlint-rule-helper";
import IgnoreNodeManager from "./IgnoreNodeManager";
import { SourceLocation } from "./SourceLocation";
Expand Down
68 changes: 39 additions & 29 deletions test/IgnoreNodeManager-test.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
// LICENSE : MIT
import assert from 'assert'
import { textlint } from "textlint"
import { IgnoreNodeManager } from "../src/index";
import { TxtNodeType, TxtParentNode } from "@textlint/ast-node-types";
import { TextlintKernel, TextlintRuleModule } from "@textlint/kernel"
import { builtInPlugins } from "./textlint-helper";

describe("IgnoreNodeManager", function () {
afterEach(function () {
textlint.resetRules();
});
describe("#isIgnoredRange()", () => {
it("should ignore multiple nodes", () => {
const ignoreManager = new IgnoreNodeManager();
Expand All @@ -22,7 +20,7 @@ describe("IgnoreNodeManager", function () {
assert.ok(ignoreManager.isIgnoredRange([2, 5]), "[2, 5]");
assert.ok(ignoreManager.isIgnoredRange([4, 5]), "[4, 5]");
assert.ok(ignoreManager.isIgnoredRange([5, 6]) === false, "[5, 6]");
assert.ok(ignoreManager.isIgnoredRange([6, 10]) === false , "[5, 6]");
assert.ok(ignoreManager.isIgnoredRange([6, 10]) === false, "[5, 6]");
});
});
describe("#ignoreChildrenByTypes()", () => {
Expand All @@ -34,18 +32,24 @@ describe("IgnoreNodeManager", function () {
This is **ignored**.
`;
const ignoreManager = new IgnoreNodeManager();
textlint.setupRules({
"rule-key": function (context: any) {
const { Syntax } = context;
return {
[Syntax.Paragraph](node: any) {
const ignoredNodeTypes: TxtNodeType[] = [context.Syntax.Code, context.Syntax.Strong];
ignoreManager.ignoreChildrenByTypes(node, ignoredNodeTypes);
}
const textlint = new TextlintKernel();
const rule: TextlintRuleModule = function (context: any) {
const { Syntax } = context;
return {
[Syntax.Paragraph](node: any) {
const ignoredNodeTypes: TxtNodeType[] = [context.Syntax.Code, context.Syntax.Strong];
ignoreManager.ignoreChildrenByTypes(node, ignoredNodeTypes);
}
}
});
return textlint.lintMarkdown(text).then(() => {
}
return textlint.lintText(text, {
ext: ".md",
rules: [{
ruleId: "test",
rule: rule
}],
plugins: builtInPlugins
}).then(() => {
assert.deepStrictEqual(ignoreManager.ignoredRanges, [
[11, 20],
[
Expand Down Expand Up @@ -96,22 +100,28 @@ This is **ignored**.
},
];
const ignoreManager = new IgnoreNodeManager();
textlint.setupRules({
"rule-key": function (context: any) {
const { Syntax, getSource } = context;
return {
[Syntax.Paragraph](node: TxtParentNode) {
ignoreManager.ignoreChildrenByTypes(node, [context.Syntax.Code]);
const text = getSource(node);
expectedList.forEach(item => {
const index = text.search(item.name);
item["actual"] = ignoreManager.isIgnoredIndex(index);
})
}
const textlint = new TextlintKernel();
const rule: TextlintRuleModule = function (context: any) {
const { Syntax, getSource } = context;
return {
[Syntax.Paragraph](node: TxtParentNode) {
ignoreManager.ignoreChildrenByTypes(node, [context.Syntax.Code]);
const text = getSource(node);
expectedList.forEach(item => {
const index = text.search(item.name);
item["actual"] = ignoreManager.isIgnoredIndex(index);
})
}
}
});
return textlint.lintMarkdown(text).then(() => {
}
return textlint.lintText(text, {
ext: ".md",
rules: [{
ruleId: "test",
rule: rule
}],
plugins: builtInPlugins
}).then(() => {
expectedList.forEach(item => {
assert.strictEqual(item.actual, item.ignored, `${item.name} should be ${item.ignored ? "ignored"
: "includes"}`);
Expand Down
6 changes: 6 additions & 0 deletions test/textlint-helper.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import markdown from "@textlint/textlint-plugin-markdown";

export const builtInPlugins = [{
pluginId: "markdown",
plugin: markdown
}];
Loading

0 comments on commit 2212f7f

Please sign in to comment.