Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

docs(README.md): include HardCodedString linter #278

Merged
84 changes: 64 additions & 20 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -90,26 +90,28 @@ linters:

## Linters

| Available Linters | Default | Description |
| ------------------------------------------------ |:--------:|-------------|
| [AllowedScriptType](#AllowedScriptType) | Yes | prevents the addition of `<script>` tags that have `type` attributes that are not in a white-list of allowed values |
| ClosingErbTagIndent | Yes | |
| ExtraNewline | Yes | |
| [FinalNewline](#FinalNewline) | Yes | warns about missing newline at the end of a ERB template |
| [NoJavascriptTagHelper](#NoJavascriptTagHelper) | Yes | prevents the usage of Rails' `javascript_tag` |
| ParserErrors | Yes | |
| PartialInstanceVariable | No | detects instance variables in partials |
| [RequireInputAutocomplete](#RequireInputAutocomplete) | Yes | warns about missing autocomplete attributes in input tags |
| [RightTrim](#RightTrim) | Yes | enforces trimming at the right of an ERB tag |
| [SelfClosingTag](#SelfClosingTag) | Yes | enforces self closing tag styles for void elements |
| [SpaceAroundErbTag](#SpaceAroundErbTag) | Yes | enforces a single space after `<%` and before `%>`|
| SpaceIndentation | Yes | |
| SpaceInHtmlTag | Yes | |
| TrailingWhitespace | Yes | |
| [DeprecatedClasses](#DeprecatedClasses) | No | warns about deprecated css classes |
| [ErbSafety](#ErbSafety) | No | detects unsafe interpolation of ruby data into various javascript contexts and enforce usage of safe helpers like `.to_json`. |
| [Rubocop](#Rubocop) | No | runs RuboCop rules on ruby statements found in ERB templates |
| [RequireScriptNonce](#RequireScriptNonce) | No | warns about missing [Content Security Policy nonces](https://guides.rubyonrails.org/security.html#content-security-policy) in script tags |
| Available Linters | Default | Description |
|-------------------------------------------------------|:-------:|-------------------------------------------------------------------------------------------------------------------------------------------|
| [AllowedScriptType](#AllowedScriptType) | Yes | prevents the addition of `<script>` tags that have `type` attributes that are not in a white-list of allowed values |
| ClosingErbTagIndent | Yes | |
| ExtraNewline | Yes | |
| [FinalNewline](#FinalNewline) | Yes | warns about missing newline at the end of a ERB template |
| [NoJavascriptTagHelper](#NoJavascriptTagHelper) | Yes | prevents the usage of Rails' `javascript_tag` |
| ParserErrors | Yes | |
| PartialInstanceVariable | No | detects instance variables in partials |
| [RequireInputAutocomplete](#RequireInputAutocomplete) | Yes | warns about missing autocomplete attributes in input tags |
| [RightTrim](#RightTrim) | Yes | enforces trimming at the right of an ERB tag |
| [SelfClosingTag](#SelfClosingTag) | Yes | enforces self closing tag styles for void elements |
| [SpaceAroundErbTag](#SpaceAroundErbTag) | Yes | enforces a single space after `<%` and before `%>` |
| SpaceIndentation | Yes | |
| SpaceInHtmlTag | Yes | |
| TrailingWhitespace | Yes | |
| [DeprecatedClasses](#DeprecatedClasses) | No | warns about deprecated css classes |
| [ErbSafety](#ErbSafety) | No | detects unsafe interpolation of ruby data into various javascript contexts and enforce usage of safe helpers like `.to_json`. |
| [Rubocop](#Rubocop) | No | runs RuboCop rules on ruby statements found in ERB templates |
| [RequireScriptNonce](#RequireScriptNonce) | No | warns about missing [Content Security Policy nonces](https://guides.rubyonrails.org/security.html#content-security-policy) in script tags |
| [HardCodedString](#HardCodedString) | No | warns if there is a visible hardcoded string in the DOM (does not check for a hardcoded string nested inside a JavaScript tag) |
francisfuzz marked this conversation as resolved.
Show resolved Hide resolved


### DeprecatedClasses

Expand Down Expand Up @@ -487,6 +489,48 @@ Linter-Specific Option | Description
`allow_blank` | True or false, depending on whether or not the `type` attribute may be omitted entirely from a `<script>` tag. Defaults to `true`.
`disallow_inline_scripts` | Do not allow inline `<script>` tags anywhere in ERB templates. Defaults to `false`.

### HardCodedString

`HardCodedStrings` warns if there is a visible hardcoded string in the DOM. It does not check for a hardcoded string nested inside a JavaScript tag.

Example configuration:

```yaml
---
linters:
HardCodedString
enabled: true
corrector:
path: path/to/corrector_file.rb
name: I18nCorrector
i18n_load_path: path/to/translator_file.rb
```

This linter requires a `corrector` option. Without a `corrector` option, the strings won't be translated.
c
francisfuzz marked this conversation as resolved.
Show resolved Hide resolved
Linter-Specific Option | Description
--------------------------|---------------------------------------------------------
path | a string pointing to the path to the corrector file
name | the name of the corrector class (can [either be `I18nCorrector` or `Rubocop::I18nCorrector`](https://github.com/Shopify/erb-lint/blob/19c9ddb94f0ea1d73ac12e18a7ea822d76adeeab/lib/erb_lint/linters/hard_coded_string.rb#L17))
i18n_load_path | a string pointing to the path of the file(s) to be translated

Below is an example corrector file. For your project, the actual details of the `autocorrect` method are left up to how you want to correct those offenses.

```ruby
class I18nCorrector
attr_reader :node

def initialize(node, filename, i18n_load_path, range)
end

def autocorrect(tag_start:, tag_end:)
->(corrector) do
node
end
end
end
```

## Custom Linters

`erb-lint` allows you to create custom linters specific to your project. It will load linters from the `.erb-linters` directory in the root of your
Expand Down