{"id":16106,"library":"lodash-template-js-parser","title":"Lodash Template JavaScript Parser","description":"This package provides a JavaScript parser/splitter specifically designed for `lodash.template` content. It's currently at version 1.1.1 and appears to be in an active maintenance state, with recent bug fixes and minor feature additions. Its core functionality is to accurately separate JavaScript code segments from template strings, ensuring that the original positional information of the JavaScript code is preserved. A key differentiator is its minimalist approach: unlike more comprehensive tools such as `eslint-plugin-lodash-template`, this library does not include its own JavaScript parser (e.g., espree or babel/parser). This makes it a lightweight solution for use cases like linting tools that need to extract executable JavaScript logic from `lodash` templates for external processing, without requiring a full Abstract Syntax Tree (AST) generation from this package itself.","status":"active","version":"1.1.1","language":"javascript","source_language":"en","source_url":"https://github.com/azu/lodash-template-js-parser","tags":["javascript","JavaScript","ast","lodash","parser","template","typescript"],"install":[{"cmd":"npm install lodash-template-js-parser","lang":"bash","label":"npm"},{"cmd":"yarn add lodash-template-js-parser","lang":"bash","label":"yarn"},{"cmd":"pnpm add lodash-template-js-parser","lang":"bash","label":"pnpm"}],"dependencies":[],"imports":[{"note":"The library primarily uses ES Module syntax and ships TypeScript types. While CommonJS `require` might work in some environments, it's not the idiomatic or type-safe approach.","wrong":"const { parseTemplate } = require('lodash-template-js-parser');","symbol":"parseTemplate","correct":"import { parseTemplate } from 'lodash-template-js-parser';"},{"note":"This is a TypeScript interface for options. Use `import type` to avoid bundling it at runtime if you are using TypeScript.","wrong":"import { parseTemplateOptions } from 'lodash-template-js-parser';","symbol":"parseTemplateOptions","correct":"import type { parseTemplateOptions } from 'lodash-template-js-parser';"},{"note":"Introduced in v1.1.0, this export provides the underlying MicroTemplate tokens for advanced parsing scenarios.","symbol":"tokens","correct":"import { tokens } from 'lodash-template-js-parser';"}],"quickstart":{"code":"import { parseTemplate } from \"lodash-template-js-parser\";\nimport assert from 'assert';\n\nconst content = `\nconst age = 18;\n<% if (age < 18) { %>\n    <li><%= name %> (age: <%= age %>)</li>\n<% } else { %>\n    <li>over the age limit!</li>\n<% }%>`\n\nconst { script, template } = parseTemplate(content, {\n    templateSettings: {\n        interpolate: [\"#{\", \"}\"] // Example of custom lodash template settings\n    }\n});\n\nassert.strictEqual(script, `\n               \n   if (age < 18) {   \n            name ;            age ;   \n   } else {   \n                                \n   }  \n`);\nassert.strictEqual(template, `\nconst age = 18;\n                     \n    <li>            (age:           )</li>\n              \n    <li>over the age limit!</li>\n      \n`);\n\nconsole.log('Script extracted:\\n', script);\nconsole.log('Template extracted:\\n', template);","lang":"typescript","description":"Demonstrates how to parse a lodash template, extract its JavaScript and template content, and apply custom template settings."},"warnings":[{"fix":"Always ensure the `templateSettings` provided to `parseTemplate` accurately reflect those used by `_.template` for the specific template being processed. These settings define the delimiters for `escape`, `evaluate`, and `interpolate` tags.","message":"The parser's behavior heavily relies on the `templateSettings` passed to `parseTemplate`. If these settings do not precisely match the `templateSettings` used by `lodash.template` when the original template was authored, the parsing may yield incorrect or unexpected results.","severity":"gotcha","affected_versions":">=1.0.0"},{"fix":"If an AST is needed, integrate an external JavaScript parser:\n```typescript\nimport { parseTemplate } from 'lodash-template-js-parser';\nimport * as espree from 'espree'; // or '@babel/parser'\n\nconst { script } = parseTemplate('<% var x = 1; %>', {});\nconst ast = espree.parse(script, { ecmaVersion: 2020 });\nconsole.log(ast.body.declarations.id.name); // 'x'\n```","message":"This library acts as a parser/splitter and returns JavaScript code as a plain string. It does *not* include its own JavaScript parser (e.g., for generating an AST). If you require an AST for the extracted JavaScript, you will need to pipe the `script` output to a separate JavaScript parser like `@babel/parser` or `espree`.","severity":"gotcha","affected_versions":">=1.0.0"},{"fix":"Tools consuming the `script` output should be prepared to handle and potentially trim or ignore leading/trailing whitespace. For AST parsers, this padding usually doesn't affect syntax trees but might impact source map generation if not accounted for.","message":"The package currently maintains positions by padding non-JS parts with whitespace. This means the `script` string might contain significant leading/trailing whitespace and empty lines corresponding to the original template's non-script portions. This can affect downstream tooling expectations.","severity":"gotcha","affected_versions":">=1.0.0"}],"env_vars":null,"last_verified":"2026-04-21T00:00:00.000Z","next_check":"2026-07-20T00:00:00.000Z","problems":[{"fix":"Ensure you are using ES Module syntax: `import { parseTemplate } from 'lodash-template-js-parser';`. If you are in a pure CommonJS environment (e.g., older Node.js scripts), you might need to use dynamic `import()` or configure your build system to transpile.","cause":"Attempting to use `require()` for CommonJS import in an environment that expects ES Modules, or incorrect destructuring of the module export.","error":"TypeError: parseTemplate is not a function"},{"fix":"Verify that `parserOptions.templateSettings` (specifically `escape`, `evaluate`, and `interpolate`) accurately reflect the custom delimiters configured for `lodash.template` when the template was created. For example, if your template uses `{{...}}` for interpolation, you must set `interpolate: ['{{', '}}']`.","cause":"The `templateSettings` passed to `parseTemplate` do not match the actual delimiters used in the lodash template, leading to misinterpretation of script blocks.","error":"Parsing results in empty or incorrect script/template strings despite valid input."}],"ecosystem":"npm"}