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

fix(useStrictMode): strict directive insertion logic #4901

Open
wants to merge 2 commits into
base: next
Choose a base branch
from

Conversation

bushuai
Copy link
Contributor

@bushuai bushuai commented Jan 16, 2025

Summary

closes #4841

  • improve the rule logic for cjs files with shebang and comments
    • will not report for files containing only a shebang/comment or both in cjs files (aligning the behavior with eslint) playground and source
  • add test cases

Example

comment only

  • before
//·comment"use·strict";//·comment    <<<--- duplicated comment and no line break here
  • after(keep it as is)
// comment

shebang only

  • before
#! /usr/bin/env node"use strict";
  • after(keep it as is)
#! /usr/bin/env node

with shebang

  • before
#!·/usr/bin·node"use·strict";
let some_variable = "some value";
  • after
#! /usr/bin node
"use strict";
let some_variable = "some value";

with comment

  • before
// comment
"use strict";// comment    <<<--- duplicated comment and no line break here
let some_variable = "some value";
  • after
"use strict";
// comment
let some_variable = "some value";

with shebang and comment

  • before
#! /usr/bin node"use strict";
// comment
let some_variable = "some value";
  • after
#! /usr/bin node
"use strict";
// comment
let some_variable = "some value";

Test Plan

  • added more test cases
  • updated the snapshots

- enhance handling of leading and trailing trivia for "use strict"
directive
- add more robust checks for script directives and interpreter tokens
- ensure proper newline placement when inserting strict mode directive
- fix the mdn web docs link in diagnostics
@github-actions github-actions bot added A-Linter Area: linter L-JavaScript Language: JavaScript and super languages labels Jan 16, 2025
Copy link

codspeed-hq bot commented Jan 16, 2025

CodSpeed Performance Report

Merging #4901 will improve performances by 7.3%

Comparing bushuai:fix/use-strict-mode (3accd60) with next (4f07cc8)

Summary

⚡ 1 improvements
✅ 94 untouched benchmarks

Benchmarks breakdown

Benchmark next bushuai:fix/use-strict-mode Change
react.production.min_3378072959512366797.js[cached] 2 ms 1.9 ms +7.3%

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-Linter Area: linter L-JavaScript Language: JavaScript and super languages
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant