@jsxtools/eslint-plugin-jsx-a11y
Version:
Static AST checker for accessibility rules on JSX elements for flat ESLint Config.
51 lines (48 loc) • 1.75 kB
JavaScript
import { runVirtualRule } from '../util/module/axe-core.js';
import { getLiteralPropValue, getProp } from '../util/module/jsx-ast-utils.js';
import { generateObjSchema, arraySchema } from '../util/schemas.js';
import getElementType from '../util/getElementType.js';
const schema = generateObjSchema({
inputComponents: arraySchema
});
const ruleOfAutocompleteValid = {
meta: {
docs: {
url: "https://github.com/jsx-eslint/eslint-plugin-jsx-a11y/tree/HEAD/docs/rules/autocomplete-valid.md",
description: "Enforce that autocomplete attributes are used correctly."
},
schema: [schema]
},
create: (context) => {
const elementType = getElementType(context);
return {
JSXOpeningElement: (node) => {
const options = context.options[0] || {};
const { inputComponents = [] } = options;
const inputTypes = ["input"].concat(inputComponents);
const elType = elementType(node);
const autocomplete = getLiteralPropValue(getProp(node.attributes, "autocomplete"));
if (typeof autocomplete !== "string" || !inputTypes.includes(elType)) {
return;
}
const type = getLiteralPropValue(getProp(node.attributes, "type"));
const { violations } = runVirtualRule("autocomplete-valid", {
nodeName: "input",
attributes: {
autocomplete,
// Which autocomplete is valid depends on the input type
type: type === null ? void 0 : type
}
});
if (violations.length === 0) {
return;
}
context.report({
node,
message: violations[0].nodes[0].all[0].message
});
}
};
}
};
export { ruleOfAutocompleteValid as default };