ember-template-lint
Lint your templates.
Last updated 21 days ago by rwjblue .
MIT · Repository · Bugs · Original npm · Tarball · package.json
$ cnpm install ember-template-lint 
SYNC missed versions from official npm registry.

ember-template-lint

npm version Build Status

ember-template-lint will lint your template and return error results.

For example, given the rule no-bare-strings is enabled, this template would be in violation:

{{!-- app/components/my-thing/template.hbs  --}}
<div>A bare string</div>

When ran through the linter's verify method, we would have a single result indicating that the no-bare-strings rule found an error.

Install

This addon is installed by default with new Ember apps, so check your package.json before installing to see if you need to install it.

To install ember-template-lint

npm install --save-dev ember-template-lint

Node.js 10 || >=12 is required.

Usage

Direct usage

Run templates through the linter's verify method like so:

var TemplateLinter = require('ember-template-lint');

var linter = new TemplateLinter();
var template = fs.readFileSync('some/path/to/template.hbs', { encoding: 'utf8' });
var results = linter.verify({ source: template, moduleId: 'template.hbs' });

results will be an array of objects which have the following properties:

  • rule - The name of the rule that triggered this warning/error.
  • message - The message that should be output.
  • line - The line on which the error occurred.
  • column - The column on which the error occurred.
  • moduleId - The module path for the file containing the error.
  • source - The source that caused the error.
  • fix - An object describing how to fix the error.

CLI executable

Basic ember-template-lint executable is provided, allowing for easy use within i.e. Git pre-commit/push hooks and development of appropriate plugins for text editors.

Ensure you wrap all glob patterns in quotes so that it won't be interpreted by the CLI. ./node_modules/.bin/ember-template-lint app/templates/** (this will expand all paths in app/templates) and ./node_modules/.bin/ember-template-lint "app/templates/**" (this will pass the glob to ember-template-lint and not interpret the glob).

Example usage:

# basic usage
./node_modules/.bin/ember-template-lint "app/templates/application.hbs"

# output errors with source description
./node_modules/.bin/ember-template-lint "app/templates/application.hbs" --verbose

# multiple file/directory/wildcard paths are accepted
./node_modules/.bin/ember-template-lint "app/templates/components/**/*" "app/templates/application.hbs"

# output errors as pretty-printed JSON string
./node_modules/.bin/ember-template-lint "app/templates/application.hbs" --json

# ignore warnings / only report errors
./node_modules/.bin/ember-template-lint "app/templates/application.hbs" --quiet

# define custom config path
./node_modules/.bin/ember-template-lint "app/templates/application.hbs" --config-path .my-template-lintrc.js

# read from stdin
./node_modules/.bin/ember-template-lint --filename app/templates/application.hbs < app/templates/application.hbs

# print list of formated rules for use with `pending` in config file
./node_modules/.bin/ember-template-lint "app/templates/application.hbs" --print-pending

# specify custom ignore pattern `['**/dist/**', '**/tmp/**', '**/node_modules/**']` by default
./node_modules/.bin/ember-template-lint "/tmp/template.hbs" --ignore-pattern "**/foo/**" --ignore-pattern "**/bar/**"

# disable ignore pattern entirely
./node_modules/.bin/ember-template-lint "/tmp/template.hbs" --no-ignore-pattern

# running a single rule without options
./node_modules/.bin/ember-template-lint --no-config-path app/templates --rule 'no-implicit-this:error'

# running a single rule with options
./node_modules/.bin/ember-template-lint --no-config-path app/templates --rule 'no-implicit-this:["error", { "allow": ["some-helper"] }]'

# specify a config object to use instead of what exists locally
./node_modules/.bin/ember-template-lint --config '{ "rules": { "no-implicit-this": { "severity": 2, "config": true } } }' test/fixtures/no-implicit-this-allow-with-regexp/app/templates

ESLint

If you are using templates inlined into your JS files, you can leverage eslint-plugin-hbs to integrate ember-template-lint into your normal eslint workflow.

Configuration

Presets

Name Description
:white_check_mark: recommended enables the recommended rules
:car: octane extends the recommended preset by enabling Ember Octane rules
:dress: stylistic enables stylistic rules for those who aren't ready to adopt ember-template-lint-plugin-prettier (including stylistic rules that were previously in the recommended preset in ember-template-lint v1)

Project Wide

You can turn on specific rules by toggling them in a .template-lintrc.js file at the base of your project, or at a custom relative path which may be identified using the CLI:

module.exports = {
  extends: 'recommended',

  rules: {
    'no-bare-strings': true
  }
}

This extends from the builtin recommended configuration (lib/config/recommended.js), and also enables the no-bare-strings rule (see here).

Using this mechanism allows you to extend from the builtin, and modify specific rules as needed.

Some rules also allow setting additional configuration, for example if you would like to configure some "bare strings" that are allowed you might have:

module.exports = {
  rules: {
    'no-bare-strings': ['ZOMG THIS IS ALLOWED!!!!']
  }
};

Configuration Keys

