Skip to content
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

JS: Hoist function declarations to the top of a block statement #18661

Merged
merged 12 commits into from
Feb 6, 2025

Conversation

asgerf
Copy link
Contributor

@asgerf asgerf commented Feb 3, 2025

Fixes the issue reported in #18652

We did not hoist function declarations inside block statements, as this is not standard behaviour, but it is non-standard behaviour implemented by most engines.

The specification discusses this in B.3.2 Block-Level Function Declarations Web Legacy Compatibility Semantics though I must admit I haven't read the compatibility semantics in detail.

This PR changes it to hoist function declarations to the top of a block statement in all cases.

@github-actions github-actions bot added the JS label Feb 3, 2025
'buildFunctionBody' no longer needs to handle hoisting, because hoisting now happens when visiting the block statement that is the body of the function.

Note that curly-brace functions contain a block statement as their body, not a list of statements.
The extra successor edge was due to visiting hoisted function declaration IDs multiple times,
which has now been fixed.
@asgerf asgerf marked this pull request as ready for review February 6, 2025 09:01
@Copilot Copilot bot review requested due to automatic review settings February 6, 2025 09:01
@asgerf asgerf requested a review from a team as a code owner February 6, 2025 09:01
Copy link
Contributor

@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

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

PR Overview

This PR introduces block-level function declaration hoisting in JavaScript code to align with non-standard but common engine behavior, while removing the prior function-level hoisting method. It also bumps the extractor version to track the extractor changes.

  • Updated the JavaScript extractor version in Main.java
  • Removed function-level hoisting logic for IFunction bodies and replaced it with block-level hoisting logic
  • Added comments referencing non-standard engine-specific behavior

Changes

File Description
Main.java Updated the extractor version from 2025-01-21 to 2025-02-03.
CFGExtractor.java Removed function-level hoisting method and introduced block-level hoisting logic.

Copilot reviewed 72 out of 72 changed files in this pull request and generated 1 comment.

Comments suppressed due to low confidence (1)

javascript/extractor/src/com/semmle/js/extractor/CFGExtractor.java:589

  • Removing the function-level hoisting method may be a breaking change if external code relies on it. Consider providing an alternative or ensuring it's no longer needed.
-    public static List<Identifier> of(IFunction fn) { ... }

Tip: If you use Visual Studio Code, you can request a review from Copilot before you push from the "Source Control" tab. Learn more

@asgerf asgerf merged commit 7f4facc into github:main Feb 6, 2025
9 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants