keyword.coffee 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. # Copyright (C) 2013 Yusuke Suzuki <utatane.tea@gmail.com>
  2. #
  3. # Redistribution and use in source and binary forms, with or without
  4. # modification, are permitted provided that the following conditions are met:
  5. #
  6. # * Redistributions of source code must retain the above copyright
  7. # notice, this list of conditions and the following disclaimer.
  8. # * Redistributions in binary form must reproduce the above copyright
  9. # notice, this list of conditions and the following disclaimer in the
  10. # documentation and/or other materials provided with the distribution.
  11. #
  12. # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  13. # AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  14. # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  15. # ARE DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> BE LIABLE FOR ANY
  16. # DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  17. # (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  18. # LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  19. # ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  20. # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  21. # THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  22. 'use strict'
  23. expect = require('chai').expect
  24. esutils = require '../'
  25. KW = [
  26. 'if'
  27. 'in'
  28. 'do'
  29. 'var'
  30. 'for'
  31. 'new'
  32. 'try'
  33. 'this'
  34. 'else'
  35. 'case'
  36. 'void'
  37. 'with'
  38. 'enum'
  39. 'while'
  40. 'break'
  41. 'catch'
  42. 'throw'
  43. 'const'
  44. 'class'
  45. 'super'
  46. 'return'
  47. 'typeof'
  48. 'delete'
  49. 'switch'
  50. 'export'
  51. 'import'
  52. 'default'
  53. 'finally'
  54. 'extends'
  55. 'function'
  56. 'continue'
  57. 'debugger'
  58. 'instanceof'
  59. ]
  60. SRW = [
  61. 'implements'
  62. 'interface'
  63. 'package'
  64. 'private'
  65. 'protected'
  66. 'public'
  67. 'static'
  68. 'let'
  69. ]
  70. describe 'keyword', ->
  71. describe 'isKeywordES6', ->
  72. it 'returns true if provided string is keyword under non-strict mode', ->
  73. for word in KW
  74. expect(esutils.keyword.isKeywordES6(word, no)).to.be.true
  75. expect(esutils.keyword.isKeywordES6('yield', no)).to.be.true
  76. it 'returns false if provided string is not keyword under non-strict mode', ->
  77. words = [
  78. 'hello'
  79. '20'
  80. '$'
  81. 'ゆゆ式'
  82. ]
  83. for word in words
  84. expect(esutils.keyword.isKeywordES6(word, no)).to.be.false
  85. for word in SRW
  86. expect(esutils.keyword.isKeywordES6(word, no)).to.be.false
  87. it 'returns true if provided string is keyword under strict mode', ->
  88. for word in KW
  89. expect(esutils.keyword.isKeywordES6(word, yes)).to.be.true
  90. expect(esutils.keyword.isKeywordES6('yield', yes)).to.be.true
  91. for word in SRW
  92. expect(esutils.keyword.isKeywordES6(word, yes)).to.be.true
  93. it 'returns false if provided string is not keyword under strict mode', ->
  94. words = [
  95. 'hello'
  96. '20'
  97. '$'
  98. 'ゆゆ式'
  99. ]
  100. for word in words
  101. expect(esutils.keyword.isKeywordES6(word, yes)).to.be.false
  102. describe 'isKeywordES5', ->
  103. it 'returns true if provided string is keyword under non-strict mode', ->
  104. for word in KW
  105. expect(esutils.keyword.isKeywordES5(word, no)).to.be.true
  106. it 'returns false if provided string is not keyword under non-strict mode', ->
  107. words = [
  108. 'hello'
  109. '20'
  110. '$'
  111. 'ゆゆ式'
  112. ]
  113. for word in words
  114. expect(esutils.keyword.isKeywordES5(word, no)).to.be.false
  115. for word in SRW
  116. expect(esutils.keyword.isKeywordES5(word, no)).to.be.false
  117. expect(esutils.keyword.isKeywordES5('yield', no)).to.be.false
  118. it 'returns true if provided string is keyword under strict mode', ->
  119. for word in KW
  120. expect(esutils.keyword.isKeywordES5(word, yes)).to.be.true
  121. expect(esutils.keyword.isKeywordES5('yield', yes)).to.be.true
  122. for word in SRW
  123. expect(esutils.keyword.isKeywordES5(word, yes)).to.be.true
  124. it 'returns false if provided string is not keyword under strict mode', ->
  125. words = [
  126. 'hello'
  127. '20'
  128. '$'
  129. 'ゆゆ式'
  130. ]
  131. for word in words
  132. expect(esutils.keyword.isKeywordES5(word, yes)).to.be.false
  133. describe 'isRestrictedWord', ->
  134. it 'returns true if provided string is "eval" or "arguments"', ->
  135. expect(esutils.keyword.isRestrictedWord('eval')).to.be.true
  136. expect(esutils.keyword.isRestrictedWord('arguments')).to.be.true
  137. it 'returns false if provided string is not "eval" or "arguments"', ->
  138. words = [
  139. 'hello'
  140. '20'
  141. '$'
  142. 'ゆゆ式'
  143. ]
  144. for word in words
  145. expect(esutils.keyword.isRestrictedWord(word)).to.be.false
  146. describe 'isIdentifierName', ->
  147. it 'returns false if provided string is empty', ->
  148. expect(esutils.keyword.isIdentifierName('')).to.be.false
  149. it 'returns true if provided string is IdentifierName', ->
  150. words = [
  151. 'hello'
  152. '$'
  153. 'ゆゆ式'
  154. '$20'
  155. 'hello20'
  156. '_'
  157. ]
  158. for word in words
  159. expect(esutils.keyword.isIdentifierName(word)).to.be.true
  160. it 'returns false if provided string is not IdentifierName', ->
  161. words = [
  162. '+hello'
  163. '0$'
  164. '-ゆゆ式'
  165. '#_'
  166. ]
  167. for word in words
  168. expect(esutils.keyword.isIdentifierName(word)).to.be.false