Class StringJavaScriptCheck

java.lang.Object
com.puppycrawl.tools.checkstyle.AbstractAutomaticBean
com.puppycrawl.tools.checkstyle.api.AbstractViolationReporter
com.puppycrawl.tools.checkstyle.api.AbstractCheck
com.vaadin.checkstyle.StringJavaScriptCheck
All Implemented Interfaces:
com.puppycrawl.tools.checkstyle.api.Configurable, com.puppycrawl.tools.checkstyle.api.Contextualizable

public class StringJavaScriptCheck extends com.puppycrawl.tools.checkstyle.api.AbstractCheck
Checkstyle rule that flags calls sending JavaScript to the browser as a string, which the browser compiles and a Content Security Policy without unsafe-eval blocks. Such JavaScript has to be declared in a @JsDefinition interface instead.

The rule only runs when its cspMode property is strict. The property comes from the csp.mode Maven property of the module, so that modules are switched one by one while they are migrated. It checks main sources only: tests and integration test modules are left out.

Only calls that send a string whatever its value are flagged:

  • executeJs(...) whose first argument is not a class literal
  • JsFunction.of(...) and addDynamicImport(...)
  • LitRenderer.of(...) with a string
  • addEventData(...) whose argument is neither a class literal nor a declared DomEventData, and every addEventDataElement(...). Their string overloads are deprecated: a property path is added with addEventDataPath or addEventDataElementPath, and any other expression is a declared DomEventData.
  • setFilter(...) on a DOM listener registration, with any argument but null. A filter is declared as a DomEventFilter and set with setFilterExpression instead.
@EventData annotations are not checked: Flow puts their values into the bundle.

The rule has no type information. A name is resolved to its declaration in the same file, following the scopes of Java: a local variable or parameter hides a field, and a declaration in one method does not apply to another. this.name, Outer.this.name and Outer.name are resolved to the fields of the class. A name that is not declared in the file, such as an inherited field, is unknown.

For LitRenderer.of, a string is recognized as a literal, a concatenation, or a name declared as String. For addEventData, the argument is a declared DomEventData when it is a name declared as DomEventData, or a method call on JsSnippets.of(...) or on a name initialized with it. Anything else is flagged, so a DomEventData from another class is kept in a DomEventData variable first. As other APIs, such as data views, also have a setFilter method, it is only flagged on a DOM listener registration: a call chain through addEventListener(...), or a name declared as DomListenerRegistration or initialized with such a chain.

Code that has to keep sending a string, such as a deprecated public API, is marked with @SuppressWarnings("checkstyle:StringJavaScriptCheck").

  • Nested Class Summary

    Nested classes/interfaces inherited from class com.puppycrawl.tools.checkstyle.AbstractAutomaticBean

    com.puppycrawl.tools.checkstyle.AbstractAutomaticBean.OutputStreamOptions
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    static final String
    Message key for event data added as a string.
    static final String
    Message key for an event filter set as a string.
    static final String
    Message key for a call that sends JavaScript as a string.
  • Constructor Summary

    Constructors
    Constructor
    Description
     
  • Method Summary

    Modifier and Type
    Method
    Description
    void
    beginTree(com.puppycrawl.tools.checkstyle.api.DetailAST rootAst)
     
    void
    finishTree(com.puppycrawl.tools.checkstyle.api.DetailAST rootAst)
     
    int[]
     
    int[]
     
    int[]
     
    void
    setCspMode(String cspMode)
    Sets the CSP mode of the module.
    void
    visitToken(com.puppycrawl.tools.checkstyle.api.DetailAST ast)
     

    Methods inherited from class com.puppycrawl.tools.checkstyle.api.AbstractCheck

    clearViolations, destroy, getFileContents, getFilePath, getLine, getLineCodePoints, getLines, getTabWidth, getTokenNames, getViolations, init, isCommentNodesRequired, leaveToken, log, log, log, setFileContents, setTabWidth, setTokens

    Methods inherited from class com.puppycrawl.tools.checkstyle.api.AbstractViolationReporter

    finishLocalSetup, getCustomMessages, getId, getMessageBundle, getSeverity, getSeverityLevel, setId, setSeverity

    Methods inherited from class com.puppycrawl.tools.checkstyle.AbstractAutomaticBean

    configure, contextualize, getConfiguration, setupChild

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Field Details

    • MSG_KEY

      public static final String MSG_KEY
      Message key for a call that sends JavaScript as a string.
      See Also:
    • EVENT_DATA_MSG_KEY

      public static final String EVENT_DATA_MSG_KEY
      Message key for event data added as a string.
      See Also:
    • FILTER_MSG_KEY

      public static final String FILTER_MSG_KEY
      Message key for an event filter set as a string.
      See Also:
  • Constructor Details

    • StringJavaScriptCheck

      public StringJavaScriptCheck()
  • Method Details

    • setCspMode

      public void setCspMode(String cspMode)
      Sets the CSP mode of the module. The rule only runs for strict.
      Parameters:
      cspMode - the CSP mode, off, warn or strict
    • getDefaultTokens

      public int[] getDefaultTokens()
      Specified by:
      getDefaultTokens in class com.puppycrawl.tools.checkstyle.api.AbstractCheck
    • getAcceptableTokens

      public int[] getAcceptableTokens()
      Specified by:
      getAcceptableTokens in class com.puppycrawl.tools.checkstyle.api.AbstractCheck
    • getRequiredTokens

      public int[] getRequiredTokens()
      Specified by:
      getRequiredTokens in class com.puppycrawl.tools.checkstyle.api.AbstractCheck
    • beginTree

      public void beginTree(com.puppycrawl.tools.checkstyle.api.DetailAST rootAst)
      Overrides:
      beginTree in class com.puppycrawl.tools.checkstyle.api.AbstractCheck
    • visitToken

      public void visitToken(com.puppycrawl.tools.checkstyle.api.DetailAST ast)
      Overrides:
      visitToken in class com.puppycrawl.tools.checkstyle.api.AbstractCheck
    • finishTree

      public void finishTree(com.puppycrawl.tools.checkstyle.api.DetailAST rootAst)
      Overrides:
      finishTree in class com.puppycrawl.tools.checkstyle.api.AbstractCheck