index.js 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. 'use strict';
  2. /* globals Proxy */
  3. /* eslint no-magic-numbers: 1 */
  4. var test = require('tape');
  5. var isCallable = require('../');
  6. var hasSymbols = typeof Symbol === 'function' && typeof Symbol('foo') === 'symbol';
  7. var generators = require('make-generator-function')();
  8. var arrows = require('make-arrow-function').list();
  9. var asyncs = require('make-async-function').list();
  10. var weirdlyCommentedArrowFn;
  11. try {
  12. /* eslint-disable no-new-func */
  13. weirdlyCommentedArrowFn = Function('return cl/*/**/=>/**/ass - 1;')();
  14. /* eslint-enable no-new-func */
  15. } catch (e) { /**/ }
  16. var forEach = require('foreach');
  17. var noop = function () {};
  18. var classFake = function classFake() { }; // eslint-disable-line func-name-matching
  19. var returnClass = function () { return ' class '; };
  20. var return3 = function () { return 3; };
  21. /* for coverage */
  22. noop();
  23. classFake();
  24. returnClass();
  25. return3();
  26. /* end for coverage */
  27. var proxy;
  28. if (typeof Proxy === 'function') {
  29. try {
  30. proxy = new Proxy(function () {}, {});
  31. // for coverage
  32. proxy();
  33. String(proxy);
  34. } catch (_) {
  35. // If `Reflect` is supported, then `Function.prototype.toString` isn't used for callability detection.
  36. if (typeof Reflect !== 'object') {
  37. // Older engines throw a `TypeError` when `Function.prototype.toString` is called on a Proxy object.
  38. proxy = null;
  39. }
  40. }
  41. }
  42. var invokeFunction = function invokeFunctionString(str) {
  43. var result;
  44. try {
  45. /* eslint-disable no-new-func */
  46. var fn = Function(str);
  47. /* eslint-enable no-new-func */
  48. result = fn();
  49. } catch (e) {}
  50. return result;
  51. };
  52. var classConstructor = invokeFunction('"use strict"; return class Foo {}');
  53. var commentedClass = invokeFunction('"use strict"; return class/*kkk*/\n//blah\n Bar\n//blah\n {}');
  54. var commentedClassOneLine = invokeFunction('"use strict"; return class/**/A{}');
  55. var classAnonymous = invokeFunction('"use strict"; return class{}');
  56. var classAnonymousCommentedOneLine = invokeFunction('"use strict"; return class/*/*/{}');
  57. test('not callables', function (t) {
  58. t.test('non-number/string primitives', function (st) {
  59. st.notOk(isCallable(), 'undefined is not callable');
  60. st.notOk(isCallable(null), 'null is not callable');
  61. st.notOk(isCallable(false), 'false is not callable');
  62. st.notOk(isCallable(true), 'true is not callable');
  63. st.end();
  64. });
  65. t.notOk(isCallable([]), 'array is not callable');
  66. t.notOk(isCallable({}), 'object is not callable');
  67. t.notOk(isCallable(/a/g), 'regex literal is not callable');
  68. t.notOk(isCallable(new RegExp('a', 'g')), 'regex object is not callable');
  69. t.notOk(isCallable(new Date()), 'new Date() is not callable');
  70. t.test('numbers', function (st) {
  71. st.notOk(isCallable(42), 'number is not callable');
  72. st.notOk(isCallable(Object(42)), 'number object is not callable');
  73. st.notOk(isCallable(NaN), 'NaN is not callable');
  74. st.notOk(isCallable(Infinity), 'Infinity is not callable');
  75. st.end();
  76. });
  77. t.test('strings', function (st) {
  78. st.notOk(isCallable('foo'), 'string primitive is not callable');
  79. st.notOk(isCallable(Object('foo')), 'string object is not callable');
  80. st.end();
  81. });
  82. t.test('non-function with function in its [[Prototype]] chain', function (st) {
  83. var Foo = function Bar() {};
  84. Foo.prototype = noop;
  85. st.equal(true, isCallable(Foo), 'sanity check: Foo is callable');
  86. st.equal(false, isCallable(new Foo()), 'instance of Foo is not callable');
  87. st.end();
  88. });
  89. t.end();
  90. });
  91. test('@@toStringTag', { skip: !hasSymbols || !Symbol.toStringTag }, function (t) {
  92. var fakeFunction = {
  93. toString: function () { return String(return3); },
  94. valueOf: return3
  95. };
  96. fakeFunction[Symbol.toStringTag] = 'Function';
  97. t.equal(String(fakeFunction), String(return3));
  98. t.equal(Number(fakeFunction), return3());
  99. t.notOk(isCallable(fakeFunction), 'fake Function with @@toStringTag "Function" is not callable');
  100. t.end();
  101. });
  102. var typedArrayNames = [
  103. 'Int8Array',
  104. 'Uint8Array',
  105. 'Uint8ClampedArray',
  106. 'Int16Array',
  107. 'Uint16Array',
  108. 'Int32Array',
  109. 'Uint32Array',
  110. 'Float32Array',
  111. 'Float64Array'
  112. ];
  113. test('Functions', function (t) {
  114. t.ok(isCallable(noop), 'function is callable');
  115. t.ok(isCallable(classFake), 'function with name containing "class" is callable');
  116. t.ok(isCallable(returnClass), 'function with string " class " is callable');
  117. t.ok(isCallable(isCallable), 'isCallable is callable');
  118. t.end();
  119. });
  120. test('Typed Arrays', function (st) {
  121. forEach(typedArrayNames, function (typedArray) {
  122. /* istanbul ignore if : covered in node 0.6 */
  123. if (typeof global[typedArray] === 'undefined') {
  124. st.comment('# SKIP typed array "' + typedArray + '" not supported');
  125. } else {
  126. st.ok(isCallable(global[typedArray]), typedArray + ' is callable');
  127. }
  128. });
  129. st.end();
  130. });
  131. test('Generators', { skip: generators.length === 0 }, function (t) {
  132. forEach(generators, function (genFn) {
  133. t.ok(isCallable(genFn), 'generator function ' + genFn + ' is callable');
  134. });
  135. t.end();
  136. });
  137. test('Arrow functions', { skip: arrows.length === 0 }, function (t) {
  138. forEach(arrows, function (arrowFn) {
  139. t.ok(isCallable(arrowFn), 'arrow function ' + arrowFn + ' is callable');
  140. });
  141. t.ok(isCallable(weirdlyCommentedArrowFn), 'weirdly commented arrow functions are callable');
  142. t.end();
  143. });
  144. test('"Class" constructors', { skip: !classConstructor || !commentedClass || !commentedClassOneLine || !classAnonymous }, function (t) {
  145. t.notOk(isCallable(classConstructor), 'class constructors are not callable');
  146. t.notOk(isCallable(commentedClass), 'class constructors with comments in the signature are not callable');
  147. t.notOk(isCallable(commentedClassOneLine), 'one-line class constructors with comments in the signature are not callable');
  148. t.notOk(isCallable(classAnonymous), 'anonymous class constructors are not callable');
  149. t.notOk(isCallable(classAnonymousCommentedOneLine), 'anonymous one-line class constructors with comments in the signature are not callable');
  150. t.end();
  151. });
  152. test('`async function`s', { skip: asyncs.length === 0 }, function (t) {
  153. forEach(asyncs, function (asyncFn) {
  154. t.ok(isCallable(asyncFn), '`async function` ' + asyncFn + ' is callable');
  155. });
  156. t.end();
  157. });
  158. test('proxies of functions', { skip: !proxy }, function (t) {
  159. t.ok(isCallable(proxy), 'proxies of functions are callable');
  160. t.end();
  161. });
  162. test('throwing functions', function (t) {
  163. t.plan(1);
  164. var thrower = function (a) { return a.b; };
  165. t.ok(isCallable(thrower), 'a function that throws is callable');
  166. });
  167. /* globals document: false */
  168. test('document.all', { skip: typeof document !== 'object' }, function (t) {
  169. t.notOk(isCallable(document), 'document is not callable');
  170. t.ok(isCallable(document.all), 'document.all is callable');
  171. t.end();
  172. });