123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196 |
- 'use strict';
- const elementType = require('jsx-ast-utils/elementType');
- const astUtil = require('./ast');
- const isCreateElement = require('./isCreateElement');
- const variableUtil = require('./variable');
- const COMPAT_TAG_REGEX = /^[a-z]/;
- function isDOMComponent(node) {
- const name = elementType(node);
- return COMPAT_TAG_REGEX.test(name);
- }
- function isFragment(node, reactPragma, fragmentPragma) {
- const name = node.openingElement.name;
-
- if (name.type === 'JSXIdentifier' && name.name === fragmentPragma) {
- return true;
- }
-
- if (
- name.type === 'JSXMemberExpression'
- && name.object.type === 'JSXIdentifier'
- && name.object.name === reactPragma
- && name.property.type === 'JSXIdentifier'
- && name.property.name === fragmentPragma
- ) {
- return true;
- }
- return false;
- }
- function isJSX(node) {
- return node && ['JSXElement', 'JSXFragment'].indexOf(node.type) >= 0;
- }
- function isJSXAttributeKey(node) {
- return node.type === 'JSXAttribute'
- && node.name
- && node.name.type === 'JSXIdentifier'
- && node.name.name === 'key';
- }
- function isWhiteSpaces(value) {
- return typeof value === 'string' ? /^\s*$/.test(value) : false;
- }
- function isReturningJSX(ASTnode, context, strict, ignoreNull) {
- const isJSXValue = (node) => {
- if (!node) {
- return false;
- }
- switch (node.type) {
- case 'ConditionalExpression':
- if (strict) {
- return isJSXValue(node.consequent) && isJSXValue(node.alternate);
- }
- return isJSXValue(node.consequent) || isJSXValue(node.alternate);
- case 'LogicalExpression':
- if (strict) {
- return isJSXValue(node.left) && isJSXValue(node.right);
- }
- return isJSXValue(node.left) || isJSXValue(node.right);
- case 'SequenceExpression':
- return isJSXValue(node.expressions[node.expressions.length - 1]);
- case 'JSXElement':
- case 'JSXFragment':
- return true;
- case 'CallExpression':
- return isCreateElement(node, context);
- case 'Literal':
- if (!ignoreNull && node.value === null) {
- return true;
- }
- return false;
- case 'Identifier': {
- const variable = variableUtil.findVariableByName(context, node.name);
- return isJSX(variable);
- }
- default:
- return false;
- }
- };
- let found = false;
- astUtil.traverseReturns(ASTnode, context, (node, breakTraverse) => {
- if (isJSXValue(node)) {
- found = true;
- breakTraverse();
- }
- });
- return found;
- }
- function isReturningOnlyNull(ASTnode, context) {
- let found = false;
- let foundSomethingElse = false;
- astUtil.traverseReturns(ASTnode, context, (node) => {
-
- astUtil.traverse(node, {
- enter(childNode) {
- const setFound = () => {
- found = true;
- this.skip();
- };
- const setFoundSomethingElse = () => {
- foundSomethingElse = true;
- this.skip();
- };
- switch (childNode.type) {
- case 'ReturnStatement':
- break;
- case 'ConditionalExpression':
- if (childNode.consequent.value === null && childNode.alternate.value === null) {
- setFound();
- }
- break;
- case 'Literal':
- if (childNode.value === null) {
- setFound();
- }
- break;
- default:
- setFoundSomethingElse();
- }
- },
- });
- });
- return found && !foundSomethingElse;
- }
- module.exports = {
- isDOMComponent,
- isFragment,
- isJSX,
- isJSXAttributeKey,
- isWhiteSpaces,
- isReturningJSX,
- isReturningOnlyNull,
- };
|