Registry / testing / eslint-template-visitor

eslint-template-visitor

JSON →
library2.3.2jsnpmunverified

Simplify ESLint rule creation by visiting AST node templates instead of manually inspecting ESTree nodes. v2.3.2 supports ESLint >=7 and uses @babel/eslint-parser behind the scenes for template parsing. It offers a declarative API with template literals, variables, spread variables, and variable declaration variables. Maintained by futpib, with regular releases and active development. Differentiates from other template-based tools by integrating directly with ESLint's visitor pattern.

npm install eslint-template-visitor
INSTALL
IMPORT
SIG · ESLINT-TEMPLATE-VI
E
eslint-template-visitor
testingjavascriptv2.3.2
Install
—
Import
—
Disk
—
Pass rate
0/ 6
Env Coverage0 / 6
glibc
18–22
musl
18–22
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
musl
node 18–226 runs
build_error
glibc
node 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); }, }); }, };
Debug
Known issues
breakingUpgraded parser from babel-eslint to @babel/eslint-parser in v2.3.0. Existing templates that relied on babel-eslint-specific behavior may break.
fix
Update parser options if needed, ensure @babel/eslint-parser is installed as a direct dependency.
affects: >=2.3.0
deprecatedThe old babel-eslint parser is deprecated; v2.3.0 switched to @babel/eslint-parser.
fix
Upgrade to v2.3.0 or later to avoid deprecated babel-eslint.
affects: <2.3.0
gotchaTemplate cannot have more than one top-level node. Throws an error in v2.1.0+.
fix
Ensure template string contains exactly one top-level AST node.
affects: >=2.1.0
gotchaVariable declaration variable (variableDeclarationVariable) must be used in place of the declaration keyword (const/let/var), not the entire declaration node.
fix
Use ${variableDeclarationVariable} x = y; instead of ${variableDeclarationVariable} x = y; with wrong placement.
affects: >=2.1.0
gotchaSpread variable matches an array of nodes, not a single node. Mistaking it for a regular variable leads to incorrect type errors.
fix
Use templates.variable() for a single node match, templates.spreadVariable() for an array.
affects: all
breakingRemoval of support for ESLint <7.0.0. Peer dependency requires eslint >=7.
fix
Upgrade ESLint to version 7 or later.
affects: >=2.0.0
Errors
Common errors & fixes
TypeError: templates.template is not a function
Calling template on the default export directly without calling eslintTemplateVisitor first.
fix
Call 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.
fix
Wrap 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.
fix
Install @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).
fix
Use substrCallTemplate.context.getMatch(objVar) not context.getMatch.
ReferenceError: eslintTemplateVisitor is not defined
Forgetting to import/require the module.
fix
Add const eslintTemplateVisitor = require('eslint-template-visitor'); or import statement.
Upgrade
Version history
2.3.2latest on npm
Audit
Dependencies
eslintrequiredPeer dependency required for ESLint rule creation and AST parsing context.
Agent activity
6 hits · last 30 days
node
6
Resources
eslint-template-visitor — npm install eslint-template-visitor · libregistry