UNPKG

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

Version:

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

93 lines (92 loc) • 4.42 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.default = void 0; var _contextCompat = require("@atlaskit/eslint-utils/context-compat"); var _isSupportedImport = require("@atlaskit/eslint-utils/is-supported-import"); var _createRule = require("../utils/create-rule"); function _createForOfIteratorHelper(r, e) { var t = "undefined" != typeof Symbol && r[Symbol.iterator] || r["@@iterator"]; if (!t) { if (Array.isArray(r) || (t = _unsupportedIterableToArray(r)) || e && r && "number" == typeof r.length) { t && (r = t); var _n = 0, F = function F() {}; return { s: F, n: function n() { return _n >= r.length ? { done: !0 } : { done: !1, value: r[_n++] }; }, e: function e(r) { throw r; }, f: F }; } throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); } var o, a = !0, u = !1; return { s: function s() { t = t.call(r); }, n: function n() { var r = t.next(); return a = r.done, r; }, e: function e(r) { u = !0, o = r; }, f: function f() { try { a || null == t.return || t.return(); } finally { if (u) throw o; } } }; } function _unsupportedIterableToArray(r, a) { if (r) { if ("string" == typeof r) return _arrayLikeToArray(r, a); var t = {}.toString.call(r).slice(8, -1); return "Object" === t && r.constructor && (t = r.constructor.name), "Map" === t || "Set" === t ? Array.from(r) : "Arguments" === t || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(t) ? _arrayLikeToArray(r, a) : void 0; } } function _arrayLikeToArray(r, a) { (null == a || a > r.length) && (a = r.length); for (var e = 0, n = Array(a); e < a; e++) n[e] = r[e]; return n; } var schema = [{ type: 'object', properties: { importSources: { type: 'array', items: { type: 'string' }, uniqueItems: true } } }]; var rule = (0, _createRule.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: schema }, create: function create(context) { var importSources = (0, _isSupportedImport.getImportSources)(context); return { CallExpression: function CallExpression(node) { var _getScope = (0, _contextCompat.getScope)(context, node), references = _getScope.references; if (!(0, _isSupportedImport.hasStyleObjectArguments)(node.callee, references, importSources)) { return; } var _iterator = _createForOfIteratorHelper(node.arguments), _step; try { var _loop = function _loop() { var argument = _step.value; if (argument.type === 'ArrayExpression') { context.report({ node: argument, messageId: 'no-array-arguments', fix: function 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; } var items = argument.elements.filter(function (value) { return value !== null; }).map(function (element) { return (0, _contextCompat.getSourceCode)(context).getText(element); }); return fixer.replaceText(argument, items.join(', ')); } }); } }; for (_iterator.s(); !(_step = _iterator.n()).done;) { _loop(); } } catch (err) { _iterator.e(err); } finally { _iterator.f(); } } }; } }); var _default = exports.default = rule;