Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(types): move type definition for rule to @textlint/types #562

Merged
merged 17 commits into from Dec 30, 2018
Merged
Show file tree
Hide file tree
Changes from 16 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 5 additions & 0 deletions packages/@textlint/ast-node-types/src/index.ts
Expand Up @@ -36,6 +36,11 @@ export enum ASTNodeTypes {
*/
export type TxtNodeType = keyof typeof ASTNodeTypes | string;

/**
* Any TxtNode types
*/
export type AnyTxtNode = TxtNode | TxtTextNode | TxtParentNode;
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

AnyTxtNode is new type.
It is conventinal type.


/**
* Basic TxtNode
* Probably, Real TxtNode implementation has more properties.
Expand Down
2 changes: 1 addition & 1 deletion packages/@textlint/fixer-formatter/package.json
Expand Up @@ -34,7 +34,7 @@
"test": "mocha \"test/**/*.{js,ts}\""
},
"dependencies": {
"@textlint/kernel": "^3.0.1",
"@textlint/types": "^1.0.0",
"chalk": "^1.1.3",
"debug": "^2.1.0",
"diff": "^2.2.2",
Expand Down
@@ -1,6 +1,6 @@
// LICENSE : MIT
"use strict";
import { TextlintFixResult } from "@textlint/kernel";
import { TextlintFixResult } from "@textlint/types";
function getMessageType(message: any) {
if (message.fatal || message.severity === 2) {
return "Error";
Expand Down
2 changes: 1 addition & 1 deletion packages/@textlint/fixer-formatter/src/formatters/diff.ts
Expand Up @@ -3,7 +3,7 @@ const fs = require("fs");
const isFile = require("is-file");
const jsdiff = require("diff");
const chalk = require("chalk");
import { TextlintFixResult } from "@textlint/kernel";
import { TextlintFixResult } from "@textlint/types";
/**
* Given a word and a count, append an s if count is not one.
* @param {string} word A word in its singular form.
Expand Down
2 changes: 1 addition & 1 deletion packages/@textlint/fixer-formatter/src/formatters/json.ts
@@ -1,6 +1,6 @@
// LICENSE : MIT
"use strict";
import { TextlintFixResult } from "@textlint/kernel";
import { TextlintFixResult } from "@textlint/types";
export default function(results: TextlintFixResult[]) {
return JSON.stringify(results);
}
Expand Up @@ -2,7 +2,7 @@
const chalk = require("chalk");
const table = require("text-table");
const widthOfString = require("string-width");
import { TextlintFixResult } from "@textlint/kernel";
import { TextlintFixResult } from "@textlint/types";

/**
* Given a word and a count, append an s if count is not one.
Expand Down
2 changes: 1 addition & 1 deletion packages/@textlint/fixer-formatter/src/index.ts
@@ -1,6 +1,6 @@
// LICENSE : MIT
"use strict";
import { TextlintFixResult } from "@textlint/kernel";
import { TextlintFixResult } from "@textlint/types";

const fs = require("fs");
const path = require("path");
Expand Down
3 changes: 2 additions & 1 deletion packages/@textlint/fixer-formatter/test/tsconfig.json
Expand Up @@ -4,7 +4,8 @@
"outDir": "../out",
"allowJs": true,
"declaration": false,
"sourceMap": true
"sourceMap": true,
"noEmit": true
},
"include": [
"../src/**/*",
Expand Down
7 changes: 3 additions & 4 deletions packages/@textlint/kernel/package.json
Expand Up @@ -13,8 +13,7 @@
"author": "azu",
"files": [
"bin/",
"lib/",
"src/"
"lib/"
],
"main": "lib/kernel/src/index.js",
"typings": "lib/kernel/src/index.d.ts",
Expand All @@ -34,6 +33,7 @@
},
"dependencies": {
"@textlint/ast-node-types": "^4.0.3",
"@textlint/types": "^1.0.0",
"@textlint/ast-traverse": "^2.0.9",
"@textlint/feature-flag": "^3.0.5",
"@types/bluebird": "^3.5.18",
Expand All @@ -57,7 +57,6 @@
"shelljs": "^0.8.2",
"ts-node": "^7.0.0",
"ts-node-test-register": "^4.0.0",
"typescript": "~2.8.4",
"unist-util-select": "^1.5.0"
"typescript": "~2.8.4"
}
}
41 changes: 25 additions & 16 deletions packages/@textlint/kernel/src/core/source-location.ts
@@ -1,10 +1,13 @@
// LICENSE : MIT
"use strict";
import SourceCode from "./source-code";
import RuleError, { RuleErrorPadding } from "./rule-error";
import {
TextlintRuleContextReportFunctionArgs,
TextlintRuleError,
TextlintRuleErrorPadding,
TextlintSourceCode
} from "@textlint/types";
import { TxtNode } from "@textlint/ast-node-types";
import { ReportArgs } from "../task/textlint-core-task";
import { TextlintFixCommand } from "@textlint/kernel";
import { TextlintMessageFixCommand } from "@textlint/kernel";

const assert = require("assert");
const ObjectAssign = require("object-assign");
Expand All @@ -14,20 +17,22 @@ export interface ReportMessage {
ruleId: string;
node: any;
severity: number;
ruleError: RuleError;
ruleError: TextlintRuleError;
}

export default class SourceLocation {
private source: SourceCode;
private source: TextlintSourceCode;

constructor(source: SourceCode) {
constructor(source: TextlintSourceCode) {
this.source = source;
}

/**
* adjust node's location with error's padding location.
*/
adjust(reportArgs: ReportArgs): { line: number; column: number; fix?: TextlintFixCommand } {
adjust(
reportArgs: TextlintRuleContextReportFunctionArgs
): { line: number; column: number; fix?: TextlintMessageFixCommand } {
const { node, ruleError, ruleId } = reportArgs;
const errorPrefix = `[${ruleId}]` || "";
const padding = ruleError;
Expand Down Expand Up @@ -72,24 +77,28 @@ report(node, new RuleError("message", {
// Introduced textlint 5.6
// https://github.com/textlint/textlint/releases/tag/5.6.0
// Always throw Error
throw new Error(`${errorPrefix} Have to use {line, column} or index.
=> use either one of the two
throw new Error(`${errorPrefix} Have to use one of {line, column} or {index}.
You should use either one:

use "line" and "column" property

report(node, new RuleError("message", {
line: paddingLineNumber,
column: paddingLineColumn
});

OR use "index" property
OR

use "index" property

report(node, new RuleError("message", {
index: paddingIndexValue
});
`);
}

const adjustedLoc = this._adjustLoc(node, padding, _backwardCompatibleIndexValue);
const adjustedFix = this._adjustFix(node, padding);
const adjustedLoc = this.toAbsoluteLocation(node, padding, _backwardCompatibleIndexValue);
const adjustedFix = this.toAbsolutePositionFix(node, padding);
/*
{
line,
Expand All @@ -100,7 +109,7 @@ report(node, new RuleError("message", {
return ObjectAssign({}, adjustedLoc, adjustedFix);
}

_adjustLoc(node: any, padding: RuleErrorPadding, _paddingIndex?: number) {
private toAbsoluteLocation(node: any, padding: TextlintRuleErrorPadding, _paddingIndex?: number) {
const nodeRange = node.range;
const line = node.loc.start.line;
const column = node.loc.start.column;
Expand Down Expand Up @@ -147,7 +156,7 @@ report(node, new RuleError("message", {
// Remove next version 6?
/*
new RuleError({
column: index
column: index
});
*/
if (padding.column !== undefined && padding.column > 0) {
Expand All @@ -168,7 +177,7 @@ report(node, new RuleError("message", {
* Adjust `fix` command range
* if `fix.isAbsolute` is not absolute position, adjust the position from the `node`.
*/
private _adjustFix(node: TxtNode, ruleErrorObject: RuleError) {
private toAbsolutePositionFix(node: TxtNode, ruleErrorObject: TextlintRuleError) {
const nodeRange = node.range;
// if not found `fix`, return empty object
if (ruleErrorObject.fix === undefined) {
Expand Down
@@ -1,12 +1,9 @@
// LICENSE : MIT
"use strict";
import { getFilter } from "./rule-creator-helper";
import {
TextlintFilterRuleOptions,
TextlintFilterRuleReporter,
TextlintKernelFilterRule
} from "../textlint-kernel-interface";
import { TextlintKernelFilterRule } from "../textlint-kernel-interface";
import { Descriptor } from "./Descriptor";
import { TextlintFilterRuleOptions, TextlintFilterRuleReporter } from "@textlint/types";
import deepEqual = require("deep-equal");

/**
Expand Down Expand Up @@ -53,7 +50,7 @@ export class TextlintFilterRuleDescriptor implements Descriptor<TextlintKernelFi
}
}

get rawOptions() {
get rawOptions(): undefined | boolean | TextlintFilterRuleOptions {
return this.kernelFilterRule.options;
}

Expand Down
@@ -1,8 +1,8 @@
// LICENSE : MIT
"use strict";
import { getFixer } from "./rule-creator-helper";
import { TextlintRuleReporter } from "../textlint-kernel-interface";
import { TextlintLintableRuleDescriptor } from "./TextlintLintableRuleDescriptor";
import { TextlintRuleReporter } from "@textlint/types";

/**
* Textlint Fixable Rule Descriptor.
Expand Down
@@ -1,12 +1,8 @@
// LICENSE : MIT
"use strict";
import {
TextlintKernelRule,
TextlintRuleModule,
TextlintRuleOptions,
TextlintRuleReporter
} from "../textlint-kernel-interface";
import { TextlintKernelRule } from "../textlint-kernel-interface";
import { assertRuleShape, getLinter } from "./rule-creator-helper";
import { TextlintRuleModule, TextlintRuleOptions, TextlintRuleReporter } from "@textlint/types";
import deepEqual = require("deep-equal");

/**
Expand Down Expand Up @@ -58,7 +54,7 @@ export class TextlintLintableRuleDescriptor {
}
}

get rawOptions() {
get rawOptions(): boolean | undefined | TextlintRuleOptions {
return this.textlintKernelRule.options;
}

Expand Down
@@ -1,12 +1,7 @@
"use strict";
import {
TextlintKernelPlugin,
TextlintPluginOptions,
TextlintPluginProcessor,
TextlintPluginProcessorConstructor
} from "../textlint-kernel-interface";
import { TextlintKernelPlugin } from "../textlint-kernel-interface";
import { Descriptor } from "./Descriptor";

import { TextlintPluginOptions, TextlintPluginProcessor, TextlintPluginProcessorConstructor } from "@textlint/types";
import deepEqual = require("deep-equal");

/**
Expand Down
@@ -1,7 +1,4 @@
// LICENSE : MIT
"use strict";

import { TextlintRuleReporter, TextlintFilterRuleReporter } from "../textlint-kernel-interface";
import { TextlintFilterRuleReporter, TextlintRuleReporter } from "@textlint/types";

/**
* detect that ruleCreator has linter function
Expand Down
15 changes: 6 additions & 9 deletions packages/@textlint/kernel/src/fixer/fixer-processor.ts
@@ -1,18 +1,15 @@
// LICENSE : MIT
"use strict";

import { TextlintSourceCode } from "@textlint/types";

const debug = require("debug")("textlint:fixer-processor");
import * as assert from "assert";
import FixerTask from "../task/fixer-task";
import SourceCode from "../core/source-code";
import SourceCodeFixer from "./source-code-fixer";
import TaskRunner from "../task/task-runner";
import {
TextlintFixResult,
TextlintKernelConstructorOptions,
TextlintMessage,
TextlintPluginProcessor
} from "../textlint-kernel-interface";
import { TextlintKernelConstructorOptions } from "../textlint-kernel-interface";
import { TextlintFixResult, TextlintMessage, TextlintPluginProcessor } from "@textlint/types";
import MessageProcessManager from "../messages/MessageProcessManager";
import { TextlintFilterRuleDescriptors, TextlintRuleDescriptors } from "../descriptor";

Expand All @@ -21,7 +18,7 @@ export interface FixerProcessorProcessArgs {
configBaseDir?: string;
ruleDescriptors: TextlintRuleDescriptors;
filterRules: TextlintFilterRuleDescriptors;
sourceCode: SourceCode;
sourceCode: TextlintSourceCode;
}

export default class FixerProcessor {
Expand Down Expand Up @@ -69,7 +66,7 @@ export default class FixerProcessor {
const fixerProcessList = ruleDescriptors.fixableDescriptors.map(ruleDescriptor => {
return (sourceText: string): Promise<string> => {
// create new SourceCode object
const newSourceCode = new SourceCode({
const newSourceCode = new TextlintSourceCode({
text: sourceText,
ast: preProcess(sourceText, sourceCode.filePath),
filePath: resultFilePath,
Expand Down
10 changes: 5 additions & 5 deletions packages/@textlint/kernel/src/fixer/rule-fixer.ts
@@ -1,6 +1,6 @@
import * as assert from "assert";
import { TxtNode } from "@textlint/ast-node-types";
import { SourceCodeRange } from "../core/source-code";
import { TextlintSourceCodeRange } from "@textlint/types";

/**
* Fix Command object has `range` and `text`.
Expand Down Expand Up @@ -72,7 +72,7 @@ export default class RuleFixer {
* @param {string} text The text to insert.
* @returns {IntermediateFixCommand} The fix command.
*/
insertTextAfterRange(range: SourceCodeRange, text: string) {
insertTextAfterRange(range: TextlintSourceCodeRange, text: string) {
return insertTextAt(range[1], text);
}

Expand All @@ -96,7 +96,7 @@ export default class RuleFixer {
* @param {string} text The text to insert.
* @returns {IntermediateFixCommand} The fix command.
*/
insertTextBeforeRange(range: SourceCodeRange, text: string) {
insertTextBeforeRange(range: TextlintSourceCodeRange, text: string) {
return insertTextAt(range[0], text);
}

Expand Down Expand Up @@ -124,7 +124,7 @@ export default class RuleFixer {
* @param {string} text The text to insert.
* @returns {IntermediateFixCommand} The fix command.
*/
replaceTextRange(range: SourceCodeRange, text: string) {
replaceTextRange(range: TextlintSourceCodeRange, text: string) {
return {
range,
text,
Expand All @@ -150,7 +150,7 @@ export default class RuleFixer {
* The `range` should be **relative** value from reported node.
* @returns {IntermediateFixCommand} The fix command.
*/
removeRange(range: SourceCodeRange) {
removeRange(range: TextlintSourceCodeRange) {
return this.replaceTextRange(range, "");
}
}