├── .npmrc ├── .gitattributes ├── .gitignore ├── .github ├── security.md └── workflows │ └── main.yml ├── .editorconfig ├── test.js ├── package.json ├── license ├── index.js ├── readme.md └── index.d.ts /.npmrc: -------------------------------------------------------------------------------- 1 | package-lock=false 2 | -------------------------------------------------------------------------------- /.gitattributes: -------------------------------------------------------------------------------- 1 | * text=auto eol=lf 2 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | node_modules 2 | yarn.lock 3 | -------------------------------------------------------------------------------- /.github/security.md: -------------------------------------------------------------------------------- 1 | # Security Policy 2 | 3 | To report a security vulnerability, please use the [Tidelift security contact](https://tidelift.com/security). Tidelift will coordinate the fix and disclosure. 4 | -------------------------------------------------------------------------------- /.editorconfig: -------------------------------------------------------------------------------- 1 | root = true 2 | 3 | [*] 4 | indent_style = tab 5 | end_of_line = lf 6 | charset = utf-8 7 | trim_trailing_whitespace = true 8 | insert_final_newline = true 9 | 10 | [*.yml] 11 | indent_style = space 12 | indent_size = 2 13 | -------------------------------------------------------------------------------- /.github/workflows/main.yml: -------------------------------------------------------------------------------- 1 | name: CI 2 | on: 3 | - push 4 | - pull_request 5 | jobs: 6 | test: 7 | name: Node.js ${{ matrix.node-version }} 8 | runs-on: ubuntu-latest 9 | strategy: 10 | fail-fast: false 11 | matrix: 12 | node-version: 13 | - 22 14 | - 18 15 | steps: 16 | - uses: actions/checkout@v4 17 | - uses: actions/setup-node@v4 18 | with: 19 | node-version: ${{ matrix.node-version }} 20 | - run: npm install 21 | - run: npm test 22 | -------------------------------------------------------------------------------- /test.js: -------------------------------------------------------------------------------- 1 | import test from 'ava'; 2 | import reservedIdentifiers, {typeScriptReservedTypes} from './index.js'; 3 | 4 | test('main', t => { 5 | t.true(reservedIdentifiers().has('await')); 6 | t.true(reservedIdentifiers().has('let')); 7 | t.true(reservedIdentifiers().has('static')); 8 | t.false(reservedIdentifiers().has('globalThis')); 9 | t.true(reservedIdentifiers({includeGlobalProperties: true}).has('globalThis')); 10 | }); 11 | 12 | test('typeScriptReservedTypes', t => { 13 | const types = typeScriptReservedTypes(); 14 | t.true(types.has('any')); 15 | t.false(types.has('await')); // Should not contain JavaScript keywords that aren't TS types 16 | }); 17 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "reserved-identifiers", 3 | "version": "1.2.0", 4 | "description": "Provides a list of reserved identifiers for JavaScript", 5 | "license": "MIT", 6 | "repository": "sindresorhus/reserved-identifiers", 7 | "funding": "https://github.com/sponsors/sindresorhus", 8 | "author": { 9 | "name": "Sindre Sorhus", 10 | "email": "sindresorhus@gmail.com", 11 | "url": "https://sindresorhus.com" 12 | }, 13 | "type": "module", 14 | "exports": { 15 | "types": "./index.d.ts", 16 | "default": "./index.js" 17 | }, 18 | "sideEffects": false, 19 | "engines": { 20 | "node": ">=18" 21 | }, 22 | "scripts": { 23 | "test": "xo && ava" 24 | }, 25 | "files": [ 26 | "index.js", 27 | "index.d.ts" 28 | ], 29 | "keywords": [ 30 | "reserved", 31 | "identifiers", 32 | "keywords", 33 | "words", 34 | "restricted", 35 | "javascript", 36 | "ecmascript", 37 | "identifier", 38 | "variable", 39 | "function", 40 | "property", 41 | "set" 42 | ], 43 | "devDependencies": { 44 | "ava": "^6.1.2", 45 | "xo": "^0.58.0" 46 | } 47 | } 48 | -------------------------------------------------------------------------------- /license: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) Sindre Sorhus (https://sindresorhus.com) 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: 6 | 7 | The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. 8 | 9 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 10 | -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | // https://262.ecma-international.org/14.0/#sec-keywords-and-reserved-words 2 | // 14 is ES2023 3 | const identifiers = [ 4 | // Keywords 5 | 'await', 6 | 'break', 7 | 'case', 8 | 'catch', 9 | 'class', 10 | 'const', 11 | 'continue', 12 | 'debugger', 13 | 'default', 14 | 'delete', 15 | 'do', 16 | 'else', 17 | 'enum', 18 | 'export', 19 | 'extends', 20 | 'false', 21 | 'finally', 22 | 'for', 23 | 'function', 24 | 'if', 25 | 'import', 26 | 'in', 27 | 'instanceof', 28 | 'new', 29 | 'null', 30 | 'return', 31 | 'super', 32 | 'switch', 33 | 'this', 34 | 'throw', 35 | 'true', 36 | 'try', 37 | 'typeof', 38 | 'var', 39 | 'void', 40 | 'while', 41 | 'with', 42 | 'yield', 43 | 44 | // Future reserved keywords (strict mode) 45 | 'implements', 46 | 'interface', 47 | 'let', 48 | 'package', 49 | 'private', 50 | 'protected', 51 | 'public', 52 | 'static', 53 | 54 | // Not keywords, but still restricted 55 | 'arguments', 56 | 'eval', 57 | ]; 58 | 59 | // https://262.ecma-international.org/14.0/#sec-value-properties-of-the-global-object 60 | const globalProperties = [ 61 | 'globalThis', 62 | 'Infinity', 63 | 'NaN', 64 | 'undefined', 65 | ]; 66 | 67 | // These are TypeScript's built-in types that are reserved and cannot be used for type names 68 | const typeScriptTypes = [ 69 | 'any', 70 | 'bigint', 71 | 'boolean', 72 | 'never', 73 | 'null', 74 | 'number', 75 | 'object', 76 | 'string', 77 | 'symbol', 78 | 'undefined', 79 | 'unknown', 80 | 'void', 81 | ]; 82 | 83 | export default function reservedIdentifiers({includeGlobalProperties = false} = {}) { 84 | return new Set([ 85 | ...identifiers, 86 | ...(includeGlobalProperties ? globalProperties : []), 87 | ]); 88 | } 89 | 90 | export function typeScriptReservedTypes() { 91 | return new Set(typeScriptTypes); 92 | } 93 | -------------------------------------------------------------------------------- /readme.md: -------------------------------------------------------------------------------- 1 | # reserved-identifiers 2 | 3 | > Provides a list of [reserved identifiers](https://262.ecma-international.org/14.0/#sec-keywords-and-reserved-words) for JavaScript 4 | 5 | It assumes the latest JavaScript version (ES2023) and module context. Supporting older JavaScript versions is a non-goal. 6 | 7 | ## Install 8 | 9 | ```sh 10 | npm install reserved-identifiers 11 | ``` 12 | 13 | ## Usage 14 | 15 | ```js 16 | import reservedIdentifiers from 'reserved-identifiers'; 17 | 18 | const identifiers = reservedIdentifiers(); 19 | const isReserved = identifier => identifiers.has(identifier); 20 | 21 | console.log(isReserved('await')); 22 | //=> true 23 | ``` 24 | 25 | ## API 26 | 27 | ### reservedIdentifiers(options?) 28 | 29 | Returns a `Set` with the identifiers. 30 | 31 | #### options 32 | 33 | Type: `object` 34 | 35 | ##### includeGlobalProperties 36 | 37 | Type: `boolean`\ 38 | Default: `false` 39 | 40 | Include the [global properties](https://tc39.es/ecma262/#sec-value-properties-of-the-global-object) `globalThis`, `Infinity`, `NaN`, and `undefined`. Although not officially reserved, they should typically [not be used as identifiers](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/undefined#sect1). 41 | 42 | ### typeScriptReservedTypes() 43 | 44 | Returns a `Set` with TypeScript's built-in types that are reserved and cannot be used for type names (interfaces, type aliases, enums, classes, type parameters). 45 | 46 | ```js 47 | import {typeScriptReservedTypes} from 'reserved-identifiers'; 48 | 49 | const types = typeScriptReservedTypes(); 50 | 51 | console.log(types.has('any')); 52 | //=> true 53 | 54 | console.log(types.has('unknown')); 55 | //=> true 56 | ``` 57 | 58 | ## Related 59 | 60 | - [is-identifier](https://github.com/sindresorhus/is-identifier) - Check if a string is a valid JavaScript identifier 61 | -------------------------------------------------------------------------------- /index.d.ts: -------------------------------------------------------------------------------- 1 | export type Options = { 2 | /** 3 | Include the [global properties](https://tc39.es/ecma262/#sec-value-properties-of-the-global-object) `globalThis`, `Infinity`, `NaN`, and `undefined`. Although not officially reserved, they should typically [not be used as identifiers](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/undefined#sect1). 4 | 5 | @default false 6 | */ 7 | readonly includeGlobalProperties?: boolean; 8 | }; 9 | 10 | /** 11 | Provides a list of [reserved identifiers](https://262.ecma-international.org/14.0/#sec-keywords-and-reserved-words) for JavaScript. 12 | 13 | @example 14 | ``` 15 | import reservedIdentifiers from 'reserved-identifiers'; 16 | 17 | const identifiers = reservedIdentifiers(); 18 | const isReserved = identifier => identifiers.has(identifier); 19 | 20 | console.log(isReserved('await')); 21 | //=> true 22 | ``` 23 | */ 24 | export default function reservedIdentifiers(options?: Options): Set; 25 | 26 | /** 27 | TypeScript's built-in types that are reserved and cannot be used for type names (interfaces, type aliases, enums, classes, type parameters). 28 | */ 29 | export type TypeScriptReservedType = 30 | | 'any' 31 | | 'bigint' 32 | | 'boolean' 33 | | 'never' 34 | | 'null' 35 | | 'number' 36 | | 'object' 37 | | 'string' 38 | | 'symbol' 39 | | 'undefined' 40 | | 'unknown' 41 | | 'void'; 42 | 43 | /** 44 | Provides a list of TypeScript's built-in types that are reserved and cannot be used for type names (interfaces, type aliases, enums, classes, type parameters). 45 | 46 | @example 47 | ``` 48 | import {typeScriptReservedTypes} from 'reserved-identifiers'; 49 | 50 | const types = typeScriptReservedTypes(); 51 | 52 | console.log(types.has('any')); 53 | //=> true 54 | 55 | console.log(types.has('unknown')); 56 | //=> true 57 | ``` 58 | */ 59 | export function typeScriptReservedTypes(): Set; 60 | --------------------------------------------------------------------------------