Skip to content

Commit

Permalink
Merge pull request #11 from textlint/typescript
Browse files Browse the repository at this point in the history
refactor(TypeScript): Convert to TypeScript
  • Loading branch information
azu authored Dec 27, 2018
2 parents 575e33e + d416e6e commit 844a361
Show file tree
Hide file tree
Showing 11 changed files with 236 additions and 1,703 deletions.
5 changes: 0 additions & 5 deletions .babelrc

This file was deleted.

40 changes: 22 additions & 18 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,39 +1,43 @@
{
"name": "textlint-rule-helper",
"description": "Helper for textlint rule.",
"version": "2.0.1",
"description": "Helper for textlint rule.",
"homepage": "https://github.com/textlint/textlint-rule-helper/",
"bugs": {
"url": "https://github.com/textlint/textlint-rule-helper/issues"
},
"repository": {
"type": "git",
"url": "https://github.com/textlint/textlint-rule-helper.git"
},
"main": "lib/index.js",
"license": "MIT",
"author": "azu",
"files": [
"lib",
"src"
],
"main": "lib/index.js",
"types": "lib/index.d.ts",
"scripts": {
"build": "babel src/ --out-dir lib/ --source-maps",
"watch": "babel src/ --out-dir lib/ --watch --source-maps",
"test": "mocha test/",
"prepublish": "npm run build"
"build": "cross-env NODE_ENV=production tsc -p .",
"prepublish": "npm run --if-present build",
"test": "mocha \"test/**/*.{js,ts}\"",
"watch": "tsc -p . --watch"
},
"author": "azu",
"license": "MIT",
"bugs": {
"url": "https://github.com/textlint/textlint-rule-helper/issues"
"dependencies": {
"@textlint/ast-node-types": "^4.0.3",
"unist-util-visit": "^1.1.0"
},
"devDependencies": {
"@babel/cli": "^7.1.2",
"@babel/core": "^7.1.2",
"@babel/preset-env": "^7.1.0",
"@babel/register": "^7.0.0",
"@types/mocha": "^5.2.5",
"@types/node": "^10.12.18",
"cross-env": "^5.2.0",
"markdown-to-ast": "^6.0.3",
"mocha": "^5.2.0",
"textlint": "^11.0.0",
"txt-ast-traverse": "^2.0.4"
},
"dependencies": {
"unist-util-visit": "^1.1.0"
"ts-node": "^7.0.1",
"ts-node-test-register": "^4.0.0",
"txt-ast-traverse": "^2.0.4",
"typescript": "^3.2.2"
}
}
18 changes: 11 additions & 7 deletions src/IgnoreNodeManager.js → src/IgnoreNodeManager.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
// LICENSE : MIT
"use strict";
import { TxtNode, TxtParentNode, TxtNodeType, TextNodeRange } from "@textlint/ast-node-types"

const visit = require('unist-util-visit');
/**
* Ignore node manager that manager ignored ranges.
*
*/
export default class IgnoreNodeManager {
private _ignoredRangeList: TextNodeRange[];

constructor() {
/**
* @type {[number,number][]}
Expand All @@ -32,7 +36,7 @@ export default class IgnoreNodeManager {
* @param {number} index
* @returns {boolean}
*/
isIgnoredIndex(index) {
isIgnoredIndex(index: number) {
return this._ignoredRangeList.some(range => {
const [start, end] = range;
return start <= index && index < end;
Expand All @@ -43,7 +47,7 @@ export default class IgnoreNodeManager {
* @param {[number, number]} aRange
* @returns {boolean}
*/
isIgnoredRange(aRange) {
isIgnoredRange(aRange: TextNodeRange) {
const index = aRange[0];
return this.isIgnoredIndex(index);
}
Expand All @@ -52,7 +56,7 @@ export default class IgnoreNodeManager {
* @param {Object} node
* @returns {boolean}
*/
isIgnored(node) {
isIgnored(node: TxtNode | TxtParentNode) {
const index = node.index;
return this.isIgnoredIndex(index);
}
Expand All @@ -61,15 +65,15 @@ export default class IgnoreNodeManager {
* add node to ignore range list
* @param {TxtNode} node
*/
ignore(node) {
ignore(node: TxtNode | TxtParentNode) {
this.ignoreRange(node.range);
}

/**
* add range to ignore range list
* @param {[number, number]} range
*/
ignoreRange(range) {
ignoreRange(range: TextNodeRange) {
this._ignoredRangeList.push(range);
}

Expand All @@ -79,8 +83,8 @@ export default class IgnoreNodeManager {
* @param {TxtNode} targetNode
* @param {string[]} ignoredNodeTypes
*/
ignoreChildrenByTypes(targetNode, ignoredNodeTypes) {
visit(targetNode, visitedNode => {
ignoreChildrenByTypes(targetNode: TxtNode | TxtParentNode, ignoredNodeTypes: TxtNodeType[]) {
visit(targetNode, (visitedNode: TxtNode | TxtParentNode) => {
if (ignoredNodeTypes.indexOf(visitedNode.type) !== -1) {
this.ignore(visitedNode);
}
Expand Down
3 changes: 1 addition & 2 deletions src/index.js → src/index.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
// LICENSE : MIT
"use strict";
import RuleHelper from "./textlint-rule-helper";
import IgnoreNodeManager from "./IgnoreNodeManager"
module.exports = {
export {
IgnoreNodeManager,
RuleHelper
};
18 changes: 10 additions & 8 deletions src/textlint-rule-helper.js → src/textlint-rule-helper.ts
Original file line number Diff line number Diff line change
@@ -1,26 +1,28 @@
// LICENSE : MIT
"use strict";
import { TxtNode } from "@textlint/ast-node-types"

/**
* RuleHelper is helper class for textlint.
* @class RuleHelper
*/
export default class RuleHelper {
// @ts-ignore
private _ruleContext: any;

/**
* Initialize RuleHelper with RuleContext object.
* @param {RuleContext} ruleContext the ruleContext is context object of the rule.
*/
constructor(ruleContext) {
this.ruleContext = ruleContext;
constructor(ruleContext: any) {
this._ruleContext = ruleContext;
}

/**
* Get parents of node.
* The parent nodes are returned in order from the closest parent to the outer ones.
* {@link node} is not contained in the results.
* @param {TxtNode} node the node is start point.
* @returns {TxtNode[]}
*/
getParents(node) {
getParents(node: TxtNode) {
const result = [];
let parent = node.parent;
while (parent != null) {
Expand All @@ -36,9 +38,9 @@ export default class RuleHelper {
* @param {string[]} types are wrapped target node
* @returns {boolean}
*/
isChildNode(node, types) {
isChildNode(node: TxtNode, types: string[]) {
const parents = this.getParents(node);
const parentsTypes = parents.map(function(parent) {
const parentsTypes = parents.map(function (parent) {
return parent.type;
});
return types.some(function (type) {
Expand Down
26 changes: 14 additions & 12 deletions test/IgnoreNodeManager-test.js → test/IgnoreNodeManager-test.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
// LICENSE : MIT
import assert from 'assert'
import { textlint } from "textlint"
import IgnoreNodeManager from "../src/IgnoreNodeManager";
import { IgnoreNodeManager } from "../src/";
import { TxtNodeType, TxtParentNode } from "@textlint/ast-node-types";

describe("IgnoreNodeManager", function() {
afterEach(function() {
describe("IgnoreNodeManager", function () {
afterEach(function () {
textlint.resetRules();
});
describe("#ignoreChildrenByTypes()", () => {
Expand All @@ -17,11 +18,12 @@ This is **ignored**.
`;
const ignoreManager = new IgnoreNodeManager();
textlint.setupRules({
"rule-key": function(context) {
const { Syntax, RuleError, report, getSource } = context;
"rule-key": function (context: any) {
const { Syntax } = context;
return {
[Syntax.Paragraph](node) {
ignoreManager.ignoreChildrenByTypes(node, [context.Syntax.Code, context.Syntax.Strong]);
[Syntax.Paragraph](node: any) {
const ignoredNodeTypes: TxtNodeType[] = [context.Syntax.Code, context.Syntax.Strong];
ignoreManager.ignoreChildrenByTypes(node, ignoredNodeTypes);
}
}
}
Expand All @@ -38,7 +40,7 @@ This is **ignored**.
});
it("should ignore range by index", () => {
const text = "123`456`789";
const expectedList = [
const expectedList: { name: string; ignored: boolean; actual?: boolean }[] = [
{
name: "1",
ignored: false
Expand Down Expand Up @@ -78,10 +80,10 @@ This is **ignored**.
];
const ignoreManager = new IgnoreNodeManager();
textlint.setupRules({
"rule-key": function(context) {
const { Syntax, RuleError, report, getSource } = context;
"rule-key": function (context: any) {
const { Syntax, getSource } = context;
return {
[Syntax.Paragraph](node) {
[Syntax.Paragraph](node: TxtParentNode) {
ignoreManager.ignoreChildrenByTypes(node, [context.Syntax.Code]);
const text = getSource(node);
expectedList.forEach(item => {
Expand All @@ -95,7 +97,7 @@ This is **ignored**.
return textlint.lintMarkdown(text).then(() => {
expectedList.forEach(item => {
assert.strictEqual(item.actual, item.ignored, `${item.name} should be ${item.ignored ? "ignored"
: "includes"}`);
: "includes"}`);
});
assert.deepStrictEqual(ignoreManager.ignoredRanges, [[3, 8]]);
});
Expand Down
2 changes: 1 addition & 1 deletion test/mocha.opts
Original file line number Diff line number Diff line change
@@ -1 +1 @@
--require @babel/register
--require ts-node-test-register
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
// LICENSE : MIT
import assert from 'assert'
import RuleHelper from "../src/textlint-rule-helper";
import { RuleHelper } from "../src/";
import { textlint } from "textlint"
import { TxtNode, TxtParentNode } from "@textlint/ast-node-types"

describe("textlint-rule-helper-test", function() {
afterEach(function() {
describe("textlint-rule-helper-test", function () {
afterEach(function () {
textlint.resetRules();
});
describe("#getParents()", () => {
Expand All @@ -13,10 +14,10 @@ describe("textlint-rule-helper-test", function() {
const text = "# Header";
let parents = [];
textlint.setupRules({
"rule-key": function(context) {
"rule-key": function (context: any) {
var helper = new RuleHelper(context);
return {
[context.Syntax.Document](node) {
[context.Syntax.Document](node: TxtParentNode) {
parents = helper.getParents(node)
}
}
Expand All @@ -32,10 +33,10 @@ describe("textlint-rule-helper-test", function() {
const text = "# Header";
let parents = [];
textlint.setupRules({
"rule-key": function(context) {
"rule-key": function (context: any) {
var helper = new RuleHelper(context);
return {
[context.Syntax.Str](node) {
[context.Syntax.Str](node: TxtNode) {
parents = helper.getParents(node)
}
}
Expand All @@ -50,12 +51,12 @@ describe("textlint-rule-helper-test", function() {
context("when the parent node is Paragraph, the child node is Str", () => {
it("should be true", () => {
const text = "text";
let result;
let result: boolean;
textlint.setupRules({
"rule-key": function(context) {
"rule-key": function (context: any) {
var helper = new RuleHelper(context);
return {
[context.Syntax.Str](node) {
[context.Syntax.Str](node: TxtNode) {
result = helper.isChildNode(node, [context.Syntax.Paragraph])
}
}
Expand All @@ -69,12 +70,12 @@ describe("textlint-rule-helper-test", function() {
context("when the parent node is Str, the child node is Paragraph", () => {
it("should be false", () => {
const text = "text";
let result;
let result: boolean;
textlint.setupRules({
"rule-key": function(context) {
"rule-key": function (context: any) {
var helper = new RuleHelper(context);
return {
[context.Syntax.Paragraph](node) {
[context.Syntax.Paragraph](node: TxtParentNode) {
result = helper.isChildNode(node, [context.Syntax.Str])
}
}
Expand Down
12 changes: 12 additions & 0 deletions test/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"extends": "../tsconfig.json",
"compilerOptions": {
"declaration": false,
"noEmit": true,
"allowJs": true
},
"include": [
"../src/**/*",
"./**/*"
]
}
36 changes: 36 additions & 0 deletions tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
{
"compilerOptions": {
/* Basic Options */
"module": "commonjs",
"moduleResolution": "node",
"newLine": "LF",
"outDir": "./lib/",
"target": "es5",
"sourceMap": true,
"declaration": true,
"jsx": "preserve",
"lib": [
"es2017",
"dom"
],
"esModuleInterop": true,
/* Strict Type-Checking Options */
"strict": true,
/* Additional Checks */
"noUnusedLocals": true,
/* Report errors on unused locals. */
"noUnusedParameters": true,
/* Report errors on unused parameters. */
"noImplicitReturns": true,
/* Report error when not all code paths in function return a value. */
"noFallthroughCasesInSwitch": true
/* Report errors for fallthrough cases in switch statement. */
},
"include": [
"src/**/*"
],
"exclude": [
".git",
"node_modules"
]
}
Loading

0 comments on commit 844a361

Please sign in to comment.