babel-plugin-jsx-remove-qa
Advanced tools
Comparing version 0.1.1 to 0.2.0
97
index.js
@@ -1,53 +0,64 @@ | ||
export default function RemoveQAClasses({ types: t }) { | ||
return { | ||
visitor: { | ||
JSXOpeningElement: function transform(path) { | ||
if (path.node.hasStrippedQAClass) { | ||
return; | ||
} | ||
const classNameRegEx = /\s?qa-([-\w])*/g; | ||
const RemoveQAClasses = ({ types: t }) => { | ||
const visitor = { | ||
JSXOpeningElement: (path, state) => { | ||
if (path.node.hasStrippedQAClass) { | ||
return; | ||
} | ||
const validClassNameAttributes = attr => { | ||
const isIdent = ( | ||
t.isJSXIdentifier(attr.name, { name: 'className' }) | ||
); | ||
return t.isJSXAttribute(attr) && isIdent; | ||
}; | ||
let attributeIdentifiers = 'className'; | ||
const classnameAttributes = path.node.attributes.filter(validClassNameAttributes); | ||
if (state.opts && state.opts.attributes) { | ||
attributeIdentifiers = [attributeIdentifiers, ...state.opts.attributes]; | ||
} | ||
if (!classnameAttributes.length) { | ||
return; | ||
} | ||
const classNameRegEx = /\s?qa-([-\w])*/g; | ||
let newClassNameValue; | ||
classnameAttributes.forEach(attr => { | ||
if (!t.isStringLiteral(attr.value)) { | ||
return; | ||
const validClassNameAttributes = attr => { | ||
const isIdent = attributeIdentifiers.find( | ||
attribute => { | ||
return t.isJSXIdentifier(attr.name, { name: attribute }); | ||
} | ||
); | ||
return t.isJSXAttribute(attr) && isIdent; | ||
}; | ||
const removeQAClassNames = currentAttr => { | ||
if (attr !== currentAttr) { | ||
return currentAttr; | ||
} | ||
const newClassNameValue = attr.value.value.replace(classNameRegEx, '').trim(); | ||
const isStringLiteral = attr => t.isStringLiteral(attr.value); | ||
return t.jSXAttribute( | ||
t.jSXIdentifier(attr.name.name), | ||
t.stringLiteral(newClassNameValue) | ||
); | ||
}; | ||
const replaceClassNameValues = attr => { | ||
const replaceQAClassName = currentAttr => { | ||
if (attr !== currentAttr) { | ||
return currentAttr; | ||
} | ||
newClassNameValue = attr.value.value.replace(classNameRegEx, 'BOB!!!').trim(); | ||
const attrs = path.node.attributes.map(removeQAClassNames); | ||
return t.jSXAttribute( | ||
t.jSXIdentifier(attr.name.name), | ||
t.stringLiteral(newClassNameValue) | ||
); | ||
}; | ||
const node = t.jSXOpeningElement( | ||
path.node.name, | ||
attrs, | ||
path.node.selfClosing | ||
); | ||
node.hasStrippedQAClass = true; | ||
path.replaceWith(node); | ||
}); | ||
} | ||
}, | ||
const attrs = path.node.attributes.map(replaceQAClassName); | ||
const node = t.jSXOpeningElement( | ||
path.node.name, | ||
attrs, | ||
path.node.selfClosing | ||
); | ||
node.hasStrippedQAClass = true; | ||
path.replaceWith(node); | ||
}; | ||
path.node.attributes | ||
.filter(validClassNameAttributes) | ||
.filter(isStringLiteral) | ||
.forEach(replaceClassNameValues); | ||
} | ||
}; | ||
} | ||
return { | ||
visitor | ||
}; | ||
}; | ||
export default RemoveQAClasses; |
{ | ||
"name": "babel-plugin-jsx-remove-qa", | ||
"version": "0.1.1", | ||
"version": "0.2.0", | ||
"description": "babel plugin to remove-qa-classes", | ||
@@ -5,0 +5,0 @@ "main": "dist/index.js", |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
6988
6
187