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

support Class Fields #14343

Closed
8 tasks done
aladdin-add opened this issue Apr 20, 2021 · 12 comments · Fixed by #14886
Closed
8 tasks done

support Class Fields #14343

aladdin-add opened this issue Apr 20, 2021 · 12 comments · Fixed by #14886
Assignees
Labels
archived due to age This issue has been archived; please open a new issue for any further discussion core Relates to ESLint's core APIs and features enhancement This change enhances an existing feature of ESLint new syntax This issue is related to new syntax that has reached stage 4

Comments

@aladdin-add
Copy link
Member

aladdin-add commented Apr 20, 2021

The version of ESLint you are using.
latest

The problem you want to solve.
support class fields officially, as it has reached stage-4.
refs: https://github.com/tc39/proposals/blob/master/finished-proposals.md

Your take on the correct solution to problem.

@aladdin-add aladdin-add added enhancement This change enhances an existing feature of ESLint core Relates to ESLint's core APIs and features new syntax This issue is related to new syntax that has reached stage 4 labels Apr 20, 2021
@eslint-github-bot eslint-github-bot bot added this to Needs Triage in Triage Apr 20, 2021
@nzakas nzakas moved this from Needs Triage to Evaluating in Triage Apr 21, 2021
@nzakas
Copy link
Member

nzakas commented Apr 21, 2021

@aladdin-add can you investigate to see which rules need to be updated and list them in this issue?

@aladdin-add
Copy link
Member Author

that's exactly what I want to do later. 😄

@rhinonan
Copy link

indent ?

Class Foo {
  private foo1 = 1
    private foo2 = 2 // eslint indent rule not given any errors
}

@aladdin-add aladdin-add self-assigned this Apr 21, 2021
@mysticatea mysticatea pinned this issue Apr 22, 2021
@mysticatea mysticatea self-assigned this Apr 22, 2021
@ota-meshi
Copy link
Member

Does ESLint need to upgrade eslint-utils?
It may not be necessary as it seems to use only some features of eslint-utils.
I would like to help eslint-utils. mysticatea/eslint-utils#20

mysticatea added a commit to eslint/eslint-scope that referenced this issue Apr 28, 2021
@nzakas nzakas added this to Planned in Public Roadmap via automation May 4, 2021
@nzakas nzakas moved this from Planned to In Progress in Public Roadmap May 4, 2021
@nzakas
Copy link
Member

nzakas commented May 4, 2021

Published new version of eslint-visitor-keys

