@atlaskit/eslint-plugin-design-system
Version:
The essential plugin for use with the Atlassian Design System.
49 lines (47 loc) • 2.63 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.hrefHasInvalidValue = void 0;
var _eslintCodemodUtils = require("eslint-codemod-utils");
var _findInParent = require("../utils/find-in-parent");
var invalidHrefValues = ['', '#', null, undefined];
var hrefHasInvalidValue = exports.hrefHasInvalidValue = function hrefHasInvalidValue(scope, href) {
// If doesn't exist,
if (!href) {
return true;
} else if (href.value) {
// If it is an invalid literal,
if ((0, _eslintCodemodUtils.isNodeOfType)(href.value, 'Literal') &&
// We know this must be a string because node is type 'Literal'
invalidHrefValues.includes(href.value.value)) {
return true;
// If it is an expression with a variable inside
} else if ((0, _eslintCodemodUtils.isNodeOfType)(href.value, 'JSXExpressionContainer') && (0, _eslintCodemodUtils.isNodeOfType)(href.value.expression, 'Identifier')) {
// Get value within the variable
var identifierName = href.value.expression.name;
var variable = (0, _findInParent.findIdentifierInParentScope)({
scope: scope,
identifierName: identifierName
});
// If the variable can't be found in the parent scope, do not throw as
// invalid because we can't know what the value actually is.
if (variable) {
var _defNode$init, _defNode$init2;
var defNode = variable.defs[0].node;
// Should be accepted as a valid `href` for
// * imported variables
// * local variables with an valid value
// * local variables defined via destructuring
// * arguments in a function declaration
// * arguments in an anonymous function
if (defNode !== null && defNode !== void 0 && defNode.imported || defNode !== null && defNode !== void 0 && (_defNode$init = defNode.init) !== null && _defNode$init !== void 0 && _defNode$init.value && !invalidHrefValues.includes(defNode === null || defNode === void 0 || (_defNode$init2 = defNode.init) === null || _defNode$init2 === void 0 ? void 0 : _defNode$init2.value) || (0, _eslintCodemodUtils.isNodeOfType)(defNode, 'VariableDeclarator') && defNode !== null && defNode !== void 0 && defNode.init && (0, _eslintCodemodUtils.isNodeOfType)(defNode.init, 'Identifier') && (0, _eslintCodemodUtils.isNodeOfType)(defNode.id, 'ObjectPattern') || (0, _eslintCodemodUtils.isNodeOfType)(defNode, 'FunctionDeclaration') || (0, _eslintCodemodUtils.isNodeOfType)(defNode, 'ArrowFunctionExpression')) {
return false;
} else {
return true;
}
}
}
}
return false;
};