UNPKG

eslint-plugin-logical-properties

Version:
23 lines (22 loc) 982 B
import { getValidPropertyName } from '../utils/getValidPropertyName.js'; import { generateDirectionalShorthandError } from './directionalShorthand.js'; export const generateShorthandMappings = (value, mappings) => mappings.map((it) => `"${it}":${value}`); export const directionalShorthandMappingTransformerFactory = ({ node, context, config: { shorthandMappings, }, }) => (property) => { if (!shorthandMappings) { return; } const propertyName = getValidPropertyName(property); const [source, target] = [ context.sourceCode.getText(property), shorthandMappings[propertyName], ]; const value = context.sourceCode.getText(property.value); const replacements = generateShorthandMappings(value, target); context.report({ node: node, message: generateDirectionalShorthandError(source, replacements), fix(fixer) { return fixer.replaceText(property, replacements.join(',')); }, }); };