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

✨ add rules for prototype members #54

Merged
merged 37 commits into from
Dec 3, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
37 commits
Select commit Hold shift + click to select a range
84e6344
✨ add es/no-string-prototype-codepointat
mysticatea Nov 30, 2020
54275ef
update definePrototypeMethodHandler
mysticatea Dec 3, 2020
e9f7332
✨ es/no-array-prototype-every
mysticatea Dec 3, 2020
5f11096
✨ es/no-array-prototype-filter
mysticatea Dec 3, 2020
b0afcd7
✨ es/no-array-prototype-foreach
mysticatea Dec 3, 2020
f0569e9
✨ es/no-array-prototype-indexof
mysticatea Dec 3, 2020
1ebc3d5
✨ es/no-array-prototype-lastindexof
mysticatea Dec 3, 2020
6fdb9b4
✨ es/no-array-prototype-map
mysticatea Dec 3, 2020
a6b721c
✨ es/no-array-prototype-reduce
mysticatea Dec 3, 2020
fb123e7
✨ es/no-array-prototype-reduceright
mysticatea Dec 3, 2020
66ce23b
✨ es/no-array-prototype-some
mysticatea Dec 3, 2020
fb35e52
✨ es/no-string-prototype-trim
mysticatea Dec 3, 2020
cf18e1f
✨ es/no-array-prototype-copywithin
mysticatea Dec 3, 2020
3b73ddb
✨ es/no-array-prototype-entries
mysticatea Dec 3, 2020
869117f
✨ es/no-array-prototype-fill
mysticatea Dec 3, 2020
07490d1
✨ es/no-array-prototype-find
mysticatea Dec 3, 2020
bd0afba
✨ es/no-array-prototype-findindex
mysticatea Dec 3, 2020
877aef1
✨ es/no-array-prototype-keys
mysticatea Dec 3, 2020
9a69048
✨ es/no-array-prototype-values
mysticatea Dec 3, 2020
d4131a8
✨ es/no-regexp-prototype-flags
mysticatea Dec 3, 2020
7dbe1ed
📝 small fix on no-string-prototype-codepointat
mysticatea Dec 3, 2020
503c2dc
✨ add es/no-string-prototype-endswith
mysticatea Dec 3, 2020
b4465ba
✨ add es/no-string-prototype-includes
mysticatea Dec 3, 2020
d7f4d08
✨ add es/no-string-prototype-normalize
mysticatea Dec 3, 2020
533b747
✨ add es/no-string-prototype-repeat
mysticatea Dec 3, 2020
3a34707
✨ add es/no-string-prototype-startswith
mysticatea Dec 3, 2020
9faa335
✨ add es/no-array-prototype-includes
mysticatea Dec 3, 2020
54fd0f2
✨ add es/no-string-prototype-padstart-padend
mysticatea Dec 3, 2020
7fb4681
✨ add es/no-promise-prototype-finally
mysticatea Dec 3, 2020
420daf6
✨ add es/no-array-prototype-flat
mysticatea Dec 3, 2020
6fb0ba3
✨ add es/no-string-prototype-trimstart-trimend
mysticatea Dec 3, 2020
29f87a6
✨ add es/no-symbol-prototype-description
mysticatea Dec 3, 2020
185d6c7
✨ add es/no-string-prototype-matchall
mysticatea Dec 3, 2020
f5fe076
✨ add es/no-string-prototype-replaceall
mysticatea Dec 3, 2020
e9dea69
⚒ update auto-generated files
mysticatea Dec 3, 2020
dde9044
📝 fix examples
mysticatea Dec 3, 2020
dc524f4
📝 fix examples
mysticatea Dec 3, 2020
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
✨ es/no-array-prototype-copywithin
  • Loading branch information
mysticatea committed Dec 3, 2020
commit cf18e1f4f0f933e116c52d81c01b7acc7c36378d
35 changes: 35 additions & 0 deletions docs/rules/no-array-prototype-copywithin.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
# es/no-array-prototype-copywithin
> disallow the `Array.prototype.copyWithin` method

