Page MenuHomePhabricator

Replace vars with const and let
Open, NormalPublic


const and let have sane scoping behavior and the latter allows for read-only assignments. var does not and must go.

Replace all var usages with const or, if necessary, let. Consider documenting, automating, or semi-automating the task to resolve merge conflicts due to lingering code review and helping other repos, such as MinervaNeue, migrate in the future. prefer-const has an autofixer and may help the effort. (Just an example:)

"rules": {
  "no-var": 2,
  "prefer-const": 1,
  "one-var": [1, "never"],
  "init-declarations": 1
// eslint --cache --max-warnings 0 --report-unused-disable-directives --fix tests src

one-var is a useful lint to avoid hoisting issues. However, these issues are not present with const, let, or ES6 functions and greatly deteriorate readability. As part of this task, disable one-var as it is for Popups and limit the scope of variables as much as practical.

Acceptance criteria

  • All occurrences of var are replaced in src, tests, and even webpack.config.js.
  • Using let or var where const can be used should cause the linter to fail.
  • Any changes to ESLint config should be attempted in eslint-config-wikimedia.
  • No regressions to production or tests.