keywords-and-reserved_words.js 5.31 KB
Newer Older
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 164 165 166 167 168 169 170 171 172 173 174 175 176 177
// Copyright 2011 the V8 project authors. All rights reserved.
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are
// met:
//
//     * Redistributions of source code must retain the above copyright
//       notice, this list of conditions and the following disclaimer.
//     * Redistributions in binary form must reproduce the above
//       copyright notice, this list of conditions and the following
//       disclaimer in the documentation and/or other materials provided
//       with the distribution.
//     * Neither the name of Google Inc. nor the names of its
//       contributors may be used to endorse or promote products derived
//       from this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

// Test proper handling of keywords, future reserved words and
// future reserved words in strict mode as specific by 7.6.1 and 7.6.2
// in ECMA-262.

// This code is based on:
// http://trac.webkit.org/export/89109/trunk/LayoutTests/fast/js/script-tests/keywords-and-reserved_words.js

function isKeyword(x)
{
  try {
    eval("var " + x + ";");
  } catch(e) {
    return true;
  }

  return false;
}

function isStrictKeyword(x)
{
  try {
    eval("'use strict'; var "+x+";");
  } catch(e) {
    return true;
  }

  return false;
}

function classifyIdentifier(x)
{
  if (isKeyword(x)) {
    // All non-strict keywords are also keywords in strict code.
    if (!isStrictKeyword(x)) {
      return "ERROR";
    }
    return "keyword";
  }

  // Check for strict mode future reserved words.
  if (isStrictKeyword(x)) {
    return "strict";
  }

  return "identifier";
}

function testKeyword(word) {
  // Classify word
  assertEquals("keyword", classifyIdentifier(word));

  // Simple use of a keyword
  assertThrows("var " + word + " = 1;", SyntaxError);
  if (word != "this") {
    assertThrows("typeof (" + word + ");", SyntaxError);
  }

  // object literal properties
  eval("var x = { " + word + " : 42 };");
  eval("var x = { get " + word + " () {} };");
  eval("var x = { set " + word + " (value) {} };");

  // object literal with string literal property names
  eval("var x = { '" + word + "' : 42 };");
  eval("var x = { get '" + word + "' () { } };");
  eval("var x = { set '" + word + "' (value) { } };");

  // Function names and arguments
  assertThrows("function " + word + " () { }", SyntaxError);
  assertThrows("function foo (" + word + ") {}", SyntaxError);
  assertThrows("function foo (a, " + word + ") { }", SyntaxError);
  assertThrows("function foo (" + word + ", a) { }", SyntaxError);
  assertThrows("function foo (a, " + word + ", b) { }", SyntaxError);
  assertThrows("var foo = function (" + word + ") { }", SyntaxError);

  // setter parameter
  assertThrows("var x = { set foo(" + word + ") { } };", SyntaxError);
}

// Not keywords - these are all just identifiers.
var identifiers = [
  "x",            "keyword",
  "id",           "strict",
  "identifier",   "use",
  // The following are reserved in ES3 but not in ES5.
  "abstract",     "int",
  "boolean",      "long",
  "byte",         "native",
  "char",         "short",
  "double",       "synchronized",
  "final",        "throws",
  "float",        "transient",
  "goto",         "volatile" ];

for (var i = 0; i < identifiers.length; i++) {
  assertEquals ("identifier", classifyIdentifier(identifiers[i]));
}

// 7.6.1.1 Keywords
var keywords = [
  "break",        "in",
  "case",         "instanceof",
  "catch",        "new",
  "continue",     "return",
  "debugger",     "switch",
  "default",      "this",
  "delete",       "throw",
  "do",           "try",
  "else",         "typeof",
  "finally",      "var",
  "for",          "void",
  "function",     "while",
  "if",           "with",
  // In ES5 "const" is a "future reserved word" but we treat it as a keyword.
  "const" ];

for (var i = 0; i < keywords.length; i++) {
  testKeyword(keywords[i]);
}

// 7.6.1.2 Future Reserved Words (without "const")
var future_reserved_words = [
  "class",
  "enum",
  "export",
  "extends",
  "import",
  "super" ];

for (var i = 0; i < future_reserved_words.length; i++) {
  testKeyword(future_reserved_words[i]);
}

// 7.6.1.2 Future Reserved Words, in strict mode only.
var future_strict_reserved_words = [
  "implements",
  "interface",
  "let",
  "package",
  "private",
  "protected",
  "public",
  "static",
  "yield" ];

for (var i = 0; i < future_strict_reserved_words.length; i++) {
  assertEquals ("strict", classifyIdentifier(future_strict_reserved_words[i]));
}

// More strict mode specific tests can be found in mjsunit/strict-mode.js.