- ✅ The following configurations enable this rule: `plugin:es/no-new-in-es2015`, `plugin:es/restrict-to-es3`, and `plugin:es/restrict-to-es5`

This rule reports ES2015 `Array.prototype.copyWithin` method as errors.

This rule is silent by default because it's hard to know types. You need to configure [the aggressive mode](../#the-aggressive-mode) or TypeScript in order to enable this rule.

## 💡 Examples

⛔ Examples of **incorrect** code for this rule:

<eslint-playground type="bad" code="/*eslint es/no-array-prototype-copywithin: [error, { aggressive: true }] */
foo.copyWithin(0, 1, 2)
" />

## 🔧 Options

This rule has an option.

```yml
rules:
es/no-array-prototype-copywithin: [error, { aggressive: false }]
```

### aggressive: boolean

Configure the aggressive mode for only this rule.
This is prior to the `settings.es.aggressive` setting.

## 📚 References

- [Rule source](https://github.com/mysticatea/eslint-plugin-es/blob/v4.1.0/lib/rules/no-array-prototype-copywithin.js)
- [Test source](https://github.com/mysticatea/eslint-plugin-es/blob/v4.1.0/tests/lib/rules/no-array-prototype-copywithin.js)
40 changes: 40 additions & 0 deletions lib/rules/no-array-prototype-copywithin.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/**
* @author Toru Nagashima <https://github.com/mysticatea>
* See LICENSE file in root directory for full license.
*/
"use strict"

const {
definePrototypeMethodHandler,
} = require("../util/define-prototype-method-handler")

module.exports = {
meta: {
docs: {
description: "disallow the `Array.prototype.copyWithin` method.",
category: "ES2015",
recommended: false,
url:
"http://mysticatea.github.io/eslint-plugin-es/rules/no-array-prototype-copywithin.html",
},
fixable: null,
messages: {
forbidden: "ES2015 '{{name}}' method is forbidden.",
},
schema: [
{
type: "object",
properties: {
aggressive: { type: "boolean" },
},
additionalProperties: false,
},
],
type: "problem",
},
create(context) {
return definePrototypeMethodHandler(context, {
Array: ["copyWithin"],
})
},
}
202 changes: 202 additions & 0 deletions tests/lib/rules/no-array-prototype-copywithin.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,202 @@
/**
* @author Toru Nagashima <https://github.com/mysticatea>
* See LICENSE file in root directory for full license.
*/
"use strict"

const path = require("path")
const RuleTester = require("../../tester")
const rule = require("../../../lib/rules/no-array-prototype-copywithin.js")
const ruleId = "no-array-prototype-copywithin"

new RuleTester().run(ruleId, rule, {
valid: [
"copyWithin(0, 1, 2)",
"foo.reverse()",
"foo.copyWithin(0, 1, 2)",
{ code: "copyWithin(0, 1, 2)", settings: { es: { aggressive: true } } },
{ code: "foo.reverse()", settings: { es: { aggressive: true } } },
{
code: "foo.copyWithin(0, 1, 2)",
options: [{ aggressive: false }],
settings: { es: { aggressive: true } },
},
],
invalid: [
{
code: "foo.copyWithin(0, 1, 2)",
errors: [
"ES2015 'Array.prototype.copyWithin' method is forbidden.",
],
settings: { es: { aggressive: true } },
},
{
code: "foo.copyWithin(0, 1, 2)",
options: [{ aggressive: true }],
errors: [
"ES2015 'Array.prototype.copyWithin' method is forbidden.",
],
settings: { es: { aggressive: false } },
},
],
})

// -----------------------------------------------------------------------------
// TypeScript
// -----------------------------------------------------------------------------
const parser = require.resolve("@typescript-eslint/parser")
const tsconfigRootDir = path.resolve(__dirname, "../../fixtures")
const project = "tsconfig.json"
const filename = path.join(tsconfigRootDir, "test.ts")

new RuleTester({ parser }).run(`${ruleId} TS`, rule, {
valid: [
{ filename, code: "copyWithin(0, 1, 2)" },
{ filename, code: "foo.reverse()" },
{ filename, code: "foo.copyWithin(0, 1, 2)" },
{ filename, code: "let foo = {}; foo.copyWithin(0, 1, 2)" },
{
filename,
code: "copyWithin(0, 1, 2)",
settings: { es: { aggressive: true } },
},
{
filename,
code: "foo.reverse()",
settings: { es: { aggressive: true } },
},

// `Array` is unknown type if tsconfig.json is not configured.
{ filename, code: "let foo = []; foo.copyWithin(0, 1, 2)" },
{ filename, code: "let foo = Array(); foo.copyWithin(0, 1, 2)" },
{
filename,
code: "function f<T extends any[]>(a: T) { a.copyWithin(0, 1, 2) }",
},
{
filename,
code:
"function f<T extends string[] | number[]>(a: T) { a.copyWithin(0, 1, 2) }",
},
],
invalid: [
{
filename,
code: "[a, b, c].copyWithin(0, 1, 2)",
errors: [
"ES2015 'Array.prototype.copyWithin' method is forbidden.",
],
},
{
filename,
code: "let foo = []; foo.copyWithin(0, 1, 2)",
errors: [
"ES2015 'Array.prototype.copyWithin' method is forbidden.",
],
settings: { es: { aggressive: true } },
},
{
filename,
code: "let foo = Array(); foo.copyWithin(0, 1, 2)",
errors: [
"ES2015 'Array.prototype.copyWithin' method is forbidden.",
],
settings: { es: { aggressive: true } },
},
{
filename,
code: "function f<T extends any[]>(a: T) { a.copyWithin(0, 1, 2) }",
errors: [
"ES2015 'Array.prototype.copyWithin' method is forbidden.",
],
settings: { es: { aggressive: true } },
},
{
filename,
code:
"function f<T extends string[] | number[]>(a: T) { a.copyWithin(0, 1, 2) }",
errors: [
"ES2015 'Array.prototype.copyWithin' method is forbidden.",
],
settings: { es: { aggressive: true } },
},
{
filename,
code: "foo.copyWithin(0, 1, 2)",
errors: [
"ES2015 'Array.prototype.copyWithin' method is forbidden.",
],
settings: { es: { aggressive: true } },
},
],
})

new RuleTester({ parser, parserOptions: { tsconfigRootDir, project } }).run(
`${ruleId} TS Full Type Information`,
rule,
{
valid: [
{ filename, code: "copyWithin(0, 1, 2)" },
{ filename, code: "foo.reverse()" },
{ filename, code: "foo.copyWithin(0, 1, 2)" },
{ filename, code: "let foo = {}; foo.copyWithin(0, 1, 2)" },
{
filename,
code: "copyWithin(0, 1, 2)",
settings: { es: { aggressive: true } },
},
{
filename,
code: "foo.reverse()",
settings: { es: { aggressive: true } },
},
],
invalid: [
{
filename,
code: "[a, b, c].copyWithin(0, 1, 2)",
errors: [
"ES2015 'Array.prototype.copyWithin' method is forbidden.",
],
},
{
filename,
code: "let foo = []; foo.copyWithin(0, 1, 2)",
errors: [
"ES2015 'Array.prototype.copyWithin' method is forbidden.",
],
},
{
filename,
code: "let foo = Array(); foo.copyWithin(0, 1, 2)",
errors: [
"ES2015 'Array.prototype.copyWithin' method is forbidden.",
],
},
{
filename,
code:
"function f<T extends any[]>(a: T) { a.copyWithin(0, 1, 2) }",
errors: [
"ES2015 'Array.prototype.copyWithin' method is forbidden.",
],
},
{
filename,
code:
"function f<T extends string[] | number[]>(a: T) { a.copyWithin(0, 1, 2) }",
errors: [
"ES2015 'Array.prototype.copyWithin' method is forbidden.",
],
},
{
filename,
code: "foo.copyWithin(0, 1, 2)",
errors: [
"ES2015 'Array.prototype.copyWithin' method is forbidden.",
],
settings: { es: { aggressive: true } },
},
],
},
)