All Projects → ota-meshi → eslint-plugin-lodash-template

ota-meshi / eslint-plugin-lodash-template

Licence: MIT License
ESLint plugin for John Resig-style micro template, Lodash's template, Underscore's template and EJS.

Programming Languages

javascript
184084 projects - #8 most used programming language
HTML
75241 projects

Projects that are alternatives of or similar to eslint-plugin-lodash-template

Eslint Config Auto
Automatically configure ESLint based on project dependencies
Stars: ✭ 302 (+1913.33%)
Mutual labels:  eslint, eslint-plugin, lodash
You Dont Need Lodash Underscore
List of JavaScript methods which you can use natively + ESLint Plugin
Stars: ✭ 13,915 (+92666.67%)
Mutual labels:  eslint-plugin, lodash, underscore
Typescript Eslint
✨ Monorepo for all the tooling which enables ESLint to support TypeScript
Stars: ✭ 10,831 (+72106.67%)
Mutual labels:  eslint, eslint-plugin, eslintplugin
eslint-plugin-test-selectors
Enforces that data-test-id attributes are added to interactive DOM elements (JSX) to help with UI testing. JSX only.
Stars: ✭ 19 (+26.67%)
Mutual labels:  eslint, eslint-plugin, eslintplugin
Eslint Plugin Lodash
ESLint rules for lodash
Stars: ✭ 208 (+1286.67%)
Mutual labels:  eslint, eslint-plugin, lodash
eslint-plugin-rulesdir
An ESLint plugin to load project-specific ESLint rules
Stars: ✭ 28 (+86.67%)
Mutual labels:  eslint, eslint-plugin
gameplate
🎮 Boilerplate for creating game with WebGL & Redux 🎲
Stars: ✭ 39 (+160%)
Mutual labels:  eslint, lodash
eslint-plugin-editorconfig
An ESLint plugin to enforce EditorConfig rules
Stars: ✭ 22 (+46.67%)
Mutual labels:  eslint, eslint-plugin
eslint-plugin-react-hook-form
ESLint plugin for react-hook-form
Stars: ✭ 27 (+80%)
Mutual labels:  eslint, eslint-plugin
eslint-plugin-yml
This ESLint plugin provides linting rules for YAML.
Stars: ✭ 40 (+166.67%)
Mutual labels:  eslint-plugin, eslintplugin
eslint-plugin-disable
Disable ESLint plugins using file path patterns and inline comments
Stars: ✭ 51 (+240%)
Mutual labels:  eslint, eslint-plugin
underwater
~2kb - ES6 Collection of helper functions. Lodash like
Stars: ✭ 18 (+20%)
Mutual labels:  lodash, underscore
html-eslint
ESLint plugin for linting HTML
Stars: ✭ 72 (+380%)
Mutual labels:  eslint, eslint-plugin
eslint-plugin-pug
An ESLint plugin for linting inline scripts in Pug files
Stars: ✭ 17 (+13.33%)
Mutual labels:  eslint, eslint-plugin
Aris
Aris - A fast and powerful tool to write HTML in JS easily. Includes syntax highlighting, templates, SVG, CSS autofixing, debugger support and more...
Stars: ✭ 61 (+306.67%)
Mutual labels:  html-template, lodash
php-lodash
php-lodash is a PHP utility library, similar to Underscore/Lodash.
Stars: ✭ 35 (+133.33%)
Mutual labels:  lodash, underscore
eslint-plugin-svelte
ESLint plugin for Svelte using AST
Stars: ✭ 22 (+46.67%)
Mutual labels:  eslint, eslint-plugin
eslint-plugin-total-functions
An ESLint plugin to enforce the use of total functions (and prevent the use of partial functions) in TypeScript.
Stars: ✭ 72 (+380%)
Mutual labels:  eslint, eslint-plugin
eslint-plugin-layout-shift
ESLint plugin to force responsive media elements to set the width/height attributes
Stars: ✭ 15 (+0%)
Mutual labels:  eslint, eslint-plugin
frontplate-cli
フロントエンドビルドCLI
Stars: ✭ 25 (+66.67%)
Mutual labels:  eslint, ejs

eslint-plugin-lodash-template

ESLint plugin for John Resig-style micro templating.

npm license npm version npm downloads npm downloads npm downloads npm downloads npm downloads Build Status Coverage Status

It can be used in projects using Underscore.js and Lodash's template.

This plugin supports code checking for templates like the examples below.

<div id="<%= id %>" class="<%= (i % 2 == 1 ? ' even': '') %>">
  <div class="grid_1 alpha right">
    <img class="righted" src="<%= profile_image_url %>"/>
  </div>
  <div class="grid_6 omega contents">
    <p><b><a href="/<%= from_user %>"><%= from_user %></a>:</b> <%= text %></p>
  </div>