The following properties are allowed in the root of the .template-lintrc.js configuration file:

  • rules -- Object This is an object containing rule specific configuration (see details for each rule below).
  • extends -- string|string[] Either a string or an array of strings. Each string allows you to specify an internally curated list of rules (we suggest recommended here).
  • pending -- string[] An array of module id's that are still pending. The goal of this array is to allow incorporating template linting into an existing project, without changing every single template file. You can add all existing templates to this pending listing and slowly work through them, while at the same time ensuring that new templates added to the project pass all defined rules.
    • You can generate this list with the: ./node_modules/.bin/ember-template-lint * --print-pending
  • ignore -- string[]|glob[] An array of module id's that are to be completely ignored. See ignore documentation for more details.
  • plugins -- (string|Object)[] An array of plugin objects, or strings that resolve to files that export plugin objects. See plugin documentation for more details.
  • overrides -- Array An array of overrides that would allow overriding of specific rules for user specified files/folders. See overrides documentation for more details.

Rules

Each rule can have its own severity level which can be a string or could be the first element of the array that contains the custom rule configuration. Supported severity levels are off, warn, error. You can define a severity level directly on the rule: Eg: 'no-bare-strings': 'warn' OR If your rule has a custom configuration, and you want to define the severity level you would need to add the severity as the first element of the array. Eg:

