What is reserved-words?
The 'reserved-words' npm package is used to check if a given word is a reserved keyword in JavaScript or other ECMAScript versions. This can be useful for validating identifiers in code generation, linting, or other scenarios where you need to ensure that certain words are not used as variable names or other identifiers.
What are reserved-words's main functionalities?
Check if a word is reserved in a specific ECMAScript version
This feature allows you to check if a given word is a reserved keyword in a specific ECMAScript version. In this example, 'class' is checked against ECMAScript 6 (ES6) and returns true because 'class' is a reserved keyword in ES6.
const reservedWords = require('reserved-words');
const isReserved = reservedWords.check('class', 6); // true for ES6
console.log(isReserved);
Check if a word is reserved in multiple ECMAScript versions
This feature allows you to check if a given word is reserved in multiple ECMAScript versions at once. In this example, 'class' is checked against ES3, ES5, and ES6, and returns true because 'class' is a reserved keyword in ES6.
const reservedWords = require('reserved-words');
const isReserved = reservedWords.check('class', [3, 5, 6]); // true for ES3, ES5, and ES6
console.log(isReserved);
Get a list of reserved words for a specific ECMAScript version
This feature allows you to get a list of all reserved words for a specific ECMAScript version. In this example, it retrieves the list of reserved words for ES6.
const reservedWords = require('reserved-words');
const reservedList = reservedWords.list(6);
console.log(reservedList);
Other packages similar to reserved-words
esprima
Esprima is a high-performance, standard-compliant ECMAScript parser. It can be used to parse JavaScript code and analyze its syntax, including checking for reserved words. Compared to 'reserved-words', Esprima offers a broader range of functionalities for parsing and analyzing JavaScript code.
acorn
Acorn is a small, fast, JavaScript-based ECMAScript parser. It can be used to parse JavaScript code and check for reserved words as part of its syntax analysis. Acorn is similar to Esprima but is designed to be faster and more modular.
reserved-words
![Build Status](https://secure.travis-ci.org/zxqfox/reserved-words.svg)
What is it?
Tiny package for detecting reserved words.
Reserved Word
is either a Keyword
, or a Future Reserved Word
, or a Null Literal
, or a Boolean Literal
.
See: ES5 #7.6.1 and
ES6 #11.6.2.
Installation
npm install reserved-words
API
check(word, [dialect], [strict])
Returns true
if provided identifier string is a Reserved Word
in some ECMAScript dialect (ECMA-262 edition).
If the strict
flag is truthy, this function additionally checks whether
word
is a Keyword or Future Reserved Word under strict mode.
Example
var reserved = require('reserved-words');
reserved.check('volatile', 'es3'); // true
reserved.check('volatile', 'es2015'); // false
reserved.check('yield', 3); // false
reserved.check('yield', 6); // true
dialects
es3 (or 3)
Represents ECMA-262 3rd edition.
See section 7.5.1.
es5 (or 5)
Represents ECMA-262 5th edition (ECMAScript 5.1).
Reserved Words are formally defined in ECMA262 sections
7.6.1.1 and 7.6.1.2.
es2015 (or es6, 6)
Represents [ECMA-262 6th edition](ECMAScript 2015).
Reserved Words are formally defined in sections
11.6.2.1 and
11.6.2.2.
License
Licensed under The MIT License