mirror of
https://github.com/hanzoai/eslint.git
synced 2026-10-30 19:38:10 +00:00
271 lines
5.7 KiB
JavaScript
271 lines
5.7 KiB
JavaScript
/**
|
|
* @fileoverview Tests for no-empty-pattern rule.
|
|
* @author Alberto RodrÃguez
|
|
*/
|
|
"use strict";
|
|
|
|
//------------------------------------------------------------------------------
|
|
// Requirements
|
|
//------------------------------------------------------------------------------
|
|
|
|
const rule = require("../../../lib/rules/no-empty-pattern"),
|
|
RuleTester = require("../../../lib/rule-tester/rule-tester");
|
|
|
|
//------------------------------------------------------------------------------
|
|
// Tests
|
|
//------------------------------------------------------------------------------
|
|
|
|
const ruleTester = new RuleTester();
|
|
|
|
ruleTester.run("no-empty-pattern", rule, {
|
|
// Examples of code that should not trigger the rule
|
|
valid: [
|
|
{ code: "var {a = {}} = foo;", languageOptions: { ecmaVersion: 6 } },
|
|
{ code: "var {a, b = {}} = foo;", languageOptions: { ecmaVersion: 6 } },
|
|
{ code: "var {a = []} = foo;", languageOptions: { ecmaVersion: 6 } },
|
|
{
|
|
code: "function foo({a = {}}) {}",
|
|
languageOptions: { ecmaVersion: 6 },
|
|
},
|
|
{
|
|
code: "function foo({a = []}) {}",
|
|
languageOptions: { ecmaVersion: 6 },
|
|
},
|
|
{ code: "var [a] = foo", languageOptions: { ecmaVersion: 6 } },
|
|
{
|
|
code: "function foo({}) {}",
|
|
options: [{ allowObjectPatternsAsParameters: true }],
|
|
languageOptions: { ecmaVersion: 6 },
|
|
},
|
|
{
|
|
code: "var foo = function({}) {}",
|
|
options: [{ allowObjectPatternsAsParameters: true }],
|
|
languageOptions: { ecmaVersion: 6 },
|
|
},
|
|
{
|
|
code: "var foo = ({}) => {}",
|
|
options: [{ allowObjectPatternsAsParameters: true }],
|
|
languageOptions: { ecmaVersion: 6 },
|
|
},
|
|
{
|
|
code: "function foo({} = {}) {}",
|
|
options: [{ allowObjectPatternsAsParameters: true }],
|
|
languageOptions: { ecmaVersion: 6 },
|
|
},
|
|
{
|
|
code: "var foo = function({} = {}) {}",
|
|
options: [{ allowObjectPatternsAsParameters: true }],
|
|
languageOptions: { ecmaVersion: 6 },
|
|
},
|
|
{
|
|
code: "var foo = ({} = {}) => {}",
|
|
options: [{ allowObjectPatternsAsParameters: true }],
|
|
languageOptions: { ecmaVersion: 6 },
|
|
},
|
|
],
|
|
|
|
// Examples of code that should trigger the rule
|
|
invalid: [
|
|
{
|
|
code: "var {} = foo",
|
|
languageOptions: { ecmaVersion: 6 },
|
|
errors: [
|
|
{
|
|
messageId: "unexpected",
|
|
data: { type: "object" },
|
|
},
|
|
],
|
|
},
|
|
{
|
|
code: "var [] = foo",
|
|
languageOptions: { ecmaVersion: 6 },
|
|
errors: [
|
|
{
|
|
messageId: "unexpected",
|
|
data: { type: "array" },
|
|
},
|
|
],
|
|
},
|
|
{
|
|
code: "var {a: {}} = foo",
|
|
languageOptions: { ecmaVersion: 6 },
|
|
errors: [
|
|
{
|
|
messageId: "unexpected",
|
|
data: { type: "object" },
|
|
},
|
|
],
|
|
},
|
|
{
|
|
code: "var {a, b: {}} = foo",
|
|
languageOptions: { ecmaVersion: 6 },
|
|
errors: [
|
|
{
|
|
messageId: "unexpected",
|
|
data: { type: "object" },
|
|
},
|
|
],
|
|
},
|
|
{
|
|
code: "var {a: []} = foo",
|
|
languageOptions: { ecmaVersion: 6 },
|
|
errors: [
|
|
{
|
|
messageId: "unexpected",
|
|
data: { type: "array" },
|
|
},
|
|
],
|
|
},
|
|
{
|
|
code: "function foo({}) {}",
|
|
languageOptions: { ecmaVersion: 6 },
|
|
errors: [
|
|
{
|
|
messageId: "unexpected",
|
|
data: { type: "object" },
|
|
},
|
|
],
|
|
},
|
|
{
|
|
code: "function foo([]) {}",
|
|
languageOptions: { ecmaVersion: 6 },
|
|
errors: [
|
|
{
|
|
messageId: "unexpected",
|
|
data: { type: "array" },
|
|
},
|
|
],
|
|
},
|
|
{
|
|
code: "function foo({a: {}}) {}",
|
|
languageOptions: { ecmaVersion: 6 },
|
|
errors: [
|
|
{
|
|
messageId: "unexpected",
|
|
data: { type: "object" },
|
|
},
|
|
],
|
|
},
|
|
{
|
|
code: "function foo({a: []}) {}",
|
|
languageOptions: { ecmaVersion: 6 },
|
|
errors: [
|
|
{
|
|
messageId: "unexpected",
|
|
data: { type: "array" },
|
|
},
|
|
],
|
|
},
|
|
{
|
|
code: "function foo({}) {}",
|
|
options: [{}],
|
|
languageOptions: { ecmaVersion: 6 },
|
|
errors: [
|
|
{
|
|
messageId: "unexpected",
|
|
data: { type: "object" },
|
|
},
|
|
],
|
|
},
|
|
{
|
|
code: "var foo = function({}) {}",
|
|
options: [{}],
|
|
languageOptions: { ecmaVersion: 6 },
|
|
errors: [
|
|
{
|
|
messageId: "unexpected",
|
|
data: { type: "object" },
|
|
},
|
|
],
|
|
},
|
|
{
|
|
code: "var foo = ({}) => {}",
|
|
options: [{}],
|
|
languageOptions: { ecmaVersion: 6 },
|
|
errors: [
|
|
{
|
|
messageId: "unexpected",
|
|
data: { type: "object" },
|
|
},
|
|
],
|
|
},
|
|
{
|
|
code: "function foo({} = {}) {}",
|
|
options: [{}],
|
|
languageOptions: { ecmaVersion: 6 },
|
|
errors: [
|
|
{
|
|
messageId: "unexpected",
|
|
data: { type: "object" },
|
|
},
|
|
],
|
|
},
|
|
{
|
|
code: "var foo = function({} = {}) {}",
|
|
options: [{}],
|
|
languageOptions: { ecmaVersion: 6 },
|
|
errors: [
|
|
{
|
|
messageId: "unexpected",
|
|
data: { type: "object" },
|
|
},
|
|
],
|
|
},
|
|
{
|
|
code: "var foo = ({} = {}) => {}",
|
|
options: [{}],
|
|
languageOptions: { ecmaVersion: 6 },
|
|
errors: [
|
|
{
|
|
messageId: "unexpected",
|
|
data: { type: "object" },
|
|
},
|
|
],
|
|
},
|
|
{
|
|
code: "var foo = ({a: {}}) => {}",
|
|
options: [{ allowObjectPatternsAsParameters: true }],
|
|
languageOptions: { ecmaVersion: 6 },
|
|
errors: [
|
|
{
|
|
messageId: "unexpected",
|
|
data: { type: "object" },
|
|
},
|
|
],
|
|
},
|
|
{
|
|
code: "var foo = ({} = bar) => {}",
|
|
options: [{ allowObjectPatternsAsParameters: true }],
|
|
languageOptions: { ecmaVersion: 6 },
|
|
errors: [
|
|
{
|
|
messageId: "unexpected",
|
|
data: { type: "object" },
|
|
},
|
|
],
|
|
},
|
|
{
|
|
code: "var foo = ({} = { bar: 1 }) => {}",
|
|
options: [{ allowObjectPatternsAsParameters: true }],
|
|
languageOptions: { ecmaVersion: 6 },
|
|
errors: [
|
|
{
|
|
messageId: "unexpected",
|
|
data: { type: "object" },
|
|
},
|
|
],
|
|
},
|
|
{
|
|
code: "var foo = ([]) => {}",
|
|
options: [{ allowObjectPatternsAsParameters: true }],
|
|
languageOptions: { ecmaVersion: 6 },
|
|
errors: [
|
|
{
|
|
messageId: "unexpected",
|
|
data: { type: "array" },
|
|
},
|
|
],
|
|
},
|
|
],
|
|
});
|