{
   "no-implicit-this": ['warn', { "allow": [ "fooData" ] }
}

Current list of rules and deprecations can be found in docs/rules.md.

Per Template File

It is also possible to disable specific rules (or all rules) in a template itself:

<!-- disable all rules -->
{{!-- template-lint-disable  --}}

<!-- disable no-bare-strings -->
{{!-- template-lint-disable no-bare-strings  --}}

<!-- disable no-bare-strings and no-triple-curlies -->
{{!-- template-lint-disable no-bare-strings no-triple-curlies  --}}

<!-- enable all rules -->
{{!-- template-lint-enable  --}}

<!-- enable no-bare-strings -->
{{!-- template-lint-enable no-bare-strings  --}}

<!-- enable no-bare-strings and no-triple-curlies -->
{{!-- template-lint-enable no-bare-strings no-triple-curlies  --}}

and to configure rules in the template:

{{!-- template-lint-configure no-bare-strings ["ZOMG THIS IS ALLOWED!!!!"]  --}}

{{!-- template-lint-configure no-bare-strings {"whitelist": "(),.", "globalAttributes": ["title"]}  --}}

{{!-- template-lint-configure no-bare-strings ["warn", ["ZOMG THIS IS ALLOWED!!!!"]]  --}}

{{!-- template-lint-configure no-bare-strings "warn"  --}}

The configure instruction can only configure a single rule, and the configuration value must be valid JSON that parses into a configuration for that rule.

These configuration instructions do not modify the rule for the rest of the template, but instead modify it within whatever DOM scope the comment instruction appears.

An instruction will apply to all later siblings and their descendants:

<!-- disable for <p> and <span> and their contents, but not for <div> or <hr> -->
<div>
  <hr>
  {{!-- template-lint-disable  --}}
  <p>
    <span>Hello!</span>
  </p>
</div>

An in-element instruction will apply to only that element:

<!-- enable for <p>, but not for <div>, <hr> or <span> -->
<div>
  <hr>
  <p {{!-- template-lint-enable  --}}>
    <span>Hello!</span>
  </p>
</div>

An in-element instruction with the -tree suffix will apply to that element and all its descendants:

<!-- configure for <p>, <span> and their contents, but not for <div> or <hr> -->
<div>
  <hr>
  <p {{!-- template-lint-configure-tree block-indentation "tab"  --}}>
    <span>Hello!</span>
  </p>
</div>

Note that enabling a rule ({{!-- template-lint-enable --}}) that has been configured in-template ({{!-- template-lint-configure --}}), will restore it to its default configuration rather than the modified in-template configuration for the scope of the {{!-- template-lint-enable --}} instruction.

Defining your own rules

You can define and use your own custom rules using the plugin system. See plugin documentation for more details.

Sharing configs

It is possible to share a config (extends) or plugin (custom rules) across projects. See ember-template-lint-plugin-peopleconnect for an example.

Semantic Versioning Policy

The semver policy for this addon can be read here: semver policy.

Contributing

A few ideas for where to take this in the future:

  • The list of rules should be configurable
  • This addon should use a test printer shared with jshint, eslint and jscs addons
  • A command-line version of the linter should be provided so IDEs and editors can provide feedback to devs during development

See the Contributing Guidelines for information on how to help out.

Current Tags

  • 2.0.0-beta.5                                ...           beta (5 months ago)
  • 2.7.0                                ...           latest (21 days ago)
  • 1.0.0-beta.6                                ...           next (2 years ago)

106 Versions

  • 2.7.0                                ...           21 days ago
  • 2.6.0                                ...           a month ago
  • 2.5.2                                ...           2 months ago
  • 2.5.1                                ...           2 months ago
  • 2.5.0                                ...           2 months ago
  • 2.4.1                                ...           2 months ago
  • 2.4.0                                ...           3 months ago
  • 2.3.0                                ...           3 months ago
  • 2.2.0                                ...           3 months ago
  • 2.1.0                                ...           3 months ago
  • 2.0.1                                ...           3 months ago
  • 2.0.0                                ...           3 months ago
  • 1.14.0                                ...           3 months ago
  • 1.13.2                                ...           4 months ago
  • 2.0.0-beta.5                                ...           5 months ago
  • 1.13.1                                ...           5 months ago
  • 1.13.0                                ...           5 months ago
  • 1.12.3                                ...           5 months ago
  • 1.12.2                                ...           5 months ago
  • 1.12.1                                ...           6 months ago
  • 2.0.0-beta.4                                ...           6 months ago
  • 1.12.0                                ...           6 months ago
  • 1.11.1                                ...           6 months ago
  • 1.11.0                                ...           6 months ago
  • 1.10.0                                ...           6 months ago
  • 2.0.0-beta.3                                ...           6 months ago
  • 1.9.0                                ...           6 months ago
  • 2.0.0-beta.2                                ...           7 months ago
  • 1.8.2                                ...           7 months ago
  • 1.8.1                                ...           7 months ago
  • 2.0.0-beta.1                                ...           7 months ago
  • 1.8.0                                ...           7 months ago
  • 1.7.0                                ...           7 months ago
  • 1.6.2                                ...           7 months ago
  • 1.6.1                                ...           7 months ago
  • 1.6.0                                ...           7 months ago
  • 1.5.3                                ...           9 months ago
  • 1.5.2                                ...           10 months ago
  • 1.5.1                                ...           10 months ago
  • 1.5.0                                ...           10 months ago
  • 1.4.0                                ...           a year ago
  • 1.3.0                                ...           a year ago
  • 1.2.0                                ...           a year ago
  • 1.1.0                                ...           a year ago
  • 1.0.0                                ...           a year ago
  • 1.0.0-beta.6                                ...           2 years ago
  • 1.0.0-beta.5                                ...           2 years ago
  • 1.0.0-beta.4                                ...           2 years ago
  • 1.0.0-beta.3                                ...           2 years ago
  • 1.0.0-beta.2                                ...           2 years ago
  • 1.0.0-beta.1                                ...           2 years ago
  • 0.8.23                                ...           2 years ago
  • 0.8.22                                ...           2 years ago
  • 0.8.21                                ...           2 years ago
  • 0.8.20                                ...           2 years ago
  • 0.8.19                                ...           2 years ago
  • 0.8.18                                ...           2 years ago
  • 0.8.17                                ...           2 years ago
  • 0.8.16                                ...           2 years ago
  • 0.8.15                                ...           2 years ago
  • 0.8.14                                ...           2 years ago
  • 0.8.13                                ...           3 years ago
  • 0.8.12                                ...           3 years ago
  • 0.8.11                                ...           3 years ago
  • 0.8.10                                ...           3 years ago
  • 0.8.9                                ...           3 years ago
  • 0.8.8                                ...           3 years ago
  • 0.8.7                                ...           3 years ago
  • 0.8.6                                ...           3 years ago
  • 0.8.5                                ...           3 years ago
  • 0.8.4                                ...           3 years ago
  • 0.8.3                                ...           3 years ago
  • 0.8.2                                ...           3 years ago
  • 0.8.1                                ...           3 years ago
  • 0.8.0                                ...           3 years ago
  • 0.7.4                                ...           3 years ago
  • 0.7.3                                ...           3 years ago
  • 0.7.2                                ...           3 years ago
  • 0.7.1                                ...           3 years ago
  • 0.7.0                                ...           3 years ago
  • 0.7.0-beta.3                                ...           3 years ago
  • 0.6.4                                ...           3 years ago
  • 0.6.3                                ...           4 years ago
  • 0.6.2                                ...           4 years ago
  • 0.6.1                                ...           4 years ago
  • 0.6.0                                ...           4 years ago
  • 0.5.18                                ...           4 years ago
  • 0.5.17                                ...           4 years ago
  • 0.5.16                                ...           4 years ago
  • 0.5.15                                ...           4 years ago
  • 0.5.14                                ...           4 years ago
  • 0.5.13                                ...           4 years ago
  • 0.5.12                                ...           4 years ago
  • 0.5.11                                ...           4 years ago
  • 0.5.10                                ...           4 years ago
  • 0.5.9                                ...           4 years ago
  • 0.5.8                                ...           4 years ago
  • 0.5.7                                ...           4 years ago
  • 0.5.6                                ...           4 years ago
  • 0.5.5                                ...           4 years ago
  • 0.5.4                                ...           4 years ago
  • 0.5.3                                ...           4 years ago
  • 0.5.2                                ...           4 years ago
  • 0.5.1                                ...           4 years ago
  • 0.5.0                                ...           4 years ago
  • 0.5.0-alpha.1                                ...           4 years ago

Copyright 2014 - 2016 © taobao.org |