Lint your Haml files in VS Code

Created: April 05, 2019 13:56 | Updated: June 01, 2019 14:12
Tags: Haml, Ruby, Visual Studio Code

Next to ERB, haml can be a bit overwhelmimg. The code is terse and (unlike ERB) indentation really matters.

I didn't realise until recently that linting haml is an option, and want to share my current configuration.

Configure VS Code

Set tab size in your settings.json

  "[haml]": {
    "editor.tabSize": 2,
  },

Add VS Code Extensions

Better Haml

Haml Lint

Install Haml-lint gem

Add the haml-lint gem either manually

gem install haml_lint

or in your gemfile

gem 'haml_lint', require: false

Add the configuration file

Create .haml-lint.yml in the base of your project directory

touch .haml-lint.yml

and add your configuration

# Default application configuration that all configurations inherit from.
#
# This is an opinionated list of which hooks are valuable to run and what their
# out of the box settings should be.

# Whether to ignore frontmatter at the beginning of HAML documents for
# frameworks such as Jekyll/Middleman
skip_frontmatter: false

linters:
  AlignmentTabs:
    enabled: true

  AltText:
    enabled: false

  ClassAttributeWithStaticValue:
    enabled: true

  ClassesBeforeIds:
    enabled: true

  ConsecutiveComments:
    enabled: true
    max_consecutive: 1

  ConsecutiveSilentScripts:
    enabled: true
    max_consecutive: 2

  EmptyObjectReference:
    enabled: true

  EmptyScript:
    enabled: true

  FinalNewline:
    enabled: true
    present: true

  HtmlAttributes:
    enabled: true

  IdNames:
    enabled: true
    style: lisp_case

  ImplicitDiv:
    enabled: true

  Indentation:
    enabled: true
    character: space # or tab
    width: 2 # ignored if character == tab

  InlineStyles:
    enabled: false

  InstanceVariables:
    enabled: true
    file_types: partials
    matchers:
      all: .*
      partials: \A_.*\.haml\z

  LeadingCommentSpace:
    enabled: true

  LineLength:
    enabled: false
    max: 100

  MultilinePipe:
    enabled: true

  MultilineScript:
    enabled: true

  ObjectReferenceAttributes:
    enabled: true

  RepeatedId:
    enabled: true
    severity: error

  RuboCop:
    enabled: true
    # These cops are incredibly noisy when it comes to HAML templates, so we
    # ignore them.
    ignored_cops:
      - Lint/BlockAlignment
      - Lint/EndAlignment
      - Lint/Void
      - Layout/AlignHash
      - Layout/AlignParameters
      - Layout/ElseAlignment
      - Layout/EndOfLine
      - Layout/IndentationWidth
      - Layout/TrailingBlankLines
      - Layout/TrailingWhitespace
      - Metrics/BlockLength
      - Metrics/BlockNesting
      - Metrics/LineLength
      - Naming/FileName
      - Style/FrozenStringLiteralComment
      - Style/IfUnlessModifier
      - Style/Next
      - Style/WhileUntilModifier

  RubyComments:
    enabled: true

  SpaceBeforeScript:
    enabled: true

  SpaceInsideHashAttributes:
    enabled: false
    style: space

  TagName:
    enabled: true

  TrailingWhitespace:
    enabled: true

  UnnecessaryInterpolation:
    enabled: true

  UnnecessaryStringOutput:
    enabled: true

  ViewLength:
    enabled: true
    max: 100

Written by Alan Vardy. Let me know how I can make this better!