index.js 5.5 KB

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