Skip to content

Latest commit

 

History

History
412 lines (303 loc) · 8.55 KB

README.md

File metadata and controls

412 lines (303 loc) · 8.55 KB

Linters

Below is a list of linters supported by slim-lint, ordered alphabetically.

CommentControlStatement

Reports control statements with only comments.

Bad

-# This is a control statement comment

Good

/ This is a Slim comment

Slim comments should be preferred as they do not result in any generated Ruby code and are optimized out of the parse tree during compilation.

ConsecutiveControlStatements

Option Description
max_consecutive Maximum number of control statements that can appear in a row

Reports the appearance of multiple consecutive control statements.

Bad

- some_code
- some_more_code
- do_you_really_need_this_much_code?

Better

ruby:
  some_code
  some_more_code
  do_you_really_need_this_much_code?

Best

- helper_that_does_all_of_the_above

Large blocks of code in templates make them difficult to read and are usually a smell. It is best to extract these into separate helpers whenever possible.

ControlStatementSpacing

Reports missing or superfluous spacing before and after control statements

Bad

div= some_code

Good

div = some_code

EmbeddedEngines

Reports forbidden embedded engines if listed.

Option Description
forbidden_engines List of forbidden embedded engines. (default [])
linters:
  EmbeddedEngines:
    enabled: true
    forbidden_engines:
      - javascript

Bad for above configuration

p Something

javascript:
  alert('foo')

EmptyControlStatement

Reports control statements with no code.

Bad

p Something
-
p Something else

Good

p Something
p Something else

EmptyLines

Reports two or more consecutive blank lines.

Bad

p Something


p Something else

Good

p Something

p Something else

FileLength

Option Description
max Maximum number of lines a single file can have. (default 300)

You can configure this amount via the max option on the linter, e.g. by adding the following to your .slim-lint.yml:

linters:
  FileLength:
    max: 100

Long files are harder to read and usually indicative of complexity.

InstanceVariables

Reports instance variables in Slim templates. Use the include configuration option to narrow down the files to e.g. only partial view templates in Rails:

linters:
  InstanceVariables:
    enabled: true
    include:
      - app/views/**/_*.html.slim

Bad for the above configuration

In app/views/somewhere/_partial.html.slim:

= @hello

Good for the above configuration

In app/views/somewhere/show.html.slim:

= render 'partial', hello: @hello

In app/views/somewhere/_partial.html.slim:

= hello

The linter allows ensuring only local variables and/or helper methods are used in the configured set of Slim templates. This is often encouraged in Rails partial templates.

LineLength

Option Description
max Maximum number of columns a single line can have. (default 80)

Wrap lines at 80 characters. You can configure this amount via the max option on the linter, e.g. by adding the following to your .slim-lint.yml:

linters:
  LineLength:
    max: 100

Long lines are harder to read and usually indicative of complexity.

RedundantDiv

Reports explicit uses of div when it would otherwise be implicit.

Bad: div is unnecessary when class/ID is specified

div.button

Good: div is required when no class/ID is specified

div

Good

.button

Slim was designed to be concise, and not embracing this makes the tool less useful.

RuboCop

Option Description
ignored_cops Array of RuboCop cops to ignore.

This linter integrates with RuboCop (a static code analyzer and style enforcer) to check the actual Ruby code in your templates. It will respect any RuboCop-specific configuration you have set in .rubocop.yml files, but will explicitly ignore some checks (like Style/IndentationWidth) since the extracted Ruby code sent to RuboCop is not well-formatted.

- name = 'James Brown'
- unused_variable = 42

p Hello #{name}!

Output from slim-lint

example.slim:2 [W] Useless assignment to variable - unused_variable

You can customize which RuboCop warnings you want to ignore by modifying the ignored_cops option (see config/default.yml for the full list of ignored cops). Note that if you modify the list you'll need to re-include all the items from the default configuration.

You can also explicitly set which RuboCop configuration to use via the SLIM_LINT_RUBOCOP_CONF environment variable. This is intended to be used by external tools which run the linter on files in temporary directories separate from the directory where the Slim template originally resided (and thus where the normal .rubocop.yml would be picked up).

Displaying Cop Names

You can display the name of the cop by adding the following to your .rubocop.yml configuration:

AllCops:
  DisplayCopNames: true

StrictLocalsMissing

Reports on missing strict locals magic comment in Slim templates. Use the include configuration option to narrow down the files to e.g. only partial view templates in Rails:

linters:
  StrictLocalsMissing:
    enabled: true
    include:
      - app/views/**/_*.html.slim

Bad for the above configuration

In app/views/somewhere/_partial.html.slim:

= some_helper(foo, bar)

Good for the above configuration

In app/views/somewhere/_partial.html.slim:

/# locals: (foo:, bar: 'default')
= some_helper(foo, bar)

By default, Rails partial templates accept any local variables. Strict locals, on the other hand, help define an explicit interface for the template that shows which local variables it accepts.

Tab

Reports detection of tabs used for indentation.

Tag

Reports forbidden tag if listed.

Option Description
forbidden_tags List of forbidden tags. (default [])
linters:
  Tag:
    enabled: true
    forbidden_tags:
      - p

Bad for above configuration

p Something
P Something else

TagAttribute

Reports forbidden tag attribute if listed.

Option Description
forbidden_attributes List of forbidden tag attributes. (default [])
linters:
  TagAttribute:
    enabled: true
    forbidden_attributes:
      - style

Bad for above configuration

p style="{ color: red; }" Something
P STYLE="{ color: blue; }" Something else

TagCase

Reports tag names with uppercase characters.

Bad

BODY
  P My paragraph

Good

body
  p My paragraph

While the HTML standard does not require lowercase tag names, they are a de facto standard and are used in almost all documentation and specifications available online. However, lowercase tags are required for XHTML documents, so using them consistently results in more portable code.

TrailingBlankLines

Reports trailing blank lines.

TrailingWhitespace

Reports trailing whitespace (spaces or tabs) on any lines in a Slim document.

Zwsp

Reports it contains ZWSP(zero width space).

Bad

| Hello Zwsp\u200b

Good

| Hello without zwsp