UNPKG

@atlaskit/eslint-plugin-ui-styling-standard

Version:

The eslint plugin to enforce and educate on Atlassian's UI Styling Standard

69 lines • 2.27 kB
import { getScope, getSourceCode } from '@atlaskit/eslint-utils/context-compat'; import { getImportSources, hasStyleObjectArguments } from '@atlaskit/eslint-utils/is-supported-import'; import { createLintRule } from '../utils/create-rule'; const schema = [{ type: 'object', properties: { importSources: { type: 'array', items: { type: 'string' }, uniqueItems: true } } }]; const rule = createLintRule({ meta: { name: 'no-array-arguments', docs: { description: 'Prevents usage of array arguments to style declaration functions', recommended: true, severity: 'error' }, messages: { 'no-array-arguments': "Pass style objects as separate arguments instead of wrapping arguments in an array; delete an empty array argument." }, type: 'problem', fixable: 'code', schema }, create(context) { const importSources = getImportSources(context); return { CallExpression(node) { const { references } = getScope(context, node); if (!hasStyleObjectArguments(node.callee, references, importSources)) { return; } for (const argument of node.arguments) { if (argument.type === 'ArrayExpression') { context.report({ node: argument, messageId: 'no-array-arguments', fix(fixer) { if (argument.elements.length === 0) { /** * Don't autofix if it's an empty array. * * I couldn't figure out how to get rid of the comma afterwards, * so the autofix was producing syntax problems. * * This is an edge case that shouldn't actually be happening, * but if it does it's not hard to just delete the empty array... */ return null; } const items = argument.elements.filter(value => value !== null).map(element => getSourceCode(context).getText(element)); return fixer.replaceText(argument, items.join(', ')); } }); } } } }; } }); export default rule;