-
Notifications
You must be signed in to change notification settings - Fork 1.7k
JS: Promote js/template-syntax-in-string-literal
to the Code Quality suite.
#19726
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
Changes from 5 commits
92084dd
861e4ee
bafe7e6
923aff2
75ee649
da5cd25
d7ad625
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,7 +5,10 @@ | |
* @problem.severity warning | ||
* @id js/template-syntax-in-string-literal | ||
* @precision high | ||
* @tags correctness | ||
* @tags quality | ||
* reliability | ||
* correctness | ||
* language-features | ||
*/ | ||
|
||
import javascript | ||
|
@@ -73,9 +76,10 @@ class CandidateStringLiteral extends StringLiteral { | |
* ``` | ||
*/ | ||
predicate hasObjectProvidingTemplateVariables(CandidateStringLiteral lit) { | ||
exists(DataFlow::CallNode call, DataFlow::ObjectLiteralNode obj | | ||
call.getAnArgument().getALocalSource() = obj and | ||
call.getAnArgument().asExpr() = lit and | ||
exists(DataFlow::CallNode call, DataFlow::ObjectLiteralNode obj, DataFlow::Node stringArg | | ||
stringArg = [StringConcatenation::getRoot(lit.flow()), lit.flow()] and | ||
stringArg = call.getAnArgument() and | ||
Napalys marked this conversation as resolved.
Show resolved
Hide resolved
|
||
obj.flowsTo(call.getAnArgument()) and | ||
forex(string name | name = lit.getAReferencedVariable() | exists(obj.getAPropertyWrite(name))) | ||
) | ||
} | ||
|
@@ -91,12 +95,36 @@ VarDecl getDeclIn(Variable v, Scope scope, string name, CandidateTopLevel tl) { | |
result.getTopLevel() = tl | ||
} | ||
|
||
/** | ||
* Tracks data flow from a string literal that may flow to a replace operation. | ||
*/ | ||
DataFlow::SourceNode trackString(CandidateStringLiteral lit, DataFlow::TypeTracker t) { | ||
t.start() and result = lit.flow() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Tracking all string literals in the program seems unnecessarily expensive. Could you restrict this to just the string literals that we might have flagged otherwise? Adding There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Added here d7ad625. |
||
or | ||
exists(DataFlow::TypeTracker t2 | result = trackString(lit, t2).track(t2, t)) | ||
} | ||
|
||
/** | ||
* Gets a string literal that flows to a replace operation. | ||
*/ | ||
DataFlow::SourceNode trackString(CandidateStringLiteral lit) { | ||
result = trackString(lit, DataFlow::TypeTracker::end()) | ||
} | ||
|
||
/** | ||
* Holds if the string literal flows to a replace method call. | ||
*/ | ||
predicate hasReplaceMethodCall(CandidateStringLiteral lit) { | ||
trackString(lit).getAMethodCall() instanceof StringReplaceCall | ||
} | ||
|
||
from CandidateStringLiteral lit, Variable v, Scope s, string name, VarDecl decl | ||
where | ||
decl = getDeclIn(v, s, name, lit.getTopLevel()) and | ||
lit.getAReferencedVariable() = name and | ||
lit.isInScope(s) and | ||
not hasObjectProvidingTemplateVariables(lit) and | ||
not lit.getStringValue() = "${" + name + "}" | ||
not lit.getStringValue() = "${" + name + "}" and | ||
not hasReplaceMethodCall(lit) | ||
select lit, "This string is not a template literal, but appears to reference the variable $@.", | ||
decl, v.getName() |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
--- | ||
category: minorAnalysis | ||
--- | ||
* Fixed false positives in the `js/template-syntax-in-string-literal` query where template syntax in string concatenation and "manual string interpolation" patterns were incorrectly flagged. |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
--- | ||
category: queryMetadata | ||
--- | ||
* Added `reliability` and `language-features` tags to the `js/template-syntax-in-string-literal` query. |
Uh oh!
There was an error while loading. Please reload this page.