Skip to content

Commit 931c012

Browse files
committed
[new] - Add API to not infer values and just get literal values.
This is for cases where we do not want to assume anything about dynamic properties (such as variables, functions, etc.). We cannot know their types until runtime, so we should just skip over them when enforcing a rule.
1 parent 58a71b2 commit 931c012

File tree

6 files changed

+63
-20
lines changed

6 files changed

+63
-20
lines changed

.eslintrc.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,6 @@ module.exports = {
6464
"ignorePattern": "((^import[^;]+;$)|(^\\s*it\\())",
6565
"ignoreUrls": true
6666
} ],
67-
"new-cap": 2,
6867
"no-alert": 2,
6968
"no-confusing-arrow": 2,
7069
"no-caller": 2,

src/util/getAttributeValue.js

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,8 @@
11
'use strict';
22

3-
import getValue from './values';
3+
import { getValue, getLiteralValue } from './values';
44

5-
/**
6-
* Returns the value of a given attribute.
7-
* Different types of attributes have their associated
8-
* values in different properties on the object.
9-
*
10-
* This function should return the most *closely* associated
11-
* value with the intention of the JSX.
12-
*
13-
* @param attribute - The JSXAttribute collected by AST parser.
14-
*/
15-
const getAttributeValue = attribute => {
5+
const extract = (attribute, extractor) => {
166
if (attribute.type === 'JSXAttribute') {
177
if (attribute.value === null) {
188
// Null valued attributes imply truthiness.
@@ -21,10 +11,24 @@ const getAttributeValue = attribute => {
2111
return true;
2212
}
2313

24-
return getValue(attribute.value);
14+
return extractor(attribute.value);
2515
}
2616

2717
return undefined;
2818
};
2919

20+
/**
21+
* Returns the value of a given attribute.
22+
* Different types of attributes have their associated
23+
* values in different properties on the object.
24+
*
25+
* This function should return the most *closely* associated
26+
* value with the intention of the JSX.
27+
*
28+
* @param attribute - The JSXAttribute collected by AST parser.
29+
*/
30+
const getAttributeValue = attribute => extract(attribute, getValue);
31+
32+
export const getLiteralAttributeValue = attribute => extract(attribute, getLiteralValue);
33+
3034
export default getAttributeValue;

src/util/values/Literal.js

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,13 @@
1-
const extract = value => value.value;
1+
const extract = value => {
2+
const { value: extractedValue } = value;
3+
4+
if (extractedValue === "true") {
5+
return true;
6+
} else if (extractedValue === "false") {
7+
return false;
8+
}
9+
10+
return extractedValue;
11+
};
212

313
export default extract;

src/util/values/expressions/index.js

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import Literal from '../Literal';
2-
import Identifier from '../Identifier';
2+
import Identifier from './Identifier';
33
import TemplateLiteral from './TemplateLiteral';
44
import ArrowFunctionExpression from './FunctionExpression';
55
import FunctionExpression from './FunctionExpression';
@@ -20,11 +20,35 @@ const TYPES = {
2020
UnaryExpression
2121
};
2222

23-
const extract = value => {
23+
const noop = () => null;
24+
25+
const LITERAL_TYPES = Object.assign({}, TYPES, {
26+
Identifier: value => {
27+
const isUndefined = TYPES.Identifier(value) === undefined;
28+
return isUndefined ? undefined : null;
29+
},
30+
ArrowFunctionExpression: noop,
31+
FunctionExpression: noop,
32+
LogicalExpression: noop,
33+
MemberExpression: noop,
34+
CallExpression: noop,
35+
UnaryExpression: value => {
36+
const extractedVal = TYPES.UnaryExpression(value);
37+
return extractedVal === undefined ? null : extractedVal;
38+
}
39+
});
40+
41+
export const extract = value => {
2442
// Value will not have the expression property when we recurse.
2543
const expression = value.expression || value;
2644

2745
return TYPES[expression.type](expression);
2846
};
2947

48+
export const extractLiteral = value => {
49+
const expression = value.expression || value;
50+
51+
return LITERAL_TYPES[expression.type](expression);
52+
};
53+
3054
export default extract;

src/util/values/index.js

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,21 @@
11
import Literal from './Literal';
2-
import Identifier from './Identifier';
32
import JSXElement from './JSXElement';
43
import JSXExpressionContainer from './expressions';
4+
import { extractLiteral } from './expressions';
55

66
const TYPES = {
77
Literal,
8-
Identifier,
98
JSXElement,
109
JSXExpressionContainer
1110
};
1211

13-
const getValue = value => TYPES[value.type](value);
12+
const LITERAL_TYPES = Object.assign({}, TYPES, {
13+
JSXElement: () => null,
14+
JSXExpressionContainer: extractLiteral
15+
});
16+
17+
export const getValue = value => TYPES[value.type](value);
18+
19+
export const getLiteralValue = value => LITERAL_TYPES[value.type](value);
1420

1521
export default getValue;

0 commit comments

Comments
 (0)