/
githubmirror
/
react-native
Обзор
Документация
Войти
/
githubmirror
/
react-native
Код
Запросы
0
Пакеты
0
Релизы
0
Аналитика
Безопасность
main
packages/eslint-plugin-react-native/platform-colors.js
99 строк
3 KB
Peter Abbondanzo
PlatformColor lazy fallback: lint rule + RNTester example (#57705)
06 авг 2026, 17:37
06 авг 2026, 17:37
31e119a
Код
Авторство
О чём код?
/** * 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 * @noflow */ module.exports = { meta: { type: 'problem', docs: { description: 'Ensure that PlatformColor() and DynamicColorIOS() are passed literals of the expected shape.', }, messages: { platformColorArgsLength: 'PlatformColor() must have at least one argument that is a literal.', platformColorArgTypes: 'PlatformColor() every argument must be a literal.', dynamicColorIOSArg: 'DynamicColorIOS() must take a single argument of type Object', dynamicColorIOSValue: 'DynamicColorIOS() value must be either a literal or a PlatformColor() call.', }, schema: [], }, create: function (context) { return { CallExpression: function (node) { if (node.callee.name === 'PlatformColor') { const args = node.arguments; // Optional trailing {fallback: <literal>}: exactly one `fallback` // property with a literal value, so it stays statically analyzable. const isFallbackObject = arg => arg.type === 'ObjectExpression' && arg.properties.length === 1 && arg.properties.every( property => property.type === 'Property' && // Reject computed keys (e.g. {['fallback']: ...}); only a plain // identifier key keeps the object statically analyzable. property.computed === false && property.key.type === 'Identifier' && property.key.name === 'fallback' && property.value.type === 'Literal', ); if (args.length === 0) { context.report({ node, messageId: 'platformColorArgsLength', }); return; } if ( !args.every( (arg, index) => arg.type === 'Literal' || (index === args.length - 1 && isFallbackObject(arg)), ) ) { context.report({ node, messageId: 'platformColorArgTypes', }); return; } } else if (node.callee.name === 'DynamicColorIOS') { const args = node.arguments; if (!(args.length === 1 && args[0].type === 'ObjectExpression')) { context.report({ node, messageId: 'dynamicColorIOSArg', }); return; } const properties = args[0].properties; properties.forEach(property => { if (!( property.type === 'Property' && (property.value.type === 'Literal' || (property.value.type === 'CallExpression' && property.value.callee.name === 'PlatformColor')) )) { context.report({ node, messageId: 'dynamicColorIOSValue', }); return; } }); } }, }; }, };