Skip to content

Commit

Permalink
RN: Replace error-subclass-name with require-extends-error ESLint…
Browse files Browse the repository at this point in the history
… Rule

Summary:
Removes the `error-subclass-name` ESLint rule from `react-native/eslint-plugin`, and implements a new `require-extends-error` ESLint rule inside the React Native repository. This rule was only intended to be used for internal development of React Native.

This will change `react-native/eslint-plugin` to no longer provide the `error-subclass-name` rule.

NOTE: One behavior difference here is that I also implemented the desired behavior of checking classes that extend `Library.SomeError`.

Changelog:
[General][Removed] - `react-native/eslint-plugin` no longer provides the `error-subclass-name` rule.

Reviewed By: lunaleaps

Differential Revision: D39858882

fbshipit-source-id: 27b53216d77a15b3425bd9669dbc9d954c1c61da
  • Loading branch information
yungsters authored and facebook-github-bot committed Sep 27, 2022
1 parent 1ec69b1 commit 4f83498
Show file tree
Hide file tree
Showing 7 changed files with 261 additions and 174 deletions.
2 changes: 1 addition & 1 deletion .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,11 @@ module.exports = {
{
files: ['Libraries/**/*.js'],
rules: {
'@react-native-community/error-subclass-name': 2,
'@react-native-community/platform-colors': 2,
'@react-native/specs/react-native-modules': 2,
'lint/no-haste-imports': 2,
'lint/no-react-native-imports': 2,
'lint/require-extends-error': 2,
},
},
{
Expand Down
6 changes: 0 additions & 6 deletions packages/eslint-plugin-react-native-community/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,6 @@ Add to your eslint config (`.eslintrc`, or `eslintConfig` field in `package.json

## Rules

### `error-subclass-name`

**NOTE:** This rule is primarily used for developing React Native itself and is not generally applicable to other projects.

Enforces that error classes ( = classes with PascalCase names ending with `Error`) only extend other error classes, and that regular functions don't have names that could be mistaken for those of error classes.

### `platform-colors`

Enforces that calls to `PlatformColor` and `DynamicColorIOS` are statically analyzable to enable performance optimizations.

This file was deleted.

This file was deleted.

1 change: 0 additions & 1 deletion packages/eslint-plugin-react-native-community/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,5 @@
*/

exports.rules = {
'error-subclass-name': require('./error-subclass-name'),
'platform-colors': require('./platform-colors'),
};
151 changes: 151 additions & 0 deletions tools/eslint/rules/__tests__/require-extends-error-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,151 @@
/**
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @format
*/

'use strict';

const rule = require('../require-extends-error.js');
const {RuleTester} = require('eslint');

const ruleTester = new RuleTester({
parser: require.resolve('hermes-eslint'),
parserOptions: {
ecmaVersion: 6,
sourceType: 'module',
},
});

ruleTester.run('functions', rule, {
valid: [
{
code: `(function () {});`,
},
{
code: `function xError() {}`,
},
],
invalid: [
{
code: 'function XError() {}',
errors: [{messageId: 'errorFunction', data: {name: 'XError'}}],
},
{
code: '(function XError() {});',
errors: [{messageId: 'errorFunction', data: {name: 'XError'}}],
},
],
});

ruleTester.run('classes', rule, {
valid: [
{
code: `(class {});`,
},
{
code: `(class extends Y {});`,
},
{
code: `class X extends Y {}`,
},
{
code: `(class X extends Y {});`,
},
{
code: `class XError extends Error {}`,
},
{
code: `(class XError extends Error {});`,
},
{
code: `class XError extends YError {}`,
},
{
code: `(class XError extends YError {});`,
},
{
code: `class XError extends Y.Error {}`,
},
{
code: `(class XError extends Y.Error {});`,
},
{
code: `class XError extends Y.Z.Error {}`,
},
{
code: `(class XError extends Y.Z.Error {});`,
},
],
invalid: [
{
code: `class XError {}`,
errors: [{messageId: 'errorClass', data: {name: 'XError'}}],
},
{
code: `(class XError {});`,
errors: [{messageId: 'errorClass', data: {name: 'XError'}}],
},
{
code: `class XError extends Y {}`,
errors: [{messageId: 'errorClass', data: {name: 'XError'}}],
},
{
code: `(class XError extends Y {});`,
errors: [{messageId: 'errorClass', data: {name: 'XError'}}],
},
{
code: `class XError extends Y.Z {}`,
errors: [{messageId: 'errorClass', data: {name: 'XError'}}],
},
{
code: `(class XError extends Y.Z {});`,
errors: [{messageId: 'errorClass', data: {name: 'XError'}}],
},
],
});

ruleTester.run('superclasses', rule, {
valid: [],
invalid: [
{
code: `(class extends Error {});`,
errors: [{messageId: 'errorSuperClassMissingName'}],
},
{
code: `class X extends Error {}`,
errors: [{messageId: 'errorSuperClass', data: {name: 'X'}}],
},
{
code: `(class X extends Error {});`,
errors: [{messageId: 'errorSuperClass', data: {name: 'X'}}],
},
{
code: `class X extends YError {}`,
errors: [{messageId: 'errorSuperClass', data: {name: 'X'}}],
},
{
code: `(class X extends YError {});`,
errors: [{messageId: 'errorSuperClass', data: {name: 'X'}}],
},
{
code: `class X extends Y.Error {}`,
errors: [{messageId: 'errorSuperClass', data: {name: 'X'}}],
},
{
code: `(class X extends Y.Error {});`,
errors: [{messageId: 'errorSuperClass', data: {name: 'X'}}],
},
{
code: `class X extends Y.Z.Error {}`,
errors: [{messageId: 'errorSuperClass', data: {name: 'X'}}],
},
{
code: `(class X extends Y.Z.Error {});`,
errors: [{messageId: 'errorSuperClass', data: {name: 'X'}}],
},
],
});
Loading

0 comments on commit 4f83498

Please sign in to comment.