reserved-words-helper.js 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. /**
  2. * Copyright 2013-present, Facebook, Inc.
  3. * All rights reserved.
  4. *
  5. * This source code is licensed under the BSD-style license found in the
  6. * LICENSE file in the root directory of this source tree. An additional grant
  7. * of patent rights can be found in the PATENTS file in the same directory.
  8. */
  9. var KEYWORDS = [
  10. 'break', 'do', 'in', 'typeof', 'case', 'else', 'instanceof', 'var', 'catch',
  11. 'export', 'new', 'void', 'class', 'extends', 'return', 'while', 'const',
  12. 'finally', 'super', 'with', 'continue', 'for', 'switch', 'yield', 'debugger',
  13. 'function', 'this', 'default', 'if', 'throw', 'delete', 'import', 'try'
  14. ];
  15. var FUTURE_RESERVED_WORDS = [
  16. 'enum', 'await', 'implements', 'package', 'protected', 'static', 'interface',
  17. 'private', 'public'
  18. ];
  19. var LITERALS = [
  20. 'null',
  21. 'true',
  22. 'false'
  23. ];
  24. // https://people.mozilla.org/~jorendorff/es6-draft.html#sec-reserved-words
  25. var RESERVED_WORDS = [].concat(
  26. KEYWORDS,
  27. FUTURE_RESERVED_WORDS,
  28. LITERALS
  29. );
  30. var reservedWordsMap = Object.create(null);
  31. RESERVED_WORDS.forEach(function(k) {
  32. reservedWordsMap[k] = true;
  33. });
  34. /**
  35. * This list should not grow as new reserved words are introdued. This list is
  36. * of words that need to be quoted because ES3-ish browsers do not allow their
  37. * use as identifier names.
  38. */
  39. var ES3_FUTURE_RESERVED_WORDS = [
  40. 'enum', 'implements', 'package', 'protected', 'static', 'interface',
  41. 'private', 'public'
  42. ];
  43. var ES3_RESERVED_WORDS = [].concat(
  44. KEYWORDS,
  45. ES3_FUTURE_RESERVED_WORDS,
  46. LITERALS
  47. );
  48. var es3ReservedWordsMap = Object.create(null);
  49. ES3_RESERVED_WORDS.forEach(function(k) {
  50. es3ReservedWordsMap[k] = true;
  51. });
  52. exports.isReservedWord = function(word) {
  53. return !!reservedWordsMap[word];
  54. };
  55. exports.isES3ReservedWord = function(word) {
  56. return !!es3ReservedWordsMap[word];
  57. };