Skip to content

Error on misplaced use strict directives - #64041

Open
Ryan Cavanaugh (RyanCavanaugh) with Copilot wants to merge 5 commits into
mainfrom
copilot/make-use-strict-error
Open

Error on misplaced use strict directives#64041
Ryan Cavanaugh (RyanCavanaugh) with Copilot wants to merge 5 commits into
mainfrom
copilot/make-use-strict-error

Conversation

Copilot AI commented Aug 26, 2026

Copy link
Copy Markdown
Contributor

A "use strict" expression outside a function’s directive prologue is ignored by JavaScript, potentially hiding an incorrectly positioned directive.

Changes

  • Report an error when "use strict" appears after executable statements or inside a nested block.
  • Apply the check to function declarations, arrows, generators, methods, and constructors.
  • Allow preceding type-only declarations because they are erased before JavaScript emit.
  • Add compiler coverage for valid and invalid placements.
function example() {
    initialize();
    "use strict"; // Error: directive is not honored here
}

Copilot AI and others added 5 commits August 26, 2026 20:45
Co-authored-by: RyanCavanaugh <6685088+RyanCavanaugh@users.noreply.github.com>
Co-authored-by: RyanCavanaugh <6685088+RyanCavanaugh@users.noreply.github.com>
Co-authored-by: RyanCavanaugh <6685088+RyanCavanaugh@users.noreply.github.com>
Co-authored-by: RyanCavanaugh <6685088+RyanCavanaugh@users.noreply.github.com>
Co-authored-by: RyanCavanaugh <6685088+RyanCavanaugh@users.noreply.github.com>
@RyanCavanaugh

Copy link
Copy Markdown
Member

Just curious what this would do

@RyanCavanaugh

Copy link
Copy Markdown
Member

TypeScript Bot (@typescript-bot) run top1000

@RyanCavanaugh
Ryan Cavanaugh (RyanCavanaugh) marked this pull request as ready for review August 26, 2026 23:18
Copilot AI balanced review requested due to automatic review settings August 26, 2026 23:18
@RyanCavanaugh

Copy link
Copy Markdown
Member

TypeScript Bot (@typescript-bot) test top1000

@typescript-automation

typescript-automation Bot commented Aug 26, 2026

Copy link
Copy Markdown

Starting jobs; this comment will be updated as builds start and complete.

Command Status Results
test top1000 ✅ Started ✅ Results

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Adds compiler diagnostics for misplaced function-level "use strict" directives while permitting erased type declarations.

Changes:

  • Detect misplaced directives across function-like bodies.
  • Add TS18064 and related compiler coverage.
  • Update affected diagnostic and emit baselines.
Show a summary per file
File Description
tsc/internal/checker/checker.go Implements placement validation.
tsc/internal/binder/binder.go Exports strict-directive detection.
tsc/internal/diagnostics/diagnosticMessages.json Defines TS18064.
tsc/internal/diagnostics/diagnostics_generated.go Adds generated diagnostic metadata.
tsc/testdata/tests/cases/compiler/useStrictNotInPrologue.ts Adds placement cases.
tsc/testdata/baselines/reference/compiler/useStrictNotInPrologue.errors.txt Records expected diagnostics.
tsc/testdata/baselines/reference/compiler/useStrictNotInPrologue.js Records emit output.
tsc/testdata/baselines/reference/compiler/useStrictNotInPrologue.symbols Records symbol output.
tsc/testdata/baselines/reference/compiler/useStrictNotInPrologue.types Records type output.
tsc/testdata/baselines/reference/compiler/strictModeInConstructor.errors.txt Updates constructor diagnostics.
tsc/testdata/baselines/reference/conformance/functionWithUseStrictAndSimpleParameterList.errors.txt Updates ES2015 conformance diagnostics.
tsc/testdata/baselines/reference/conformance/functionWithUseStrictAndSimpleParameterList_es2016.errors.txt Updates ES2016 conformance diagnostics.

Review details

💡 Add a code-review agent skill for context-aware, tailored reviews. Learn more in the docs.

Files not reviewed (1)
  • tsc/internal/diagnostics/diagnostics_generated.go: Generated file
  • Files reviewed: 11/12 changed files
  • Comments generated: 1
  • Review effort level: Balanced

Comment on lines +7431 to +7434
function := ast.GetContainingFunction(node)
if function == nil {
return
}
@typescript-automation

Copy link
Copy Markdown

Ryan Cavanaugh (@RyanCavanaugh) Here are the results of running the top 1000 repos with tsc comparing main and refs/pull/64041/merge:

Everything looks good!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants