@jsxtools/eslint-plugin-jsx-a11y
Version:
Static AST checker for accessibility rules on JSX elements for flat ESLint Config.
53 lines (50 loc) • 1.43 kB
JavaScript
const tags = require('language-tags');
const jsxAstUtils = require('../util/module/jsx-ast-utils.cjs');
const schemas = require('../util/schemas.cjs');
const getElementType = require('../util/getElementType.cjs');
const errorMessage = "lang attribute must have a valid value.";
const schema = schemas.generateObjSchema();
const ruleOfLang = {
meta: {
docs: {
url: "https://github.com/jsx-eslint/eslint-plugin-jsx-a11y/tree/HEAD/docs/rules/lang.md",
description: "Enforce lang attribute has a valid value."
},
schema: [schema]
},
create: (context) => {
const elementType = getElementType(context);
return {
JSXAttribute: (node) => {
const name = jsxAstUtils.propName(node);
if (name && name.toUpperCase() !== "LANG") {
return;
}
const { parent } = node;
const type = elementType(parent);
if (type && type !== "html") {
return;
}
const value = jsxAstUtils.getLiteralPropValue(node);
if (value === null) {
return;
}
if (value === void 0) {
context.report({
node,
message: errorMessage
});
return;
}
if (tags.default.check(value)) {
return;
}
context.report({
node,
message: errorMessage
});
}
};
}
};
module.exports = ruleOfLang;