eslint-plugin-logical-properties
Version:
23 lines (22 loc) • 1.03 kB
JavaScript
import { getValidPropertyName } from '../utils/getValidPropertyName.js';
import { generateDirectionalPropertyError } from './directionalMapping.js';
export const generateDirectionalValueError = (property, source, target) => generateDirectionalPropertyError(`${property}: ${source}`, `${property}: ${target}`);
export const directionalValueTransformerFactory = ({ node, context, config: { values = {}, }, }) => (property) => {
const propertyName = getValidPropertyName(property);
const [source, target] = [
context.sourceCode.getText(property.value)
.replace(/^["'`]/, '')
.replace(/["'`]$/, ''),
values[propertyName],
];
if (Object.keys(target).includes(source)) {
const replacement = target[source];
context.report({
node,
message: generateDirectionalValueError(propertyName, source, replacement),
fix(fixer) {
return fixer.replaceText(property.value, `"${replacement}"`);
},
});
}
};