UNPKG

@textlint/kernel

Version:
101 lines 3.84 kB
import { TextlintRuleContextFixCommandGeneratorImpl } from "./TextlintRuleContextFixCommandGeneratorImpl"; import { TextlintRuleSeverityLevelKeys } from "./TextlintRuleSeverityLevelKeys"; import { TextlintRuleErrorImpl } from "./TextlintRuleErrorImpl"; import { createPaddingLocator } from "./TextlintRulePaddingLocator"; import { invariant } from "../util/invariant"; const ruleFixer = new TextlintRuleContextFixCommandGeneratorImpl(); export class TextlintRuleContextImpl { constructor(args) { /** * report function that is called in a rule */ this.report = (node, ruleError, _shouldNotUsed) => { invariant(!(node instanceof TextlintRuleErrorImpl), "1st argument should be node. Usage: `report(node, ruleError);`"); invariant(_shouldNotUsed === undefined, "3rd argument should not be used. Usage: `report(node, ruleError);`"); if (ruleError instanceof TextlintRuleErrorImpl) { // severity come from `.textlintrc` option like `{ "<rule-name>" : { serverity: "warning" } } ` this._report({ ruleId: this._ruleId, node, severity: this._severityLevel, ruleError }); } else { const ruleReportedObject = ruleError; // severity come from report arguments like `report(node, { severity: 1 })` const level = ruleReportedObject.severity || TextlintRuleSeverityLevelKeys.error; this._report({ ruleId: this._ruleId, node, severity: level, ruleError: ruleReportedObject }); } }; /** * get file path current processing. */ this.getFilePath = () => { return this._sourceCode.getFilePath(); }; /** * Gets the source code for the given node. * @param {TxtNode=} node The AST node to get the text for. * @param {int=} beforeCount The number of characters before the node to retrieve. * @param {int=} afterCount The number of characters after the node to retrieve. * @returns {string} The text representing the AST node. */ this.getSource = (node, beforeCount, afterCount) => { return this._sourceCode.getSource(node, beforeCount, afterCount); }; /** * get config base directory path * config base directory path often is the place of .textlintrc * * e.g.) /path/to/dir/.textlintrc * `getConfigBaseDir()` return `"/path/to/dir/"`. * * When using textlint as module, it is specified by `configBaseDir` * If not found the value, return undefined. * * You can use it for resolving relative path from config dir. * @returns {string|undefined} */ this.getConfigBaseDir = () => { return this._configBaseDir; }; this._ruleId = args.ruleId; this._sourceCode = args.sourceCode; this._report = args.report; this.locator = createPaddingLocator(); this._configBaseDir = args.configBaseDir; this._severityLevel = args.severityLevel; Object.freeze(this); } /** * Rule id * @returns {string} */ get id() { return this._ruleId; } /** * severity level */ get severity() { return this._severityLevel; } /** * Node's type values * @type {ASTNodeTypes} */ get Syntax() { return this._sourceCode.getSyntax(); } /** * CustomError object * @type {RuleError} */ get RuleError() { return TextlintRuleErrorImpl; } /** * Rule fixer command object * @type {RuleFixer} */ get fixer() { return ruleFixer; } } //# sourceMappingURL=TextlintRuleContextImpl.js.map