Skip to content

Commit

Permalink
Merge pull request #16161 from RasmusWL/js/strict-mode
Browse files Browse the repository at this point in the history
JS: Parser: Never run in strict mode
  • Loading branch information
asgerf authored Apr 17, 2024
2 parents 7434a58 + ed80e4e commit f78ea26
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 37 deletions.
25 changes: 13 additions & 12 deletions javascript/extractor/src/com/semmle/jcorn/Parser.java
Original file line number Diff line number Diff line change
Expand Up @@ -244,7 +244,13 @@ public Parser(Options options, String input, int startPos) {
this.exprAllowed = true;

// Figure out if it's a module code.
this.strict = this.inModule = options.sourceType().equals("module");
this.inModule = options.sourceType().equals("module");

// We don't care to report syntax errors in code that might be using strict mode. In
// the end, we don't know whether that code is put through additional build steps
// causing our alleged syntax errors to disappear. Therefore, we hardcode
// this.strict to false.
this.strict = false;

// Used to signify the start of a potential arrow function
this.potentialArrowAt = -1;
Expand Down Expand Up @@ -323,18 +329,13 @@ protected void next() {
this.nextToken();
}

// Toggle strict mode. Re-reads the next number or string to please
// pedantic tests (`"use strict"; 010;` should fail).
// DEPRECATED. When we respected strict mode, this method was used to toggle strict
// mode (and would re-read the next number or string to please pedantic tests (`"use
// strict"; 010;` should fail)).

public void setStrict(boolean strict) {
this.strict = strict;
if (this.type != TokenType.num && this.type != TokenType.string) return;
this.pos = this.start;
while (this.pos < this.lineStart) {
this.lineStart = this.input.lastIndexOf("\n", this.lineStart - 2) + 1;
--this.curLine;
}
this.nextToken();
// always false
return;
}

public TokContext curContext() {
Expand Down Expand Up @@ -3107,7 +3108,7 @@ protected BlockStatement parseBlock(boolean allowStrict) {
if (stmt != null) body.add(stmt);
if (first && allowStrict && this.isUseStrict(stmt)) {
oldStrict = this.strict;
this.setStrict(this.strict = true);
this.setStrict(true);
}
first = false;
}
Expand Down
2 changes: 1 addition & 1 deletion javascript/extractor/src/com/semmle/js/extractor/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public class Main {
* A version identifier that should be updated every time the extractor changes in such a way that
* it may produce different tuples for the same file under the same {@link ExtractorConfig}.
*/
public static final String EXTRACTOR_VERSION = "2023-10-13";
public static final String EXTRACTOR_VERSION = "2024-04-17";

public static final Pattern NEWLINE = Pattern.compile("\n");

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -196,29 +196,5 @@ successor(#20045,#20048)
successor(#20057,#20041)
successor(#20038,#20037)
successor(#20054,#20038)
#20059=*
js_parse_errors(#20059,#20001,"Error: Assigning to arguments in strict mode"," arguments = 42;
")
#20060=@"loc,{#10000},3,3,3,3"
locations_default(#20060,#10000,3,3,3,3)
hasLocation(#20059,#20060)
#20061=*
lines(#20061,#20001,"function f() {","
")
hasLocation(#20061,#20003)
#20062=*
lines(#20062,#20001," 'use strict';","
")
hasLocation(#20062,#20005)
indentation(#10000,2," ",2)
#20063=*
lines(#20063,#20001," arguments = 42;","
")
hasLocation(#20063,#20007)
indentation(#10000,3," ",2)
#20064=*
lines(#20064,#20001,"}","")
hasLocation(#20064,#20009)
numlines(#20001,4,0,0)
numlines(#10000,4,4,0)
filetype(#10000,"javascript")
7 changes: 7 additions & 0 deletions javascript/ql/src/change-notes/2024-04-17-strict-mode.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
category: minorAnalysis
---
* The JavaScript extractor will on longer report syntax errors related to "strict mode".
Files containing such errors are now being fully analyzed along with other sources files.
This improves our support for source files that technically break the "strict mode" rules,
but where a build steps transforms the code such that it ends up working at runtime.

0 comments on commit f78ea26

Please sign in to comment.