amilajack / eslint-plugin-compat

Check the browser compatibility of your code
MIT License
3.08k stars 106 forks source link

Dont work with Array.prototype.at #539

Open KODerFunk opened 1 year ago

KODerFunk commented 1 year ago

I have Next.js (13) project, in code i use ['some', 'arrays'].at(-1) and dont has error from this plugin.

Array.prototype.at supports in Safari 15.4: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/at but i have correct .browserlistrc with not safari < 12:

last 2 versions
since 2018
node 17

not dead
not android < 5
not op_mini all
not opera < 64
not ios_saf < 12.2
not safari < 12
not edge < 100
not firefox < 100
not chrome < 80

Project fixed when i add import 'core-js/features/array/at' in pages/_app.tsx.

Plugin added correctly, and have setting in .eslintrc.json:

      "settings": {
        "polyfills": [
          "URL.createObjectURL",
          "URL.revokeObjectURL"
        ]
      },

Added because Next add some (3) polyfills underhood.

koddsson commented 1 year ago

Is this still not working in the latest version (4.1.4)? I think I've fixed it there. If not still working, can you provide a small reproduction case that we can take a look at?

MetRonnie commented 1 year ago

Hmm, I guess this is a little tricky because I guess ESLint doesn't know the types of variables?

const thing = [4, 5, 6]

const a = thing.at(2) // no error
const b = [4, 5, 6].at(2) // correct error

https://github.com/MetRonnie/eslint-plugin-compat-demo/tree/issue-539

koddsson commented 1 year ago

Right! So I think that ESLint doesn't have the type information of the variable using the default parser (using the TypeScript parser we might be able to do that) so as it stands now we can't know that the thing identifier is in fact an Array.

Am I getting that right @amilajack?

antoniosteiger commented 9 months ago

FYI, This is still not working, just tested it with the following code:

let c = [4, 5, 6]; let b = c.findLastIndex();

findLastIndex() is only supported in Chrome 97+, but in my browserlist I have chrome >= 80. Compat doesn't flag this. I am using ESLint's default parser with sourceType module.

mintplo commented 8 months ago

FYI, This is still not working too.

.browserslistrc

Chrome >= 92
Safari >= 15.4
Firefox >= 90
Edge >= 92
[].findLast() ✅ (eslint checked)
people.list.findLast ❌ (eslint no-checked)

CleanShot 2024-03-03 at 11 15 10@2x

tantian1498 commented 7 months ago

正确的!所以我_认为_ESLint 没有使用默认解析器的变量的类型信息(使用 TypeScript 解析器我们也许能够做到这一点),所以现在我们无法知道标识符thing实际上是一个Array.

我说得对吗@amilajack?

Do you have any suggestions to make it work when I use js?

tantian1498 commented 7 months ago

Hmm, I guess this is a little tricky because I guess ESLint doesn't know the types of variables?

const thing = [4, 5, 6]

const a = thing.at(2) // no error
const b = [4, 5, 6].at(2) // correct error

https://github.com/MetRonnie/eslint-plugin-compat-demo/tree/issue-539

i have the same problem, do you solve it now?

koddsson commented 6 months ago

I took a stab at implementing eslint-plugin-compat using the TypeScript parser so that the rule has type information and linting code like has been referenced in this issue should be possible (check out the unit tests).

I would appreciate if people in this thread would give it a go and file issues if there's something wrong with it.

Check out the repo: https://github.com/koddsson/eslint-plugin-tscompat

dartess commented 3 months ago

@koddsson will this be ported to this repository, or is it a standalone alternative solution?