</div>
<% for ( var i = 0; i < users.length; i++ ) { %>
  <li><a href="<%= users[i].url %>"><%= users[i].name %></a></li>
<% } %>

Playground on the Web

output sample(on SublimeText):

sample-sublime-text

Features

  • Enable ESLint in Underscore.js and Lodash's templates.
  • You can find issues specific to template tags.
  • Improves readability of HTML templates.
  • For JavaScript (TypeScript) templates, enable ESLint both inside and outside the template tag. (This is an experimental feature)
  • Partial supports for EJS.

Installation

npm install --save-dev eslint eslint-plugin-lodash-template

Documentation

See documents.

Usage

Create .eslintrc.* file to configure rules. See also: http://eslint.org/docs/user-guide/configuring.

Example .eslintrc.js:

module.exports = {
  extends: [
    // add more generic rulesets here, such as:
    // 'eslint:recommended',
    'plugin:lodash-template/recommended-with-html'
  ],
  rules: {
    // override/add rules settings here, such as:
    // 'lodash-template/no-warning-html-comments': 'error'
  }
}

Attention

This plugin does special handling for the following rule warnings in the template.

Rule ID Process Description Another way this plugin supports
indent Disable warnings lodash-template/scriptlet-indent rule,
lodash-template/html-indent rule
strict Disable warnings --
no-empty Disable warnings --
max-statements-per-line Disable warnings --
padded-blocks Disable warnings --
no-implicit-globals Disable warnings --
no-multi-spaces Disable warnings lodash-template/no-multi-spaces-in-scriptlet rule,
lodash-template/no-multi-spaces-in-html-tag rule
no-unused-expressions Disable warnings within interpolate(<%=...%>) --
quotes Disable warnings if doublequote is set --
no-irregular-whitespace Disable warnings outside template tags lodash-template/no-irregular-whitespace rule

Configs

This plugin provides 6 predefined configs:

  • plugin:lodash-template/base - Settings and rules to enable correct ESLint parsing
  • plugin:lodash-template/best-practices - Above, plus rules to improve dev experience
  • plugin:lodash-template/recommended - Above, plus rules to improve code readability
  • plugin:lodash-template/recommended-with-html - Above, plus rules to improve code readability with HTML template
  • plugin:lodash-template/recommended-with-script - plugin:lodash-template/recommended config, plus to enable ESLint parsing of JavaScript templates (This is an experimental feature)
  • plugin:lodash-template/all - All rules of this plugin are included

Rules

The --fix option on the command line automatically fixes problems reported by rules which have a wrench 🔧 below.

Base Rules (Enabling Correct ESLint Parsing)

Enable this plugin using with:

{
  "extends": "plugin:lodash-template/base"
}
Rule ID Description
lodash-template/no-script-parsing-error disallow parsing errors in template

Best Practices (Improve Development Experience)

Enforce all the rules in this category with:

