Skip to content

Linter: erb-no-unsafe-script-interpolation docs is wrong and interferes with erb-no-unsafe-raw #1569

@t27duck

Description

@t27duck

erb-no-unsafe-script-interpolation says to not use escape_javascript or j() in erb in script tags.

The docs say to do something like this:

<script>
  var name = <%= user.name.to_json %>;
</script>

This actual breaks the resulting Javascript as to_json is not flagged as safe by Rails by default resulting in:

<script>
  var name = &quot;Someone&quot;;
</script>

To unbreak it, you have to call html_safe on it:

<script>
  var name = <%= user.name.to_json.html_safe %>;
</script>

However, this interferes with erb-no-unsafe-raw.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions