Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
/*
Task
- check already define
- resolve package name
- load package
- emit rule
*/
// ignore already defined rule
// ignore rules from rulePaths because avoid ReferenceError is that try to require.
if (isFile(ruleName)) {
const ruleCreator = moduleInterop(require(ruleName));
const ruleEntry = [ruleName, ruleCreator];
this.emit(TextLintModuleLoader.Event.filterRule, ruleEntry);
return;
}
const definedRuleName = normalizeTextlintFilterRuleKey(ruleName);
// ignore plugin's rule
if (isPluginRuleKey(definedRuleName)) {
Logger.warn(`${definedRuleName} is Plugin's rule. This is unknown case, please report issue.`);
return;
}
const pkgPath = this.moduleResolver.resolveFilterRulePackageName(ruleName);
debug("Loading filter rules from %s", pkgPath);
const ruleCreator = moduleInterop(require(pkgPath));
const ruleEntry = [definedRuleName, ruleCreator];
this.emit(TextLintModuleLoader.Event.filterRule, ruleEntry);
}
}
loadPlugin(pluginName: string) {
const pkgPath = this.moduleResolver.resolvePluginPackageName(pluginName);
debug("Loading rules from plugin: %s", pkgPath);
const plugin = moduleInterop(require(pkgPath));
const pluginNameWithoutPrefix = normalizeTextlintPluginKey(pluginName);
// Notes: plugins not support "rules" and "rulesConfig"
// https://github.com/textlint/textlint/issues/291
if (plugin.hasOwnProperty("rules")) {
throw new Error(`textlint plugins not support "rules" and "rulesConfig".
But ${pluginName} has these filed.
For more details, See https://github.com/textlint/textlint/issues/291`);
}
// register plugin.Processor
if (!plugin.hasOwnProperty("Processor")) {
throw new Error(`textlint plugin should have "Processor".
For more details. See https://github.com/textlint/textlint/blob/master/docs/plugin.md`);
}
const pluginEntry = [pluginNameWithoutPrefix, plugin];
this.emit(TextLintModuleLoader.Event.plugin, pluginEntry);
}
Task
- check already define
- resolve package name
- load package
- emit rule
*/
// ruleName is filePath
if (isFile(ruleName)) {
const ruleCreator = moduleInterop(require(ruleName));
const ruleEntry = [ruleName, ruleCreator];
this.emit(TextLintModuleLoader.Event.rule, ruleEntry);
return;
}
// ignore already defined rule
// ignore rules from rulePaths because avoid ReferenceError is that try to require.
const definedRuleName = normalizeTextlintRuleKey(ruleName);
// ignore plugin's rule
if (isPluginRuleKey(definedRuleName)) {
Logger.warn(`${definedRuleName} is Plugin's rule. This is unknown case, please report issue.`);
return;
}
const pkgPath = this.moduleResolver.resolveRulePackageName(ruleName);
debug("Loading rules from %s", pkgPath);
const ruleCreator = moduleInterop(require(pkgPath));
const ruleEntry = [definedRuleName, ruleCreator];
this.emit(TextLintModuleLoader.Event.rule, ruleEntry);
}
loadPreset(presetName: string) {
/*
Caution: Rules of preset are defined as following.
{
"rules": {
"preset-gizmo": {
"ruleA": false
}
}
It mean that "ruleA" is defined as "preset-gizmo/ruleA"
*/
const presetRuleNameWithoutPrefix = normalizeTextlintRulePresetKey(presetName);
// ignore plugin's rule
if (isPluginRuleKey(presetRuleNameWithoutPrefix)) {
Logger.warn(`${presetRuleNameWithoutPrefix} is Plugin's rule. This is unknown case, please report issue.`);
return;
}
const pkgPath = this.moduleResolver.resolvePresetPackageName(presetName);
debug("Loading rules from preset: %s", pkgPath);
const preset = moduleInterop(require(pkgPath));
const entities = TextLintModuleMapper.createEntities(preset.rules, presetRuleNameWithoutPrefix);
entities.forEach(entry => {
this.emit(TextLintModuleLoader.Event.rule, entry);
});
}
return pkgNames.filter(pkgName => {
const ruleOrFilter =
pkgName.indexOf(TextlintPackageNamePrefix.filterRule) !== -1 ||
pkgName.indexOf(TextlintPackageNamePrefix.rule) !== -1;
if (pkgName === "textlint-rule-helper") {
return false;
}
return ruleOrFilter;
});
})
.filter(pkgName => {
return pkgName.indexOf(TextlintPackageNamePrefix.filterRule) !== -1;
})
.map(filterName => {
return pkgNames.filter(pkgName => {
const ruleOrFilter =
pkgName.indexOf(TextlintPackageNamePrefix.filterRule) !== -1 ||
pkgName.indexOf(TextlintPackageNamePrefix.rule) !== -1;
if (pkgName === "textlint-rule-helper") {
return false;
}
return ruleOrFilter;
});
})
.filter(pkgName => {
return pkgName.indexOf(TextlintPackageNamePrefix.rule) !== -1;
})
.map(filterName => {
Object.keys(rulesConfig).forEach(ruleName => {
const normalizedKey = normalizeTextlintPresetSubRuleKey({ preset: presetName, rule: ruleName });
mapped[normalizedKey] = rulesConfig[ruleName];
});
return mapped;