Class StringJavaScriptCheck
- All Implemented Interfaces:
com.puppycrawl.tools.checkstyle.api.Configurable,com.puppycrawl.tools.checkstyle.api.Contextualizable
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 literalJsFunction.of(...)andaddDynamicImport(...)LitRenderer.of(...)with a stringaddEventData(...)whose argument is neither a class literal nor a declaredDomEventData, and everyaddEventDataElement(...). Their string overloads are deprecated: a property path is added withaddEventDataPathoraddEventDataElementPath, and any other expression is a declaredDomEventData.setFilter(...)on a DOM listener registration, with any argument butnull. A filter is declared as aDomEventFilterand set withsetFilterExpressioninstead.
@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 -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionvoidbeginTree(com.puppycrawl.tools.checkstyle.api.DetailAST rootAst) voidfinishTree(com.puppycrawl.tools.checkstyle.api.DetailAST rootAst) int[]int[]int[]voidsetCspMode(String cspMode) Sets the CSP mode of the module.voidvisitToken(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, setTokensMethods inherited from class com.puppycrawl.tools.checkstyle.api.AbstractViolationReporter
finishLocalSetup, getCustomMessages, getId, getMessageBundle, getSeverity, getSeverityLevel, setId, setSeverityMethods inherited from class com.puppycrawl.tools.checkstyle.AbstractAutomaticBean
configure, contextualize, getConfiguration, setupChild
-
Field Details
-
MSG_KEY
Message key for a call that sends JavaScript as a string.- See Also:
-
EVENT_DATA_MSG_KEY
Message key for event data added as a string.- See Also:
-
FILTER_MSG_KEY
Message key for an event filter set as a string.- See Also:
-
-
Constructor Details
-
StringJavaScriptCheck
public StringJavaScriptCheck()
-
-
Method Details
-
setCspMode
Sets the CSP mode of the module. The rule only runs forstrict.- Parameters:
cspMode- the CSP mode,off,warnorstrict
-
getDefaultTokens
public int[] getDefaultTokens()- Specified by:
getDefaultTokensin classcom.puppycrawl.tools.checkstyle.api.AbstractCheck
-
getAcceptableTokens
public int[] getAcceptableTokens()- Specified by:
getAcceptableTokensin classcom.puppycrawl.tools.checkstyle.api.AbstractCheck
-
getRequiredTokens
public int[] getRequiredTokens()- Specified by:
getRequiredTokensin classcom.puppycrawl.tools.checkstyle.api.AbstractCheck
-
beginTree
public void beginTree(com.puppycrawl.tools.checkstyle.api.DetailAST rootAst) - Overrides:
beginTreein classcom.puppycrawl.tools.checkstyle.api.AbstractCheck
-
visitToken
public void visitToken(com.puppycrawl.tools.checkstyle.api.DetailAST ast) - Overrides:
visitTokenin classcom.puppycrawl.tools.checkstyle.api.AbstractCheck
-
finishTree
public void finishTree(com.puppycrawl.tools.checkstyle.api.DetailAST rootAst) - Overrides:
finishTreein classcom.puppycrawl.tools.checkstyle.api.AbstractCheck
-