babel-plugin-flow-type-getter
Advanced tools
Comparing version 1.0.6 to 1.0.7
@@ -95,3 +95,3 @@ class Test1 { | ||
if (Test1.__getFlowTypes != null) { | ||
return Test1.__getFlowTypes()[testing_this_string].stringified; | ||
return Test1.__getFlowTypes()[testing_this_string] != null ? Test1.__getFlowTypes()[testing_this_string].stringified : 'undefined'; | ||
} else { | ||
@@ -104,3 +104,3 @@ //_____babel-plugin-flow-type-getter-marker-comment____ | ||
if (obj_test.t1.__getFlowTypes != null) { | ||
return obj_test.t1.__getFlowTypes().prop2.types.find(__type => __type == 'User') != null; | ||
return obj_test.t1.__getFlowTypes().prop2 != null ? obj_test.t1.__getFlowTypes().prop2.types.find(__type => __type == 'undefined') != null : false; | ||
} else { | ||
@@ -113,3 +113,3 @@ //_____babel-plugin-flow-type-getter-marker-comment____ | ||
if (Test1.__getFlowTypes != null) { | ||
return Test1.__getFlowTypes()['prop5'].is_array; | ||
return Test1.__getFlowTypes()['prop5'] != null ? Test1.__getFlowTypes()['prop5'].is_array : false; | ||
} else { | ||
@@ -122,6 +122,31 @@ //_____babel-plugin-flow-type-getter-marker-comment____ | ||
if (obj_test['t2'].__getFlowTypes != null) { | ||
return obj_test['t2'].__getFlowTypes().prop2.is_array; | ||
return obj_test['t2'].__getFlowTypes().prop2 != null ? obj_test['t2'].__getFlowTypes().prop2.is_array : false; | ||
} else { | ||
//_____babel-plugin-flow-type-getter-marker-comment____ | ||
return Array.isArray(obj_test['t2'].prop2); | ||
} | ||
})()); // true | ||
console.log('undefined test : ', (() => { | ||
if (Test1.__getFlowTypes != null) { | ||
return Test1.__getFlowTypes().prop9 != null ? Test1.__getFlowTypes().prop9.stringified : 'undefined'; | ||
} else { | ||
//_____babel-plugin-flow-type-getter-marker-comment____ | ||
return typeof Test1.prop9; | ||
} | ||
})()); // undefined | ||
console.log('binary undefined test : ', (() => { | ||
if (Test1.__getFlowTypes != null) { | ||
return Test1.__getFlowTypes().prop9 != null ? Test1.__getFlowTypes().prop9.types.find(__type => __type == 'undefined') != null : false; | ||
} else { | ||
//_____babel-plugin-flow-type-getter-marker-comment____ | ||
return typeof Test1.prop9 == string; | ||
} | ||
})()); // false | ||
console.log('array undefined test : ', (() => { | ||
if (Test1.__getFlowTypes != null) { | ||
return Test1.__getFlowTypes().prop9 != null ? Test1.__getFlowTypes().prop9.is_array : false; | ||
} else { | ||
//_____babel-plugin-flow-type-getter-marker-comment____ | ||
return Array.isArray(Test1.prop9); | ||
} | ||
})()); // false |
@@ -45,1 +45,4 @@ class Test1 { | ||
console.log('array test : ', Array.isArray(obj_test['t2'].prop2)) // true | ||
console.log('undefined test : ', typeof Test1.prop9); // undefined | ||
console.log('binary undefined test : ', typeof Test1.prop9 == 'string'); // false | ||
console.log('array undefined test : ', Array.isArray(Test1.prop9)); // false |
30
index.js
@@ -101,2 +101,14 @@ const babylon = require('babylon'); | ||
const unaryExpressionGenerator = (expression) => { | ||
return `${expression} != null ? ${expression}.stringified : 'undefined'`; | ||
} | ||
const binaryExpressionGenerator = (expression, right_value) => { | ||
return `${expression} != null ? ${expression}.types.find(__type => __type == '${right_value}') != null : false`; | ||
} | ||
const arrayExpressionGenerator = (expression) => { | ||
return `${expression} != null ? ${expression}.is_array : false`; | ||
} | ||
const replacementGenerator = (path, replacement_case) => { | ||
@@ -111,10 +123,10 @@ const { node } = path; | ||
if(t.isStringLiteral(node.argument.property)) { | ||
expression = `${object_string}.${customFunctionName}()['${node.argument.property.value}'].stringified`; | ||
expression = unaryExpressionGenerator(`${object_string}.${customFunctionName}()['${node.argument.property.value}']`); | ||
} | ||
else { | ||
expression = `${object_string}.${customFunctionName}()[${prop_name}].stringified`; | ||
expression = unaryExpressionGenerator(`${object_string}.${customFunctionName}()[${prop_name}]`); | ||
} | ||
} | ||
else { | ||
expression = `${object_string}.${customFunctionName}().${prop_name}.stringified`; | ||
expression = unaryExpressionGenerator(`${object_string}.${customFunctionName}().${prop_name}`); | ||
} | ||
@@ -133,10 +145,10 @@ code_expression = `typeof ${object_string}.${prop_name}`; | ||
if(t.isStringLiteral(left.argument.property)) { | ||
expression = `${object_string}.${customFunctionName}()['${left.argument.property.value}'].types.find(__type => __type == '${right_value}') != null`; | ||
expression = binaryExpressionGenerator(`${object_string}.${customFunctionName}()['${left.argument.property.value}']`); | ||
} | ||
else { | ||
expression = `${object_string}.${customFunctionName}()[${prop_name}].types.find(__type => __type == '${right_value}') != null`; | ||
expression = binaryExpressionGenerator(`${object_string}.${customFunctionName}()[${prop_name}]`); | ||
} | ||
} | ||
else { | ||
expression = `${object_string}.${customFunctionName}().${prop_name}.types.find(__type => __type == '${right_value}') != null`; | ||
expression = binaryExpressionGenerator(`${object_string}.${customFunctionName}().${prop_name}`); | ||
} | ||
@@ -151,10 +163,10 @@ code_expression = `typeof ${object_string}.${prop_name} == ${right_value}`; | ||
if(t.isStringLiteral(argument.property)) { | ||
expression = `${object_string}.${customFunctionName}()['${argument.property.value}'].is_array`; | ||
expression = arrayExpressionGenerator(`${object_string}.${customFunctionName}()['${argument.property.value}']`); | ||
} | ||
else { | ||
expression = `${object_string}.${customFunctionName}()[${prop_name}].is_array`; | ||
expression = arrayExpressionGenerator(`${object_string}.${customFunctionName}()[${prop_name}]`); | ||
} | ||
} | ||
else { | ||
expression = `${object_string}.${customFunctionName}().${prop_name}.is_array`; | ||
expression = arrayExpressionGenerator(`${object_string}.${customFunctionName}().${prop_name}`); | ||
} | ||
@@ -161,0 +173,0 @@ code_expression = `Array.isArray(${object_string}.${prop_name})`; |
{ | ||
"name": "babel-plugin-flow-type-getter", | ||
"version": "1.0.6", | ||
"version": "1.0.7", | ||
"description": "Access flow types from uninstantiated classes.", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -59,7 +59,11 @@ ## Access your Flow types from classes before you instantiate objects. | ||
const test1_str = 't1'; | ||
const testing_this_string = 'prop1'; | ||
console.log('unary test : ', typeof obj_test[test1_str].prop1); // number | ||
console.log('unary test : ', typeof Test1[testing_this_string]); // number | ||
console.log('binary test : ', typeof obj_test.t1.prop2 == 'User'); // true | ||
console.log('array test : ', Array.isArray(Test1.prop5)); // false | ||
console.log('array test : ', Array.isArray(Test1['prop5'])); // false | ||
console.log('array test : ', Array.isArray(obj_test['t2'].prop2)) // true | ||
console.log('undefined test : ', typeof Test1.prop9); // undefined | ||
console.log('binary undefined test : ', typeof Test1.prop9 == 'string'); // false | ||
console.log('array undefined test : ', Array.isArray(Test1.prop9)); // false | ||
```` | ||
@@ -66,0 +70,0 @@ --- |
59723
1865
77