From afbc2d08f4dabf00222e9561344b65e51d6ed190 Mon Sep 17 00:00:00 2001 From: Esteban Date: Tue, 16 Nov 2021 21:11:20 +0100 Subject: [PATCH] Remove unused react-internal/invariant-args ESLint rule. (#22778) --- .eslintrc.js | 1 - .../InitializeNativeFabricUIManager.js | 2 - .../ReactNativeViewConfigRegistry.js | 2 - .../Libraries/ReactPrivate/UIManager.js | 2 - .../__tests__/invariant-args-test.internal.js | 83 ------------- scripts/eslint-rules/index.js | 1 - scripts/eslint-rules/invariant-args.js | 109 ------------------ .../ReactNativeViewConfigRegistry.js | 2 - 8 files changed, 202 deletions(-) delete mode 100644 scripts/eslint-rules/__tests__/invariant-args-test.internal.js delete mode 100644 scripts/eslint-rules/invariant-args.js diff --git a/.eslintrc.js b/.eslintrc.js index 6a0dc2dceae47..414adb07f9338 100644 --- a/.eslintrc.js +++ b/.eslintrc.js @@ -113,7 +113,6 @@ module.exports = { {isProductionUserAppCode: true}, ], 'react-internal/no-to-warn-dev-within-to-throw': ERROR, - 'react-internal/invariant-args': ERROR, 'react-internal/warning-args': ERROR, 'react-internal/no-production-logging': ERROR, 'react-internal/no-cross-fork-imports': ERROR, diff --git a/packages/react-native-renderer/src/__mocks__/react-native/Libraries/ReactPrivate/InitializeNativeFabricUIManager.js b/packages/react-native-renderer/src/__mocks__/react-native/Libraries/ReactPrivate/InitializeNativeFabricUIManager.js index d4218ab247347..c0eb4c8317548 100644 --- a/packages/react-native-renderer/src/__mocks__/react-native/Libraries/ReactPrivate/InitializeNativeFabricUIManager.js +++ b/packages/react-native-renderer/src/__mocks__/react-native/Libraries/ReactPrivate/InitializeNativeFabricUIManager.js @@ -5,8 +5,6 @@ * LICENSE file in the root directory of this source tree. */ -/* eslint-disable react-internal/invariant-args */ - 'use strict'; // Mock of the Native Hooks diff --git a/packages/react-native-renderer/src/__mocks__/react-native/Libraries/ReactPrivate/ReactNativeViewConfigRegistry.js b/packages/react-native-renderer/src/__mocks__/react-native/Libraries/ReactPrivate/ReactNativeViewConfigRegistry.js index ce0975ea950a5..dc1b818b34489 100644 --- a/packages/react-native-renderer/src/__mocks__/react-native/Libraries/ReactPrivate/ReactNativeViewConfigRegistry.js +++ b/packages/react-native-renderer/src/__mocks__/react-native/Libraries/ReactPrivate/ReactNativeViewConfigRegistry.js @@ -7,8 +7,6 @@ * @flow strict-local */ -/* eslint-disable react-internal/invariant-args */ - 'use strict'; import type { diff --git a/packages/react-native-renderer/src/__mocks__/react-native/Libraries/ReactPrivate/UIManager.js b/packages/react-native-renderer/src/__mocks__/react-native/Libraries/ReactPrivate/UIManager.js index 346c4aa45c5f5..17ba978e8b37b 100644 --- a/packages/react-native-renderer/src/__mocks__/react-native/Libraries/ReactPrivate/UIManager.js +++ b/packages/react-native-renderer/src/__mocks__/react-native/Libraries/ReactPrivate/UIManager.js @@ -5,8 +5,6 @@ * LICENSE file in the root directory of this source tree. */ -/* eslint-disable react-internal/invariant-args */ - 'use strict'; // Mock of the Native Hooks diff --git a/scripts/eslint-rules/__tests__/invariant-args-test.internal.js b/scripts/eslint-rules/__tests__/invariant-args-test.internal.js deleted file mode 100644 index c36171a62c1d2..0000000000000 --- a/scripts/eslint-rules/__tests__/invariant-args-test.internal.js +++ /dev/null @@ -1,83 +0,0 @@ -/** - * Copyright (c) Facebook, Inc. and its affiliates. - * - * This source code is licensed under the MIT license found in the - * LICENSE file in the root directory of this source tree. - * - * @emails react-core - */ - -'use strict'; - -const rule = require('../invariant-args'); -const {RuleTester} = require('eslint'); -const ruleTester = new RuleTester(); - -ruleTester.run('eslint-rules/invariant-args', rule, { - valid: [ - 'arbitraryFunction(a, b)', - // These messages are in the error code map - "invariant(false, 'Do not override existing functions.')", - "invariant(false, 'createRoot(...): Target container is not a DOM element.')", - ], - invalid: [ - { - code: "invariant('hello, world');", - errors: [ - { - message: 'invariant takes at least two arguments', - }, - ], - }, - { - code: 'invariant(true, null);', - errors: [ - { - message: 'The second argument to invariant must be a string literal', - }, - ], - }, - { - code: 'var g = 5; invariant(true, g);', - errors: [ - { - message: 'The second argument to invariant must be a string literal', - }, - ], - }, - { - code: "invariant(true, 'error!');", - errors: [ - { - message: - 'The invariant format should be able to uniquely identify this ' + - 'invariant. Please, use a more descriptive format than: error!', - }, - ], - }, - { - code: "invariant(true, '%s %s, %s %s: %s (%s)', 1, 2, 3, 4, 5, 6);", - errors: [ - { - message: - 'The invariant format should be able to uniquely identify this ' + - 'invariant. Please, use a more descriptive format than: ' + - '%s %s, %s %s: %s (%s)', - }, - ], - }, - { - code: "invariant(false, 'Not in error map')", - errors: [ - { - message: - 'Error message does not have a corresponding production error code.\n\n' + - 'Run `yarn extract-errors` to add the message to error code map, ' + - 'so it can be stripped from the production builds. ' + - "Alternatively, if you're updating an existing error message, " + - 'you can modify `scripts/error-codes/codes.json` directly.', - }, - ], - }, - ], -}); diff --git a/scripts/eslint-rules/index.js b/scripts/eslint-rules/index.js index 7dfac457ec062..3bf0fb61521da 100644 --- a/scripts/eslint-rules/index.js +++ b/scripts/eslint-rules/index.js @@ -5,7 +5,6 @@ module.exports = { 'no-primitive-constructors': require('./no-primitive-constructors'), 'no-to-warn-dev-within-to-throw': require('./no-to-warn-dev-within-to-throw'), 'warning-args': require('./warning-args'), - 'invariant-args': require('./invariant-args'), 'prod-error-codes': require('./prod-error-codes'), 'no-production-logging': require('./no-production-logging'), 'no-cross-fork-imports': require('./no-cross-fork-imports'), diff --git a/scripts/eslint-rules/invariant-args.js b/scripts/eslint-rules/invariant-args.js deleted file mode 100644 index dd1bb201ec1f3..0000000000000 --- a/scripts/eslint-rules/invariant-args.js +++ /dev/null @@ -1,109 +0,0 @@ -/** - * Copyright (c) Facebook, Inc. and its affiliates. - * - * This source code is licensed under the MIT license found in the - * LICENSE file in the root directory of this source tree. - * - * @emails react-core - */ - -'use strict'; - -const fs = require('fs'); -const path = require('path'); -const existingErrorMap = JSON.parse( - fs.readFileSync(path.resolve(__dirname, '../error-codes/codes.json')) -); -const messages = new Set(); -Object.keys(existingErrorMap).forEach(key => - messages.add(existingErrorMap[key]) -); - -/** - * The warning() and invariant() functions take format strings as their second - * argument. - */ - -module.exports = { - meta: { - schema: [], - }, - create(context) { - // we also allow literal strings and concatenated literal strings - function getLiteralString(node) { - if (node.type === 'Literal' && typeof node.value === 'string') { - return node.value; - } else if (node.type === 'BinaryExpression' && node.operator === '+') { - const l = getLiteralString(node.left); - const r = getLiteralString(node.right); - if (l !== null && r !== null) { - return l + r; - } - } - return null; - } - - return { - CallExpression: function(node) { - // This could be a little smarter by checking context.getScope() to see - // how warning/invariant was defined. - const isInvariant = - node.callee.type === 'Identifier' && node.callee.name === 'invariant'; - if (!isInvariant) { - return; - } - if (node.arguments.length < 2) { - context.report(node, '{{name}} takes at least two arguments', { - name: node.callee.name, - }); - return; - } - const format = getLiteralString(node.arguments[1]); - if (format === null) { - context.report( - node, - 'The second argument to {{name}} must be a string literal', - {name: node.callee.name} - ); - return; - } - if (format.length < 10 || /^[s\W]*$/.test(format)) { - context.report( - node, - 'The {{name}} format should be able to uniquely identify this ' + - '{{name}}. Please, use a more descriptive format than: {{format}}', - {name: node.callee.name, format: format} - ); - return; - } - // count the number of formatting substitutions, plus the first two args - const expectedNArgs = (format.match(/%s/g) || []).length + 2; - if (node.arguments.length !== expectedNArgs) { - context.report( - node, - 'Expected {{expectedNArgs}} arguments in call to {{name}} based on ' + - 'the number of "%s" substitutions, but got {{length}}', - { - expectedNArgs: expectedNArgs, - name: node.callee.name, - length: node.arguments.length, - } - ); - } - - if (!messages.has(format)) { - context.report( - node, - 'Error message does not have a corresponding production ' + - 'error code.\n\n' + - 'Run `yarn extract-errors` to add the message to error code ' + - 'map, so it can be stripped from the production builds. ' + - "Alternatively, if you're updating an existing error " + - 'message, you can modify ' + - '`scripts/error-codes/codes.json` directly.' - ); - } - }, - }; - }, -}; diff --git a/scripts/rollup/shims/react-native/ReactNativeViewConfigRegistry.js b/scripts/rollup/shims/react-native/ReactNativeViewConfigRegistry.js index 5f219fea65208..194a91e31ae62 100644 --- a/scripts/rollup/shims/react-native/ReactNativeViewConfigRegistry.js +++ b/scripts/rollup/shims/react-native/ReactNativeViewConfigRegistry.js @@ -8,8 +8,6 @@ * @flow strict-local */ -/* eslint-disable react-internal/invariant-args */ - 'use strict'; import {type ViewConfig} from './ReactNativeTypes';