Skip to content

Commit

Permalink
refactor(kernel): Move TxtNode interface to @textlint/ast-node-types (#…
Browse files Browse the repository at this point in the history
…358)

* refactor(kernel): Move TxtNode interface to @textlint/ast-node-types

* fix JSDOc

* refactor(ast-node-types): Update comment

* docs(ast-node-types): Update
  • Loading branch information
azu committed Dec 8, 2017
1 parent 2bb426c commit 55d2a2f
Show file tree
Hide file tree
Showing 10 changed files with 90 additions and 51 deletions.
10 changes: 7 additions & 3 deletions packages/@textlint/ast-node-types/README.md
Expand Up @@ -5,9 +5,7 @@ The definition for textlint AST Node types.
This module for parse plugin.


Please see for more details:

- <https://github.com/textlint/textlint/blob/master/docs/txtnode.md>
For more details, see [TxtNode document](https://github.com/textlint/textlint/blob/master/docs/txtnode.md).

## Installation

Expand All @@ -20,6 +18,12 @@ import { ASTNodeTypes } from "@textlint/ast-node-types";
console.log(ASTNodeTypes.Document)
```

### Type interface for TxtNode

This library include type interface of [TxtNode](https://github.com/textlint/textlint/blob/master/docs/txtnode.md).
TypeScript user can use it as type of TxtNode.


## For parser creator

Please use it for creating your textlint-plugin parser.
Expand Down
69 changes: 69 additions & 0 deletions packages/@textlint/ast-node-types/src/TextLintASTNodeTypes.ts
Expand Up @@ -2,6 +2,7 @@
"use strict";
/**
* AST Node types list on TxtNode.
* Constant value of types
* @see https://github.com/textlint/textlint/blob/master/docs/txtnode.md
*/
export const ASTNodeTypes = {
Expand All @@ -27,3 +28,71 @@ export const ASTNodeTypes = {
Code: "Code",
Delete: "Delete"
};
/**
* Key of TxtNode type
* Union Types
*/
export type TxtNodeType = keyof typeof ASTNodeTypes;

/**
* Basic TxtNode
*/
export interface TxtNode {
type: TxtNodeType | string;
raw: string;
range: TextNodeRange;
loc: TxtNodeLineLocation;
// parent is runtime information
// Not need in AST
parent?: TxtNode;
}

/**
* Inline Text Node.
* For example, Str Node.
*/
export interface TxtTextNode extends TxtNode {
value: string;
}

/**
* Parent Node.
* For example, Paragraph Node
*/
export interface TxtParentNode extends TxtNode {
children: TxtNode[] | TxtTextNode[];
}

/**
* Root Node.
* Root Node is only one in the document.
* In other words, Root Node is Document Node.
*/
export interface TxtRootNode extends TxtNode {
type: "Document";
children: TxtNode[];
}

/**
* Location
*/
export interface TxtNodeLineLocation {
start: TxtNodePosition;
end: TxtNodePosition;
}

/**
* Position's line start with 1.
* Position's column start with 0.
* This is for compatibility with JavaScript AST.
* https://gist.github.com/azu/8866b2cb9b7a933e01fe
*/
export interface TxtNodePosition {
line: number; // start with 1
column: number; // start with 0
}

/**
* Range start with 0
*/
export type TextNodeRange = [number, number];
2 changes: 1 addition & 1 deletion packages/@textlint/kernel/src/core/filter-rule-context.ts
@@ -1,7 +1,7 @@
// LICENSE : MIT
"use strict";
import SourceCode from "./source-code";
import { TxtNode } from "../textlint-kernel-interface";
import { TxtNode } from "@textlint/ast-node-types";
import RuleError from "./rule-error";
import { ShouldIgnoreFunction } from "../task/textlint-core-task";

Expand Down
4 changes: 3 additions & 1 deletion packages/@textlint/kernel/src/core/rule-context.ts
@@ -1,12 +1,14 @@
// LICENSE : MIT
"use strict";

const assert = require("assert");
import { TxtNode } from "@textlint/ast-node-types";
import RuleFixer from "../fixer/rule-fixer";
import RuleError from "./rule-error";
import SeverityLevel from "../shared/type/SeverityLevel";
import { getSeverity } from "../shared/rule-severity";
import SourceCode from "./source-code";
import { TextlintRuleOptions, TxtNode } from "../textlint-kernel-interface";
import { TextlintRuleOptions } from "../textlint-kernel-interface";
import { ReportFunction } from "../task/textlint-core-task";
// instance for rule context
const ruleFixer = new RuleFixer();
Expand Down
4 changes: 2 additions & 2 deletions packages/@textlint/kernel/src/core/rule-creator-helper.ts
@@ -1,8 +1,8 @@
// LICENSE : MIT
"use strict";
import { ASTNodeTypes } from "@textlint/ast-node-types";
import { ASTNodeTypes, TxtNode } from "@textlint/ast-node-types";
import RuleContext from "./rule-context";
import { TextlintRuleOptions, TxtNode } from "../textlint-kernel-interface";
import { TextlintRuleOptions } from "../textlint-kernel-interface";
/**
* Reporter function
*
Expand Down
4 changes: 2 additions & 2 deletions packages/@textlint/kernel/src/core/source-code.ts
@@ -1,12 +1,12 @@
import { TxtNode } from "../textlint-kernel-interface";
import { TxtNode } from "@textlint/ast-node-types";

const assert = require("assert");
const StructuredSource = require("structured-source");
import { ASTNodeTypes } from "@textlint/ast-node-types";

/**
* Validates that the given AST has the required information.
* @param {TxtAST.TxtNode} [ast] The Program node of the AST to check.
* @param {TxtNode} [ast] The Program node of the AST to check.
* @throws {Error} If the AST doesn't contain the correct information.
* @returns {void}
* @private
Expand Down
3 changes: 2 additions & 1 deletion packages/@textlint/kernel/src/core/source-location.ts
Expand Up @@ -2,7 +2,8 @@
"use strict";
import SourceCode from "./source-code";
import RuleError, { RuleErrorPadding } from "./rule-error";
import { TextlintMessage, TxtNode } from "../textlint-kernel-interface";
import { TextlintMessage } from "../textlint-kernel-interface";
import { TxtNode } from "@textlint/ast-node-types";

const assert = require("assert");
const ObjectAssign = require("object-assign");
Expand Down
2 changes: 1 addition & 1 deletion packages/@textlint/kernel/src/fixer/rule-fixer.ts
@@ -1,5 +1,5 @@
import * as assert from "assert";
import { TxtNode } from "../textlint-kernel-interface";
import { TxtNode } from "@textlint/ast-node-types";
import { SourceCodeRange } from "../core/source-code";

/**
Expand Down
3 changes: 2 additions & 1 deletion packages/@textlint/kernel/src/task/textlint-core-task.ts
Expand Up @@ -12,7 +12,8 @@ import MessageType from "../shared/type/MessageType";
import { EventEmitter } from "events";
import * as assert from "assert";
import SourceCode from "../core/source-code";
import { TextLintFixCommand, TextlintRuleOptions, TxtNode } from "../textlint-kernel-interface";
import { TxtNode } from "@textlint/ast-node-types";
import { TextLintFixCommand, TextlintRuleOptions } from "../textlint-kernel-interface";
import { default as RuleContext, RuleReportedObject } from "../core/rule-context";
import { RuleCreatorReporter } from "../core/rule-creator-helper";
import FilterRuleContext from "../core/filter-rule-context";
Expand Down
40 changes: 1 addition & 39 deletions packages/@textlint/kernel/src/textlint-kernel-interface.ts
@@ -1,5 +1,5 @@
// rule config
import { ASTNodeTypes } from "@textlint/ast-node-types";
import { TxtNode } from "@textlint/ast-node-types";
import { SeverityLevelTypes } from "./shared/type/SeverityLevel";
import { TextLintRuleCreator } from "./core/rule-creator-helper";

Expand Down Expand Up @@ -49,44 +49,6 @@ export interface TextlintConfig {
quiet?: boolean;
}

// TextLint AST Node
export interface TxtNode {
type: keyof typeof ASTNodeTypes | string;
raw: string;
range: [number, number];
loc: LineLocation;
// parent is runtime information
// Not need in AST
parent?: TxtNode;
}

// Inline Node
export interface TxtTextNode extends TxtNode {
value: string;
}

// Parent Node
export interface TxtParentNode extends TxtNode {
children: TxtNode[] | TxtTextNode[];
}

export interface TxtRootNode extends TxtNode {
type: "Document";
children: TxtNode[];
}

export interface LineLocation {
start: Position;
end: Position;
}

export interface Position {
line: number; // start with 1
column: number; // start with 0
// This is for compatibility with JavaScript AST.
// https://gist.github.com/azu/8866b2cb9b7a933e01fe
}

// Plugin
export interface TextlintKernelProcessorConstructor extends Function {
// TODO: support plugin config
Expand Down

0 comments on commit 55d2a2f

Please sign in to comment.