Permalink
Name already in use
A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
codeql-action/node_modules/jsx-ast-utils/src/getProp.js
Go to fileThis commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
91 lines (78 sloc)
2.43 KB
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import propName from './propName'; | |
const DEFAULT_OPTIONS = { | |
ignoreCase: true, | |
}; | |
/** | |
* Returns the JSXAttribute itself or undefined, indicating the prop | |
* is not present on the JSXOpeningElement. | |
* | |
*/ | |
export default function getProp(props = [], prop = '', options = DEFAULT_OPTIONS) { | |
function getName(name) { return options.ignoreCase ? name.toUpperCase() : name; } | |
const propToFind = getName(prop); | |
function isPropToFind(property) { | |
return property.type === 'Property' | |
&& property.key.type === 'Identifier' | |
&& propToFind === getName(property.key.name); | |
} | |
const foundAttribute = props.find((attribute) => { | |
// If the props contain a spread prop, try to find the property in the object expression. | |
if (attribute.type === 'JSXSpreadAttribute') { | |
return attribute.argument.type === 'ObjectExpression' | |
&& propToFind !== getName('key') // https://github.com/reactjs/rfcs/pull/107 | |
&& attribute.argument.properties.some(isPropToFind); | |
} | |
return propToFind === getName(propName(attribute)); | |
}); | |
if (foundAttribute && foundAttribute.type === 'JSXSpreadAttribute') { | |
return propertyToJSXAttribute(foundAttribute.argument.properties.find(isPropToFind)); | |
} | |
return foundAttribute; | |
} | |
function propertyToJSXAttribute(node) { | |
const { key, value } = node; | |
return { | |
type: 'JSXAttribute', | |
name: { type: 'JSXIdentifier', name: key.name, ...getBaseProps(key) }, | |
value: value.type === 'Literal' | |
? adjustRangeOfNode(value) | |
: { type: 'JSXExpressionContainer', expression: adjustExpressionRange(value), ...getBaseProps(value) }, | |
...getBaseProps(node), | |
}; | |
} | |
function adjustRangeOfNode(node) { | |
const [start, end] = node.range || [node.start, node.end]; | |
return { | |
...node, | |
end: undefined, | |
range: [start, end], | |
start: undefined, | |
}; | |
} | |
function adjustExpressionRange({ expressions, quasis, ...expression }) { | |
return { | |
...adjustRangeOfNode(expression), | |
...(expressions ? { expressions: expressions.map(adjustRangeOfNode) } : {}), | |
...(quasis ? { quasis: quasis.map(adjustRangeOfNode) } : {}), | |
}; | |
} | |
function getBaseProps({ loc, ...node }) { | |
const { range } = adjustRangeOfNode(node); | |
return { | |
loc: getBaseLocation(loc), | |
range, | |
}; | |
} | |
function getBaseLocation({ | |
start, | |
end, | |
source, | |
filename, | |
}) { | |
return { | |
start, | |
end, | |
...(source !== undefined ? { source } : {}), | |
...(filename !== undefined ? { filename } : {}), | |
}; | |
} |