diff --git a/javascript/extractor/src/com/semmle/jcorn/Parser.java b/javascript/extractor/src/com/semmle/jcorn/Parser.java index e12ab864d10d..53d71c92d506 100644 --- a/javascript/extractor/src/com/semmle/jcorn/Parser.java +++ b/javascript/extractor/src/com/semmle/jcorn/Parser.java @@ -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; @@ -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() { @@ -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; } diff --git a/javascript/extractor/src/com/semmle/js/extractor/Main.java b/javascript/extractor/src/com/semmle/js/extractor/Main.java index 5b4b6a2494eb..5b618c550edf 100644 --- a/javascript/extractor/src/com/semmle/js/extractor/Main.java +++ b/javascript/extractor/src/com/semmle/js/extractor/Main.java @@ -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"); diff --git a/javascript/extractor/tests/strictmode/output/trap/assignargs.js.trap b/javascript/extractor/tests/strictmode/output/trap/assignargs.js.trap index 8b34d3f5ad66..efc24fe55e5c 100644 --- a/javascript/extractor/tests/strictmode/output/trap/assignargs.js.trap +++ b/javascript/extractor/tests/strictmode/output/trap/assignargs.js.trap @@ -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") diff --git a/javascript/ql/src/change-notes/2024-04-17-strict-mode.md b/javascript/ql/src/change-notes/2024-04-17-strict-mode.md new file mode 100644 index 000000000000..65dd10d45eb3 --- /dev/null +++ b/javascript/ql/src/change-notes/2024-04-17-strict-mode.md @@ -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.