1 | import getValue, { getLiteralValue } from './values';
|
---|
2 |
|
---|
3 | const extractValue = (attribute, extractor) => {
|
---|
4 | if (attribute && attribute.type === 'JSXAttribute') {
|
---|
5 | if (attribute.value === null) {
|
---|
6 | // Null valued attributes imply truthiness.
|
---|
7 | // For example: <div aria-hidden />
|
---|
8 | // See: https://facebook.github.io/react/docs/jsx-in-depth.html#boolean-attributes
|
---|
9 | return true;
|
---|
10 | }
|
---|
11 |
|
---|
12 | return extractor(attribute.value);
|
---|
13 | }
|
---|
14 |
|
---|
15 | return undefined;
|
---|
16 | };
|
---|
17 |
|
---|
18 | /**
|
---|
19 | * Returns the value of a given attribute.
|
---|
20 | * Different types of attributes have their associated
|
---|
21 | * values in different properties on the object.
|
---|
22 | *
|
---|
23 | * This function should return the most *closely* associated
|
---|
24 | * value with the intention of the JSX.
|
---|
25 | *
|
---|
26 | * @param attribute - The JSXAttribute collected by AST parser.
|
---|
27 | */
|
---|
28 | export default function getPropValue(attribute) {
|
---|
29 | return extractValue(attribute, getValue);
|
---|
30 | }
|
---|
31 |
|
---|
32 | /**
|
---|
33 | * Returns the value of a given attribute.
|
---|
34 | * Different types of attributes have their associated
|
---|
35 | * values in different properties on the object.
|
---|
36 | *
|
---|
37 | * This function should return a value only if we can extract
|
---|
38 | * a literal value from its attribute (i.e. values that have generic
|
---|
39 | * types in JavaScript - strings, numbers, booleans, etc.)
|
---|
40 | *
|
---|
41 | * @param attribute - The JSXAttribute collected by AST parser.
|
---|
42 | */
|
---|
43 | export function getLiteralPropValue(attribute) {
|
---|
44 | return extractValue(attribute, getLiteralValue);
|
---|
45 | }
|
---|