aboutsummaryrefslogtreecommitdiff
path: root/node_modules/function-name-support/index.js
blob: d421499612577f79e514a7946f3a8d64d268b8d1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
'use strict'

const vm = require('vm')

const context = vm.createContext()
function test (code) {
  try {
    return vm.runInContext(`(function () {
      'use strict'
      ${code}
    })()`, context) === true
  } catch (err) {
    return false
  }
}

const support = {}

// Tests from <https://github.com/kangax/compat-table/blob/c0a3b882b27e4e9f4ef449a1c985ee7857326b94/data-es6.js#L12733:L13115>.
support.functionStatements = test(`
  function foo(){};
  return foo.name === 'foo' &&
    (function(){}).name === '';
`)

support.functionExpressions = test(`
  return (function foo(){}).name === 'foo' &&
    (function(){}).name === '';
`)

support.newFunction = test(`
  return (new Function).name === "anonymous";
`)

support.boundFunctions = test(`
  function foo() {};
  return foo.bind({}).name === "bound foo" &&
    (function(){}).bind({}).name === "bound ";
`)

support.functionVariables = test(`
  var foo = function() {};
  var bar = function baz() {};
  return foo.name === "foo" && bar.name === "baz";
`)

support.functionObjectMethods = test(`
  var o = { foo: function(){}, bar: function baz(){}};
  o.qux = function(){};
  return o.foo.name === "foo" &&
         o.bar.name === "baz" &&
         o.qux.name === "";
`)

support.accessorProperties = test(`
  var o = { get foo(){}, set foo(x){} };
  var descriptor = Object.getOwnPropertyDescriptor(o, "foo");
  return descriptor.get.name === "get foo" &&
         descriptor.set.name === "set foo";
`)

support.shorthandMethods = test(`
  var o = { foo(){} };
  return o.foo.name === "foo"
`)

support.symbolKeyedMethods = test(`
  var sym1 = Symbol("foo");
  var sym2 = Symbol();
  var o = {
    [sym1]: function(){},
    [sym2]: function(){}
  };

  return o[sym1].name === "[foo]" &&
         o[sym2].name === "";
`)

support.classStatements = test(`
  class foo {};
  class bar { static name() {} };
  return foo.name === "foo" &&
    typeof bar.name === "function";
`)

support.classExpressions = test(`
  return class foo {}.name === "foo" &&
    typeof class bar { static name() {} }.name === "function";
`)

support.classVariables = test(`
  var foo = class {};
  var bar = class baz {};
  var qux = class { static name() {} };
  return foo.name === "foo" &&
         bar.name === "baz" &&
         typeof qux.name === "function";
`)

support.classObjectMethods = test(`
  var o = { foo: class {}, bar: class baz {}};
  o.qux = class {};
  return o.foo.name === "foo" &&
         o.bar.name === "baz" &&
         o.qux.name === "";
`)

support.classPrototypeMethods = test(`
  class C { foo(){} };
  return (new C).foo.name === "foo";
`)

support.classStaticMethods = test(`
  class C { static foo(){} };
  return C.foo.name === "foo";
`)

exports.support = Object.freeze(support)

const hasFullSupport =
  support.functionStatements &&
  support.functionExpressions &&
  support.newFunction &&
  support.boundFunctions &&
  support.functionVariables &&
  support.functionObjectMethods &&
  support.accessorProperties &&
  support.shorthandMethods &&
  support.symbolKeyedMethods &&
  support.classStatements &&
  support.classExpressions &&
  support.classVariables &&
  support.classObjectMethods &&
  support.classPrototypeMethods &&
  support.classStaticMethods
exports.hasFullSupport = hasFullSupport

const bitFlags = [
  'functionStatements',
  'functionExpressions',
  'newFunction',
  'boundFunctions',
  'functionVariables',
  'functionObjectMethods',
  'accessorProperties',
  'shorthandMethods',
  'symbolKeyedMethods',
  'classStatements',
  'classExpressions',
  'classVariables',
  'classObjectMethods',
  'classPrototypeMethods',
  'classStaticMethods'
  // Add new flags at the end. Reordering flags is a breaking change.
].reduce((acc, key, index) => {
  return support[key] === true
    ? acc + (1 << index)
    : acc
}, 0b0)
exports.bitFlags = bitFlags

exports.isSubsetOf = otherFlags => (bitFlags & otherFlags) === bitFlags
exports.isSupersetOf = otherFlags => (bitFlags & otherFlags) === otherFlags