textlint
Version:
The pluggable linting tool for text and markdown.
184 lines (183 loc) • 8.12 kB
JavaScript
// LICENSE : MIT
;
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
__setModuleDefault(result, mod);
return result;
};
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.TextLintModuleResolver = void 0;
const path = __importStar(require("path"));
const textlint_package_name_util_1 = require("../textlint-package-name-util");
const package_prefix_1 = require("../config/package-prefix");
const debug_1 = __importDefault(require("debug"));
const resolver_1 = require("@textlint/resolver");
const tryResolve = (packageName) => {
return (0, resolver_1.tryResolve)(packageName, {
parentModule: "textlint-legacy"
});
};
const debug = (0, debug_1.default)("textlint:module-resolver");
/**
* This class aim to resolve textlint's package name and get the module path.
*
* Define
*
* - `package` is npm package
* - `module` is package's main module
*
* ## Support
*
* - textlint-rule-*
* - textlint-preset-*
* - textlint-plugin-*
* - textlint-config-*
*/
class TextLintModuleResolver {
constructor(config) {
/**
* @type {string} baseDirectory for resolving
*/
this.baseDirectory = config && config.rulesBaseDirectory ? config.rulesBaseDirectory : "";
}
/**
* Take package name, and return path to module.
* @param {string} packageName
* @returns {string} return path to module
*/
resolveRulePackageName(packageName) {
const baseDir = this.baseDirectory;
const fullPackageName = (0, textlint_package_name_util_1.createFullPackageName)(package_prefix_1.PackageNamePrefix.rule, packageName);
// <rule-name> or textlint-rule-<rule-name>
const pkgPath = tryResolve(path.join(baseDir, fullPackageName)) || tryResolve(path.join(baseDir, packageName));
if (!pkgPath) {
debug(`rule fullPackageName: ${fullPackageName}`);
throw new ReferenceError(`Failed to load textlint's rule module: "${packageName}" is not found.
See FAQ: https://github.com/textlint/textlint/blob/master/docs/faq/failed-to-load-textlints-module.md
`);
}
return pkgPath;
}
/**
* Take package name, and return path to module.
* @param {string} packageName
* @returns {string} return path to module
*/
resolveFilterRulePackageName(packageName) {
const baseDir = this.baseDirectory;
const fullPackageName = (0, textlint_package_name_util_1.createFullPackageName)(package_prefix_1.PackageNamePrefix.filterRule, packageName);
// <rule-name> or textlint-filter-rule-<rule-name> or @scope/<rule-name>
const pkgPath = tryResolve(path.join(baseDir, fullPackageName)) || tryResolve(path.join(baseDir, packageName));
if (!pkgPath) {
debug(`filter rule fullPackageName: ${fullPackageName}`);
throw new ReferenceError(`Failed to load textlint's filter rule module: "${packageName}" is not found.
See FAQ: https://github.com/textlint/textlint/blob/master/docs/faq/failed-to-load-textlints-module.md
`);
}
return pkgPath;
}
/**
* Take package name, and return path to module.
* @param {string} packageName
* @returns {string} return path to module
*/
resolvePluginPackageName(packageName) {
const baseDir = this.baseDirectory;
const fullPackageName = (0, textlint_package_name_util_1.createFullPackageName)(package_prefix_1.PackageNamePrefix.plugin, packageName);
// <plugin-name> or textlint-plugin-<rule-name>
const pkgPath = tryResolve(path.join(baseDir, fullPackageName)) || tryResolve(path.join(baseDir, packageName));
if (!pkgPath) {
debug(`plugin fullPackageName: ${fullPackageName}`);
throw new ReferenceError(`Failed to load textlint's plugin module: "${packageName}" is not found.
See FAQ: https://github.com/textlint/textlint/blob/master/docs/faq/failed-to-load-textlints-module.md
`);
}
return pkgPath;
}
/**
* Take package name, and return path to module.
* @param {string} packageName
* The user must specify preset- prefix to these `packageName`.
* @returns {string} return path to module
*/
resolvePresetPackageName(packageName) {
const baseDir = this.baseDirectory;
const PREFIX = package_prefix_1.PackageNamePrefix.rulePreset;
/* Implementation Note
preset name is defined in config file:
In the case, `packageName` is "preset-gizmo"
TextLintModuleResolver resolve "preset-gizmo" to "textlint-rule-preset-gizmo"
{
"rules": {
"preset-gizmo": {
"ruleA": false
}
}
}
*/
// preset-<name> or textlint-rule-preset-<name>
// @scope/preset-<name> or @scope/textlint-rule-preset-<name>
const packageNameWithoutPreset = packageName
.replace(/^preset-/, "")
.replace(/^@([^/]+)\/preset-(.*)$/, `@$1/$2`);
const fullPackageName = (0, textlint_package_name_util_1.createFullPackageName)(PREFIX, packageNameWithoutPreset);
const fullFullPackageName = `${PREFIX}${packageNameWithoutPreset}`;
const pkgPath =
// textlint-rule-preset-<preset-name> or @scope/textlint-rule-preset-<preset-name>
tryResolve(path.join(baseDir, fullFullPackageName)) ||
// <preset-name>
tryResolve(path.join(baseDir, packageNameWithoutPreset)) ||
// <rule-name>
tryResolve(path.join(baseDir, fullPackageName)) ||
// <package-name>
tryResolve(path.join(baseDir, packageName));
if (!pkgPath) {
debug(`preset fullPackageName: ${fullPackageName}`);
debug(`preset fullFullPackageName: ${fullFullPackageName}`);
throw new ReferenceError(`Failed to load textlint's preset module: "${packageName}" is not found.
See FAQ: https://github.com/textlint/textlint/blob/master/docs/faq/failed-to-load-textlints-module.md
`);
}
return pkgPath;
}
/**
* Take Config package name, and return path to module.
* @param {string} packageName
* @returns {string} return path to module
*/
resolveConfigPackageName(packageName) {
const baseDir = this.baseDirectory;
const fullPackageName = (0, textlint_package_name_util_1.createFullPackageName)(package_prefix_1.PackageNamePrefix.config, packageName);
// <plugin-name> or textlint-config-<rule-name>
const pkgPath = tryResolve(path.join(baseDir, fullPackageName)) || tryResolve(path.join(baseDir, packageName));
if (!pkgPath) {
throw new ReferenceError(`Failed to load textlint's config module: "${packageName}" is not found.
See FAQ: https://github.com/textlint/textlint/blob/master/docs/faq/failed-to-load-textlints-module.md
`);
}
return pkgPath;
}
}
exports.TextLintModuleResolver = TextLintModuleResolver;
//# sourceMappingURL=textlint-module-resolver.js.map