@mysticatea mysticatea moved this from Evaluating to Pull Request Opened in Triage May 16, 2021
@nzakas nzakas added this to Pull Request Opened in v8.0.0 May 20, 2021
@nzakas nzakas moved this from Pull Request Opened to Planned in v8.0.0 May 20, 2021
@mysticatea mysticatea moved this from Planned to Pull Request Opened in v8.0.0 May 22, 2021
nzakas added a commit to eslint/espree that referenced this issue Jun 11, 2021
* Update: add ES2022 class features support (refs eslint/eslint#14343)

* update README.md

* update tools and add docs for adding new syntax

* add dist

* upgrade eslint-visitor-keys

* Revert "add dist"

This reverts commit 3e9caa7.

* add npm prepare script

* Update CONTRIBUTING.md

Co-authored-by: 薛定谔的猫 <weiran.zsd@outlook.com>

* update CONTRIBUTING.md

* fix for JSON incompatible values

* Apply suggestions from code review

Co-authored-by: 薛定谔的猫 <weiran.zsd@outlook.com>
Co-authored-by: Nicholas C. Zakas <nicholas@nczconsulting.com>
nzakas added a commit to eslint/eslint-scope that referenced this issue Jul 2, 2021
* Update: support class fields (refs eslint/eslint#14343)

* upgrade eslint-visitor-keys

* update espree

* make function-scope for class field initializers

* Update package.json

* Update package.json

* Update tests/class-fields.js

* Update tests/class-fields.js

Co-authored-by: Brandon Mills <btmills@users.noreply.github.com>

* Remove trailing whitespace to fix lint failure

* introduce class-field-initializer scope

* fix typo

* fix variableScope

Co-authored-by: Nicholas C. Zakas <nicholas@nczconsulting.com>
Co-authored-by: Brandon Mills <btmills@users.noreply.github.com>
@nzakas
Copy link
Member

nzakas commented Jul 23, 2021

Published new version of eslint-scope.

@nzakas nzakas moved this from Pull Request Opened to Ready for Merge in v8.0.0 Jul 29, 2021
nzakas added a commit that referenced this issue Aug 5, 2021
…#14591)

* update package.json (temporary)

* update ast-utils

- `getFunctionNameWithKind(node)` ... supports class fields and private
  identifier. And now it uses property names rather than function names
  if named function expressions are methods because the property name
  is exposed.
- `getFunctionHeadLoc(node)` ... supports class fields. And now returns
  the range of property names instead of the arrow locations if arrow
  functions are at method places.
- `isSameReference(l,r)` ... supports `PrivateIdentifier`.

* update camelcase

This commit includes a large refactoring.
Previously, the `Identifier` node listener handled all cases by
checking parent node types. But because the `Identifier` node has so
broad meanings, it's confusing about what kind of nodes it's handling.

Now it uses variables and references of `eslint-scope`. And it checks
properties, re-exported identifiers, and labels by detailed esqueries.
The property check newly supports class fields and private identifiers.

This fixes #13021 as well.

* update accessor-pairs (test-only)

* update class-methods-use-this

* update computed-property-spacing

* update dot-location (test-only)

* update dot-notation (test-only)

* update func-names

Function expressions at field initializers have inferred names.
Therefore the `as-needed` option should not report anonymous functions
at field initializers.

* update getter-return (test-only)

* update grouped-accessor-pairs (test-only)

* update indent

* update keyword-spacing

* update lines-between-class-members (test-only)

* update no-dupe-class-members

* update no-extra-semi

* update no-invalid-this

* update no-multi-assign

* update no-proto (test-only)

* update no-prototype-builtins (test-only)

* update no-restricted-properties (test-only)

* update no-self-assign

* update no-self-compare (test-only)

* update no-setter-return

* update no-shadow (test-only)

* update no-this-before-super (test-only)

* update no-throw-literal (test-only)

* update no-undef-init

* update no-underscore-dangle

* update no-unexpected-multiline (test-only)

* update no-unreachable

* update no-useless-call (test-only)

* update no-useless-computed-key

* update no-eval

* update operator-assignment (test-only)

* update operator-linebreak

* update padded-blocks (test-only)

* update prefer-exponentiation-operator

* update prefer-numeric-literals (test-only)

* update prefer-object-spread (test-only)

* update prefer-promise-reject-errors (test-only)

* update prefer-regex-literals (test-only)

* update prefer-spread (test-only)

* update quotes

* update radix (test-only)

* update require-atomic-updates (test-only)

* update require-unicode-regexp (test-only)

* update semi-spacing

* update semi-style

* update semi

* update space-infix-ops

* update strict (test-only)

* add more tests to no-unexpected-multiline

* fix some tests for 7345747

* fix no-invalid-this

* fix no-eval

* Update eslint-scope

* Upgrade Espree

* Fix eslint-scope references to parser test

* Fix: id-denylist

* Update comments

* Fix: id-match

* Fix: id-length

* Update: id-denylist for class fields

* Update: id-length

* Update: id-denylist code and docs

* Docs: id-denylist

* Update: id-match

Co-authored-by: Nicholas C. Zakas <nicholas@humanwhocodes.com>
@nzakas nzakas moved this from Ready for Merge to Planned in v8.0.0 Aug 5, 2021
@nzakas nzakas moved this from Planned to Pull Request Opened in v8.0.0 Aug 6, 2021
@nzakas nzakas linked a pull request Aug 6, 2021 that will close this issue
1 task
@nzakas nzakas moved this from Pull Request Opened to Ready for Merge in v8.0.0 Aug 26, 2021
Public Roadmap automation moved this from In Progress to Complete Aug 26, 2021
Triage automation moved this from Pull Request Opened to Complete Aug 26, 2021
v8.0.0 automation moved this from Ready for Merge to Done Aug 26, 2021
mdjermanovic added a commit that referenced this issue Aug 26, 2021
* Fix: Code path analysis for class fields (fixes #14343)

* PropertyDefinition code paths

* Add test and comments

* Reverse order of code path closing

* Added CodePath#origin

* Update tests/lib/linter/code-path-analysis/code-path.js

Co-authored-by: Milos Djermanovic <milos.djermanovic@gmail.com>

* Update lib/linter/code-path-analysis/code-path.js

Co-authored-by: Milos Djermanovic <milos.djermanovic@gmail.com>

Co-authored-by: Milos Djermanovic <milos.djermanovic@gmail.com>
@nzakas nzakas unpinned this issue Sep 2, 2021
@f0o
Copy link

f0o commented Oct 21, 2021

Class foo {
  #x
}

still yields error Parsing error: Unexpected character '#' on eslint 8.0.1 - Did I miss something?

@minimit
Copy link

minimit commented Oct 21, 2021

Class foo {
  #x
}

still yields error Parsing error: Unexpected character '#' on eslint 8.0.1 - Did I miss something?

Be sure to have this setting in .eslintjs.rc.

parserOptions: {
    ecmaVersion: 13,

@f0o
Copy link

f0o commented Oct 21, 2021

@minimit That's the one! Thanks!

@NinjaCross
Copy link

NinjaCross commented Dec 10, 2021

I have this same error running eslint 8.4.1 using gulp-eslint 7.21.1

"C:\Program Files\nodejs\node.exe" J:\MyProject\MyProject.WebSite\node_modules\gulp\bin\gulp.js --color --gulpfile J:\MyProject\MyProject.WebSite\gulpFile.js run-eslint
[19:28:51] Using gulpfile J:\MyProject\MyProject.WebSite\gulpFile.js
[19:28:51] Starting 'run-eslint'...
START Es Lint
[19:28:51] Finished 'run-eslint' after 229 ms
END Es Lint
[19:28:52] no problems
[19:28:52]
J:\MyProject\MyProject.WebSite\wwwroot\js\MyEventListener.js
  7:5  error  Parsing error: Unexpected character '#'

✖ 1 problem (1 error, 0 warnings)

package.json

{
  "version": "1.0.0",
  "name": "my-website",
  "private": true,
  "engines": {
    "node": ">=17"
  },
  "scripts": {
    
  },
  "devDependencies": {
    "fs": "^0.0.2",
    "glob": "^7.2.0",
    "gulp": "^4.0.2",
    "gulp-cli": "^2.3.0",
    "gulp-clean": "^0.4.0",
    "sass": "^1.44.0",
    "eslint": "^8.4.1",
    "gulp-eslint": "^6.0.0"
  },
  "dependencies": {

  }
}

.eslintrc.json

{
	"extends": "eslint:recommended",
	"globals": {},
	"env": {
		"browser": true,
		"commonjs": true,
		"es6": true,
		"jquery": true
	},
	"parserOptions":{
		"ecmaVersion": 2020  <--- I tried with multiple values (11, 13, 2018, 2020), but no one of them solved the problem
	},
	"rules": {
		"no-prototype-builtins": "off",
		"no-unused-vars": "off",
		"no-undef": "off"
	}
}

gulpFile.js

/// <binding AfterBuild='build-scss'/>
const gulp = require('gulp');
const clean = require('gulp-clean');
const glob = require("glob");
const fs = require("fs");
const eslint = require('gulp-eslint');

gulp.task("run-eslint", callback => {
    const options = {
        realpath: true,
        nodir: false
    };

    console.log('START Es Lint');

    glob("wwwroot/js/*.js", options, async (er, files) => {
        if (er) {
            console.error(er);
            callback(er);
        } else {
            for (const f of files) {
                if (!f.endsWith('.min.js')) {
                    runEsLint(f);
                }
            }
        }
        callback();
        console.log('END Es Lint');
    });
});

function runEsLint(filePath) {
    gulp.src(filePath). // get the target file
        pipe(eslint()). // attaches the lint output to the "eslint" property of the file object so it can be used by other modules.
        pipe(eslint.format()). // outputs the lint results to the console
        pipe(eslint.format('visualstudio')). // outputs the lint results formatted for Visual Studio
        pipe(eslint.failAfterError()) // To have the process exit with an error code (1) on lint error, return the stream and pipe to failAfterError last.
    ;
}

Any idea ?

@JLHwung
Copy link
Contributor

JLHwung commented Dec 10, 2021

@NinjaCross gulp-eslint pins eslint to v6, see adametry/gulp-eslint#250

@NinjaCross
Copy link

@NinjaCross gulp-eslint pins eslint to v6, see adametry/gulp-eslint#250

I see. Thank you, I wasn't aware of this.
I solved changing the parser.
Now with Babel it seems to work ok.

@eslint-github-bot eslint-github-bot bot locked and limited conversation to collaborators Feb 23, 2022
@eslint-github-bot eslint-github-bot bot added the archived due to age This issue has been archived; please open a new issue for any further discussion label Feb 23, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
archived due to age This issue has been archived; please open a new issue for any further discussion core Relates to ESLint's core APIs and features enhancement This change enhances an existing feature of ESLint new syntax This issue is related to new syntax that has reached stage 4
Projects
Public Roadmap
  
Complete
Archived in project
v8.0.0
  
Done
Triage
Complete
Development

Successfully merging a pull request may close this issue.

9 participants