simplify unary resolution and move operators to types
This commit is contained in:
parent
7333b4e392
commit
da1d5e5577
@ -1,13 +1,6 @@
|
||||
import type NodePath from "./index";
|
||||
import * as t from "../../types";
|
||||
|
||||
const BOOLEAN_BINARY_OPERATORS = ["==", "===", "!=", "!==", ">", "<", ">=", "<=", "in", "instanceof"];
|
||||
const NUMBER_BINARY_OPERATORS = ["-", "/", "*", "**", "&", "|", ">>", ">>>", "<<", "^"];
|
||||
|
||||
const BOOLEAN_UNARY_OPERATORS = ["delete"];
|
||||
const NUMBER_UNARY_OPERATORS = ["+", "-", "++", "--", "~"];
|
||||
const STRING_UNARY_OPERATORS = ["typeof"];
|
||||
|
||||
/**
|
||||
* Description
|
||||
*/
|
||||
@ -131,6 +124,11 @@ export function _inferTypeAnnotation(force?: boolean): ?Object {
|
||||
var path = this.resolve();
|
||||
|
||||
var node = path.node;
|
||||
|
||||
if (!node && path.key === "init" && path.parentPath.isVariableDeclarator()) {
|
||||
return t.voidTypeAnnotation();
|
||||
}
|
||||
|
||||
if (!node) return;
|
||||
|
||||
if (node.typeAnnotation) {
|
||||
@ -199,11 +197,11 @@ export function _inferTypeAnnotation(force?: boolean): ?Object {
|
||||
|
||||
if (operator === "void") {
|
||||
return t.voidTypeAnnotation();
|
||||
} else if (NUMBER_UNARY_OPERATORS.indexOf(operator) >= 0) {
|
||||
} else if (t.NUMBER_UNARY_OPERATORS.indexOf(operator) >= 0) {
|
||||
return t.numberTypeAnnotation();
|
||||
} else if (STRING_UNARY_OPERATORS.indexOf(operator) >= 0) {
|
||||
} else if (t.STRING_UNARY_OPERATORS.indexOf(operator) >= 0) {
|
||||
return t.stringTypeAnnotation();
|
||||
} else if (BOOLEAN_UNARY_OPERATORS.indexOf(operator) >= 0) {
|
||||
} else if (t.BOOLEAN_UNARY_OPERATORS.indexOf(operator) >= 0) {
|
||||
return t.booleanTypeAnnotation();
|
||||
}
|
||||
}
|
||||
@ -211,9 +209,9 @@ export function _inferTypeAnnotation(force?: boolean): ?Object {
|
||||
if (path.isBinaryExpression()) {
|
||||
let operator = node.operator;
|
||||
|
||||
if (NUMBER_BINARY_OPERATORS.indexOf(operator) >= 0) {
|
||||
if (t.NUMBER_BINARY_OPERATORS.indexOf(operator) >= 0) {
|
||||
return t.numberTypeAnnotation();
|
||||
} else if (BOOLEAN_BINARY_OPERATORS.indexOf(operator) >= 0) {
|
||||
} else if (t.BOOLEAN_BINARY_OPERATORS.indexOf(operator) >= 0) {
|
||||
return t.booleanTypeAnnotation();
|
||||
} else if (operator === "+") {
|
||||
var right = path.get("right").resolve();
|
||||
@ -260,15 +258,6 @@ export function _inferTypeAnnotation(force?: boolean): ?Object {
|
||||
}
|
||||
}
|
||||
|
||||
if (path.isUnaryExpression() && node.prefix) {
|
||||
let operator = node.operator;
|
||||
if (operator === "!") {
|
||||
return t.booleanTypeAnnotation();
|
||||
} else if (operator === "+" || operator === "-") {
|
||||
return t.numberTypeAnnotation();
|
||||
}
|
||||
}
|
||||
|
||||
if (path.isLiteral()) {
|
||||
var value = node.value;
|
||||
if (typeof value === "string") return t.stringTypeAnnotation();
|
||||
|
||||
@ -24,11 +24,17 @@ function registerType(type: string, skipAliasCheck?: boolean) {
|
||||
};
|
||||
}
|
||||
|
||||
export var STATEMENT_OR_BLOCK_KEYS = ["consequent", "body", "alternate"];
|
||||
export var NATIVE_TYPE_NAMES = ["Array", "ArrayBuffer", "Boolean", "DataView", "Date", "Error", "EvalError", "Float32Array", "Float64Array", "Function", "Int8Array", "Int16Array", "Int32Array", "Map", "Number", "Object", "Proxy", "Promise", "RangeError", "ReferenceError", "RegExp", "Set", "String", "Symbol", "SyntaxError", "TypeError", "Uint8Array", "Uint8ClampedArray", "Uint16Array", "Uint32Array", "URIError", "WeakMap", "WeakSet"];
|
||||
export var FLATTENABLE_KEYS = ["body", "expressions"];
|
||||
export var FOR_INIT_KEYS = ["left", "init"];
|
||||
export var COMMENT_KEYS = ["leadingComments", "trailingComments"];
|
||||
export const STATEMENT_OR_BLOCK_KEYS = ["consequent", "body", "alternate"];
|
||||
export const FLATTENABLE_KEYS = ["body", "expressions"];
|
||||
export const FOR_INIT_KEYS = ["left", "init"];
|
||||
export const COMMENT_KEYS = ["leadingComments", "trailingComments"];
|
||||
|
||||
export const BOOLEAN_BINARY_OPERATORS = ["==", "===", "!=", "!==", ">", "<", ">=", "<=", "in", "instanceof"];
|
||||
export const NUMBER_BINARY_OPERATORS = ["-", "/", "*", "**", "&", "|", ">>", ">>>", "<<", "^"];
|
||||
|
||||
export const BOOLEAN_UNARY_OPERATORS = ["delete", "!"];
|
||||
export const NUMBER_UNARY_OPERATORS = ["+", "-", "++", "--", "~"];
|
||||
export const STRING_UNARY_OPERATORS = ["typeof"];
|
||||
|
||||
export const VISITOR_KEYS = require("./visitor-keys");
|
||||
export const BUILDER_KEYS = require("./builder-keys");
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user