{
  "extends": "plugin:lodash-template/best-practices"
}
Rule ID Description
lodash-template/no-empty-template-tag disallow empty micro-template tag. (ex. 🆖 <% %>)
lodash-template/no-invalid-template-interpolation disallow other than expression in micro-template interpolation. (ex. 🆖 <%= if (test) { %>)
🔧 lodash-template/no-semi-in-template-interpolation disallow the semicolon at the end of expression in micro template interpolation.(ex. 🆗 <%= text %> 🆖 <%= text; %>)

Recommended (Improve Readability)

Enforce all the rules in this category and all the rules in Best Practices categories with:

{
  "extends": "plugin:lodash-template/recommended"
}
Rule ID Description
🔧 lodash-template/no-irregular-whitespace disallow irregular whitespace outside the template tags.
🔧 lodash-template/no-multi-spaces-in-scriptlet disallow multiple spaces in scriptlet. (ex. 🆖 <% if···(test)···{ %>)
🔧 lodash-template/scriptlet-indent enforce consistent indentation to scriptlet in micro-template tag.
🔧 lodash-template/template-tag-spacing enforce unified spacing in micro-template tag. (ex. 🆗 <%= prop %>, 🆖 <%=prop%>)

Recommended with HTML template (Improve Readability with HTML template)

Enforce all the rules in this category and all the rules in Best Practices/Recommended categories with:

{
  "extends": "plugin:lodash-template/recommended-with-html"
}
Rule ID Description
🔧 lodash-template/attribute-name-casing enforce HTML attribute name casing. (ex. 🆗 <div foo-bar> 🆖 <div fooBar> <div FOO-BAR>)
🔧 lodash-template/attribute-value-quote enforce quotes style of HTML attributes. (ex. 🆗 <div class="abc"> 🆖 <div class='abc'> <div class=abc>)
🔧 lodash-template/element-name-casing enforce HTML element name casing. (ex. 🆗 <xxx-element> 🆖 <xxxElement> <DIV>)
🔧 lodash-template/html-closing-bracket-newline require or disallow a line break before tag's closing brackets
🔧 lodash-template/html-closing-bracket-spacing require or disallow a space before tag's closing brackets. (ex. 🆗 <input> <input·/> 🆖 <input·> <input/>)
🔧 lodash-template/html-comment-content-newline require or disallow a line break before and after HTML comment contents
🔧 lodash-template/html-comment-spacing enforce unified spacing in HTML comment. (ex. 🆗 <!-- comment -->, 🆖 <!--comment-->)
🔧 lodash-template/html-content-newline require or disallow a line break before and after HTML contents
🔧 lodash-template/html-indent enforce consistent HTML indentation.
🔧 lodash-template/max-attributes-per-line enforce the maximum number of HTML attributes per line
lodash-template/no-duplicate-attributes disallow duplication of HTML attributes. (ex. 🆖 <div foo foo>)
lodash-template/no-html-comments disallow HTML comments. (ex. 🆖 <!-- comment -->)
🔧 lodash-template/no-multi-spaces-in-html-tag disallow multiple spaces in HTML tags. (ex. 🆖 <input···type="text">)
🔧 lodash-template/no-space-attribute-equal-sign disallow spacing around equal signs in attribute. (ex. 🆗 <div class="item"> 🆖 <div class = "item">)
lodash-template/no-warning-html-comments disallow specified warning terms in HTML comments. (ex. 🆖 <!-- TODO:task -->)

Uncategorized

Rule ID Description
lodash-template/no-template-tag-in-start-tag disallow template tag in start tag outside attribute values. (ex. 🆖 <input <%= 'disabled' %> >)
lodash-template/prefer-escape-template-interpolations prefer escape micro-template interpolations. (ex. 🆗 <%- ... %>, 🆖 <%= ... %>)

Deprecated

  • ⚠️ We're going to remove deprecated rules in the next major release. Please migrate to successor/new rules.
  • 😇 We don't fix bugs which are in deprecated rules since we don't have enough resources.
Rule ID Replaced by
lodash-template/no-multi-spaces-in-script lodash-template/no-multi-spaces-in-scriptlet
lodash-template/plugin-option (no replacement)
lodash-template/script-indent lodash-template/scriptlet-indent

Plugin Option

Suppress no-undef warnings in the template tag

Please set the global variable used in all templates as follows.

.eslintrc.*:

  {
      "settings": {
+         "lodash-template/globals": ["variableName"]
      }
  }

Please write the global comment in the file as follows for the variable to be used with a specific template.

+ <% /* global users */ %>
  <% for ( var i = 0; i < users.length; i++ ) { %>
    <li><a href="<%= users[i].url %>"><%= users[i].name %></a></li>
  <% } %>

Suppress reports for specific rules in template files

Please set as follows.

.eslintrc.*:

  {
      "settings": {
+         "lodash-template/ignoreRules": ["no-undef", "no-tabs"]
      }
  }

The ESLint standard suppression method can also be used by using template tag, as follows.

+ <% /* eslint no-ternary: 0 */ %>

Customize parser

For example, if you set Lodash templateSettings as follows,

_.templateSettings = {
    evaluate:    /{{([\s\S]+?)}}/g,
    interpolate: /{{=([\s\S]+?)}}/g,
    escape:      /{{-([\s\S]+?)}}/g
};

please set parserOptions(ex. .eslintrc.*) as follows.

      parserOptions: {
+         templateSettings: {
+             evaluate:    ["{{", "}}"],
+             interpolate: ["{{=", "}}"],
+             escape:      ["{{-", "}}"],
+         },
      },

For example, to parse like EJS, set as follows,

(If plugin:lodash-template/*** is set in extends, it is automatically applied to the extension .ejs.)

      parserOptions: {
+         templateSettings: {
+             evaluate:    [ ["<%", "<%_"], ["%>", "-%>", "_%>"] ],
+             interpolate: [  "<%-",        ["%>", "-%>", "_%>"] ],
+             escape:      [  "<%=",        ["%>", "-%>", "_%>"] ],
+             comment:     [  "<%#",        ["%>", "-%>", "_%>"] ],
+             literal:     [  "<%%" ],
+         },
      },

(This plugin do not provide complete support for EJS. e.g. the include directive.)

Customize target extensions

Please set .eslintrc.* as follows.

(For example, for EJS.)

+    "overrides": [
+        {
+            "files": ["*.ejs"],
+            "processor": "lodash-template/html"
+        }
+    ]

For JavaScript (TypeScript) Templates

(This is an experimental feature. Also check for known limitations.)

For example if you have a file like below.

/* eslint no-multi-spaces: error */
<% /* eslint lodash-template/no-multi-spaces-in-scriptlet: error */ %>

// if this plugin is not used, a parsing error will occur.
const obj    = <%= JSON.stringify(options     ) %>
//       ^^^^                            ^^^^^ 
//         |                              |
//         |          If you don't use `"plugin:lodash-template/recommended-with-script"`,
//         |          only the space after `options` is reported.
//         |
//         + When using `"plugin:lodash-template/recommended-with-script"`, the space after `obj` is also reported.

Playground on the Web

Configuring

Please set .eslintrc.* as follows.

+    "overrides": [
+        {
+            "files": ["**/your/templates/*.js"],
+            "extends": ["plugin:lodash-template/recommended-with-script"]
+        }
+    ]

If you do not want to use the included rules, set the details as follows.

    "overrides": [
        {
            "files": ["**/your/templates/*.js"],
-            "extends": ["plugin:lodash-template/recommended-with-script"],
+            "extends": ["plugin:lodash-template/base"],
+            "processor": "lodash-template/script",
+            "rules": {
+                "lodash-template/no-invalid-template-interpolation": "error"
+                ...
+            }
        }
    ]

If you want to use it with TypeScript, you need to configure parserOptions.

    "overrides": [
        {
-            "files": ["**/your/templates/*.js"],
+            "files": ["**/your/templates/*.ts"],
+            "parserOptions": {
+                 "parser": "@typescript-eslint/parser",
+                 "sourceType": "module"
+            },
            "extends": ["plugin:lodash-template/recommended-with-script"]
        }
    ]

FAQ

Known Limitations in Script Templates

Due to known limitations in script templates, you may need to rewrite some templates. Otherwise, you may not be able to use this plugin or some rules.

Parsing Error

Interpolation in the script template will try to replace it with an identifier and parse it. If you generate a complex script in interpolation, you may get a parsing error.

👍 The following script can be parsed well.

let <%= idName %> = 42;
export { <%= idName %> };

👎 The following script cannot be parsed well.

<%= 'let ' + idName %> = 42;
export { <%= idName %> };

False Positives in Some Rules

If you use branching in your template, the plugin will generate multiple script ASTs needed to cover all branches. (Then merge the results of validating these ASTs.)
This can confuse some rules and cause false positives.

However, this is necessary to avoid script parsing errors.

e.g.

Template:

const a = 'foo'
<% if (x) { %>
  const b = 1;
<% } else { %>
  const b = 2;
<% } %>

Generated Script 1:

const a = 'foo'

  const b = 1;

Generated Script 2:

const a = 'foo'



  const b = 2;

If we use the following script, it is a parsing error.

const a = 'foo'

  const b = 1;

  const b = 2; // <- Identifier 'b' has already been declared

The plugin also tries to generate scripts using branches that are as consistent as possible.

e.g.

Template:

<% if (x.foo) { %>
  const a = 'x.foo is true'
<% } %>
// ...
<% if (x.foo) { %>
  console.log(a)
<% } else { %>
  // process for x.foo is false
<% } %>

Generated Script 1:

  const a = 'x.foo is true'

// ...

  console.log(a)

Generated Script 2:

// ...



  // process for x.foo is false

However, branching conditions are compared using text, so even logically the same can be confusing.

e.g.

Template:

<% if (x['foo']) { %>
  const a = 'x.foo is true'
<% } %>
// ...
<% if (x.foo) { %>
  console.log(a)
<% } else { %>
  // process for x.foo is false
<% } %>

Generated Script 1:

  const a = 'x.foo is true'

// ...

  console.log(a)

Generated Script 2:

  const a = 'x.foo is true'

// ...



  // process for x.foo is false

This template gets an error 'a' is assigned a value but never used. from the no-unused-vars rule.

Editor Settings with HTML templates

About how to mark warnings on editor.

  • VSCode (VS Code ESLint extension)

    settings.json:

    {
        "eslint.validate": [ "javascript", "javascriptreact", "html" ]
    }
  • Sublime Text3 (SublimeLinter-eslint)

    [Preference] > [Package Settings] > [SublimeLinter] > [Settings]

    // SublimeLinter Settings - User
    {
        "linters": {
            "eslint": {
                "selector": "text.html, source.js - meta.attribute-with-value"
            }
        }
    }

Migrations

Contributing

Welcome contributing!

Please use GitHub's Issues/PRs.

parserServices

Information provided by parserServices on this plugin

Development Tools

  • npm test runs tests and measures coverage.
  • npm run update runs in order to update readme and recommended configuration.

License

See the LICENSE file for license rights and limitations (MIT).

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].