Registry /
testing / eslint-template-visitor
Install & Compatibility
Where this runs
tested against v? · npm install
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
muslnode 18–226 runs
build_error
glibcnode 18–226 runs
build_error
Code
Verified usage
Verified import paths — ran on the pinned version, not inferred.
default
✓ import eslintTemplateVisitor from 'eslint-template-visitor';
✗ const { eslintTemplateVisitor } = require('eslint-template-visitor');
Default export is the factory function. Named import is incorrect.
default (require)
✓ const eslintTemplateVisitor = require('eslint-template-visitor');
✗ const { template } = require('eslint-template-visitor');
CommonJS require returns the default export directly.
template (after calling factory)
✓ const templates = eslintTemplateVisitor(); const { template } = templates;
✗ const { template } = eslintTemplateVisitor();
template is a method on the object returned by the factory function, not a named export of the module.
ESLint rule using template visitor to detect and optionally fix .substr() calls.
const eslintTemplateVisitor = require('eslint-template-visitor');
const templates = eslintTemplateVisitor();
const objVar = templates.variable();
const argVar = templates.spreadVariable();
const substrCallTemplate = templates.template`${objVar}.substr(${argVar})`;
module.exports = {
meta: {
type: 'suggestion',
docs: {
description: 'Prefer String#slice() over String#substr()',
},
fixable: 'code',
},
create(context) {
return templates.visitor({
[substrCallTemplate](node) {
const objectNode = substrCallTemplate.context.getMatch(objVar);
const argumentNodes = substrCallTemplate.context.getMatch(argVar);
const problem = {
node,
message: 'Prefer `String#slice()` over `String#substr()`.',
};
if (argumentNodes.length === 0) {
problem.fix = fixer => fixer.replaceText(node, context.getSourceCode().getText(objectNode) + '.slice()');
}
context.report(problem);
},
});
},
};
Errors
Common errors & fixes
TypeError: templates.template is not a function
Calling template on the default export directly without calling eslintTemplateVisitor first.
fixCall eslintTemplateVisitor() to get the templates object, then use templates.template.
Error: Template must have exactly one top-level node
Template string contains multiple top-level statements or expressions.
fixWrap the template in a block or ensure only one top-level node.
Cannot find module '@babel/eslint-parser'
@babel/eslint-parser is required as a dependency since v2.3.0 but not installed.
fixInstall @babel/eslint-parser: npm install --save-dev @babel/eslint-parser
TypeError: context.getMatch is not a function
Accessing getMatch incorrectly (e.g., on context instead of template context).
fixUse substrCallTemplate.context.getMatch(objVar) not context.getMatch.
ReferenceError: eslintTemplateVisitor is not defined
Forgetting to import/require the module.
fixAdd const eslintTemplateVisitor = require('eslint-template-visitor'); or import statement. Audit
Dependencies
eslintrequiredPeer dependency required for ESLint rule creation and AST parsing context.