|
| 1 | +/** |
| 2 | + * Copyright (c) Facebook, Inc. and its affiliates. |
| 3 | + * |
| 4 | + * This source code is licensed under the MIT license found in the |
| 5 | + * LICENSE file in the root directory of this source tree. |
| 6 | + * |
| 7 | + * @emails oncall+react_native |
| 8 | + * @format |
| 9 | + */ |
| 10 | + |
| 11 | +'use strict'; |
| 12 | + |
| 13 | +const ESLintTester = require('./eslint-tester.js'); |
| 14 | + |
| 15 | +const rule = require('../error-subclass-name.js'); |
| 16 | + |
| 17 | +const eslintTester = new ESLintTester(); |
| 18 | + |
| 19 | +const INVALID_SUPERCLASS_MESSAGE = |
| 20 | + "'SomethingEndingWithError' must extend an error class (like 'Error') because its name is in PascalCase and ends with 'Error'."; |
| 21 | +const INVALID_OWN_NAME_MESSAGE = |
| 22 | + "'Foo' may not be the name of an error class. It should be in PascalCase and end with 'Error'."; |
| 23 | +const MISSING_OWN_NAME_MESSAGE = |
| 24 | + "An error class should have a PascalCase name ending with 'Error'."; |
| 25 | +const INVALID_FUNCTION_NAME_MESSAGE = |
| 26 | + "'SomethingEndingWithError' is a reserved name. PascalCase names ending with 'Error' are reserved for error classes and may not be used for regular functions. Either rename this function or convert it to a class that extends 'Error'."; |
| 27 | + |
| 28 | +eslintTester.run('../error-subclass-name', rule, { |
| 29 | + valid: [ |
| 30 | + 'class FooError extends Error {}', |
| 31 | + '(class FooError extends Error {})', |
| 32 | + 'class FooError extends SomethingEndingWithError {}', |
| 33 | + '(class FooError extends SomethingEndingWithError {})', |
| 34 | + 'function makeError() {}', |
| 35 | + '(function () {})', |
| 36 | + |
| 37 | + // The following cases are currently allowed but could be disallowed in the |
| 38 | + // future. This is technically an escape hatch. |
| 39 | + 'class Foo extends SomeLibrary.FooError {}', |
| 40 | + '(class extends SomeLibrary.FooError {})', |
| 41 | + ], |
| 42 | + invalid: [ |
| 43 | + { |
| 44 | + code: 'class SomethingEndingWithError {}', |
| 45 | + errors: [{message: INVALID_SUPERCLASS_MESSAGE}], |
| 46 | + }, |
| 47 | + { |
| 48 | + code: '(class SomethingEndingWithError {})', |
| 49 | + errors: [{message: INVALID_SUPERCLASS_MESSAGE}], |
| 50 | + }, |
| 51 | + { |
| 52 | + code: 'class Foo extends Error {}', |
| 53 | + errors: [{message: INVALID_OWN_NAME_MESSAGE}], |
| 54 | + }, |
| 55 | + { |
| 56 | + code: '(class Foo extends Error {})', |
| 57 | + errors: [{message: INVALID_OWN_NAME_MESSAGE}], |
| 58 | + }, |
| 59 | + { |
| 60 | + code: 'class Foo extends SomethingEndingWithError {}', |
| 61 | + errors: [{message: INVALID_OWN_NAME_MESSAGE}], |
| 62 | + }, |
| 63 | + { |
| 64 | + code: '(class Foo extends SomethingEndingWithError {})', |
| 65 | + errors: [{message: INVALID_OWN_NAME_MESSAGE}], |
| 66 | + }, |
| 67 | + { |
| 68 | + code: '(class extends Error {})', |
| 69 | + errors: [{message: MISSING_OWN_NAME_MESSAGE}], |
| 70 | + }, |
| 71 | + { |
| 72 | + code: 'class SomethingEndingWithError extends C {}', |
| 73 | + errors: [{message: INVALID_SUPERCLASS_MESSAGE}], |
| 74 | + }, |
| 75 | + { |
| 76 | + code: '(class SomethingEndingWithError extends C {})', |
| 77 | + errors: [{message: INVALID_SUPERCLASS_MESSAGE}], |
| 78 | + }, |
| 79 | + { |
| 80 | + code: 'function SomethingEndingWithError() {}', |
| 81 | + errors: [{message: INVALID_FUNCTION_NAME_MESSAGE}], |
| 82 | + }, |
| 83 | + { |
| 84 | + code: '(function SomethingEndingWithError() {})', |
| 85 | + errors: [{message: INVALID_FUNCTION_NAME_MESSAGE}], |
| 86 | + }, |
| 87 | + |
| 88 | + // The following cases are intentionally disallowed because the member |
| 89 | + // expression `SomeLibrary.FooError` doesn't imply that the superclass is |
| 90 | + // actually declared with the name `FooError`. |
| 91 | + { |
| 92 | + code: 'class SomethingEndingWithError extends SomeLibrary.FooError {}', |
| 93 | + errors: [{message: INVALID_SUPERCLASS_MESSAGE}], |
| 94 | + }, |
| 95 | + { |
| 96 | + code: '(class SomethingEndingWithError extends SomeLibrary.FooError {})', |
| 97 | + errors: [{message: INVALID_SUPERCLASS_MESSAGE}], |
| 98 | + }, |
| 99 | + ], |
| 100 | +}); |
0 commit comments