diff --git a/types/reserved-words/index.d.ts b/types/reserved-words/index.d.ts new file mode 100644 index 0000000000..b01b539324 --- /dev/null +++ b/types/reserved-words/index.d.ts @@ -0,0 +1,40 @@ +// Type definitions for reserved-words 0.1 +// Project: https://github.com/zxqfox/reserved-words#readme +// Definitions by: Spencer Miskoviak +// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped +// TypeScript Version: 3.0 + +export type Dialect = + | "es3" + | 3 + | "es5" + | 5 + | "es2015" + | 6 + | "es7" + | 7 + | "es6" + | "next"; + +export interface Keywords { + [index: string]: { + [index: string]: true; + }; +} + +export const KEYWORDS: Keywords; + +/** + * Checks a word for being a reserved word and returns true if the provided + * identifier string is a reserved word in some ECMAScript dialect. + * + * @param word word to check + * @param dialect dialect or version + * @param strict strict mode additionally checks whether word is a keyword or + * future reserved word under strict mode. + */ +export function check( + word: string, + dialect?: Dialect, + strict?: boolean +): boolean; diff --git a/types/reserved-words/reserved-words-tests.ts b/types/reserved-words/reserved-words-tests.ts new file mode 100644 index 0000000000..b664d12871 --- /dev/null +++ b/types/reserved-words/reserved-words-tests.ts @@ -0,0 +1,24 @@ +import { check, Dialect, KEYWORDS } from "reserved-words"; + +const dialect: Dialect = "es3"; + +// $ExpectType boolean +check("while"); + +// $ExpectType boolean +check("while", "es6"); + +// $ExpectType boolean +check("while", "es6", true); + +// $ExpectType boolean +check("yield", 3); + +// $ExpectError +check("yield", 1); + +// $ExpectError +check("yield", "es4"); + +// $ExpectType true +KEYWORDS["6-strict"]["break"]; diff --git a/types/reserved-words/tsconfig.json b/types/reserved-words/tsconfig.json new file mode 100644 index 0000000000..104c0d9ca9 --- /dev/null +++ b/types/reserved-words/tsconfig.json @@ -0,0 +1,23 @@ +{ + "compilerOptions": { + "module": "commonjs", + "lib": [ + "es6" + ], + "noImplicitAny": true, + "noImplicitThis": true, + "strictNullChecks": true, + "baseUrl": "../", + "typeRoots": [ + "../" + ], + "types": [], + "noEmit": true, + "forceConsistentCasingInFileNames": true, + "strictFunctionTypes": true + }, + "files": [ + "index.d.ts", + "reserved-words-tests.ts" + ] +} diff --git a/types/reserved-words/tslint.json b/types/reserved-words/tslint.json new file mode 100644 index 0000000000..3db14f85ea --- /dev/null +++ b/types/reserved-words/tslint.json @@ -0,0 +1 @@ +{ "extends": "dtslint/dt.json" }