1.0.0 • Published 6 years ago

handlebars-lint v1.0.0

Weekly downloads
375
License
MIT
Repository
github
Last release
6 years ago

handlebars-lint NPM version NPM monthly downloads NPM total downloads Linux Build Status

Lint for missing variables, helpers, block helpers or partials.

Please consider following this project's author, Jon Schlinkert, and consider starring the project to show your :heart: and support.

Install

Install with npm:

$ npm install --save handlebars-lint

Usage

lint

Pass a context and a string with handlebars templates and lint for missing variables, helpers, block helpers or partials.

Params

  • str {String}: The string to lint.
  • options {Object}: Pass a context on options.context or your own instance of handlebars on options.hbs.
  • returns {Object}

Example

const lint = require('handlebars-lint');
lint(string, options);

Example

Assuming we lint the following string:

Title: {{upper foo}}
Hi, my name is {{proper name}}

With the following context:

const missing = lint(str, {
  context: {
    helpers: {
      upper: function() {},
      proper: function() {},
    },
    variables: {
      name: 'Brian'
    }
  }
});
console.log(missing);
//=> { variables: [ 'foo' ] }

Extended example

Assuming we lint the following string:

{{upper "bar"}}
{{upper varname}}
{{baz}}
{{zzz name=name}}

More content.

{{abc (sub blah)}}

{{> onetwo (three blah)}}
{{> four five}}

{{#markdown foo}}
> A markdown blockquote
{{> six (seven eight)}}
{{lower whatever}}
{{/markdown}}

The end.

The following:

var missing = lint(str, {
  context: {
    helpers: {
      upper: function() {}
    },
    variables: {
      name: 'Brian'
    }
  }
});
console.log(missing);

Results in:

{ 
  helpers: [ 'upper', 'baz', 'zzz', 'abc', 'sub', 'three', 'seven', 'lower' ],
  variables: [ 'varname', 'name', 'blah', 'five', 'eight', 'whatever', 'foo' ],
  partials: [ 'onetwo', 'four', 'six' ],
  blockHelpers: [ 'markdown' ] 
}

Heads up!

The linter assumes that an expression is a variable when:

  1. The expression has no params or hash arguments, as in {{foo}}, versus {{foo bar}} or {{foo bar=baz}}
  2. There is no variable or helper on the context to distinguish otherwise.

About

Pull requests and stars are always welcome. For bugs and feature requests, please create an issue.

Running and reviewing unit tests is a great way to get familiarized with a library and its API. You can install dependencies and run tests with the following command:

$ npm install && npm test

(This project's readme.md is generated by verb, please don't edit the readme directly. Any changes to the readme must be made in the .verb.md readme template.)

To generate the readme, run the following command:

$ npm install -g verbose/verb#dev verb-generate-readme && verb

Related projects

You might also be interested in these projects:

Author

Jon Schlinkert

License

Copyright © 2018, Jon Schlinkert. Released under the MIT License.


This file was generated by verb-generate-readme, v0.6.0, on April 27, 2018.