| |
| |
| |
| |
| |
| "use strict"; |
|
|
| |
| |
| |
|
|
| const globalVariablesValues = new Set([ |
| true, |
| "true", |
| "writable", |
| "writeable", |
| false, |
| "false", |
| "readonly", |
| "readable", |
| null, |
| "off", |
| ]); |
|
|
| |
| |
| |
|
|
| |
| |
| |
| |
| |
| function isNonNullObject(value) { |
| return typeof value === "object" && value !== null; |
| } |
|
|
| |
| |
| |
| |
| |
| function isNonArrayObject(value) { |
| return isNonNullObject(value) && !Array.isArray(value); |
| } |
|
|
| |
| |
| |
| |
| |
| function isUndefined(value) { |
| return typeof value === "undefined"; |
| } |
|
|
| |
| |
| |
|
|
| |
| |
| |
| |
| |
| |
| function validateEcmaVersion(ecmaVersion) { |
| if (isUndefined(ecmaVersion)) { |
| throw new TypeError( |
| 'Key "ecmaVersion": Expected an "ecmaVersion" property.', |
| ); |
| } |
|
|
| if (typeof ecmaVersion !== "number" && ecmaVersion !== "latest") { |
| throw new TypeError( |
| 'Key "ecmaVersion": Expected a number or "latest".', |
| ); |
| } |
| } |
|
|
| |
| |
| |
| |
| |
| |
| function validateSourceType(sourceType) { |
| if ( |
| typeof sourceType !== "string" || |
| !/^(?:script|module|commonjs)$/u.test(sourceType) |
| ) { |
| throw new TypeError( |
| 'Key "sourceType": Expected "script", "module", or "commonjs".', |
| ); |
| } |
| } |
|
|
| |
| |
| |
| |
| |
| |
| function validateGlobals(globals) { |
| if (!isNonArrayObject(globals)) { |
| throw new TypeError('Key "globals": Expected an object.'); |
| } |
|
|
| for (const key of Object.keys(globals)) { |
| |
| if (key === "__proto__") { |
| continue; |
| } |
|
|
| if (key !== key.trim()) { |
| throw new TypeError( |
| `Key "globals": Global "${key}" has leading or trailing whitespace.`, |
| ); |
| } |
|
|
| if (!globalVariablesValues.has(globals[key])) { |
| throw new TypeError( |
| `Key "globals": Key "${key}": Expected "readonly", "writable", or "off".`, |
| ); |
| } |
| } |
| } |
|
|
| |
| |
| |
| |
| |
| |
| function validateParser(parser) { |
| if ( |
| !parser || |
| typeof parser !== "object" || |
| (typeof parser.parse !== "function" && |
| typeof parser.parseForESLint !== "function") |
| ) { |
| throw new TypeError( |
| 'Key "parser": Expected object with parse() or parseForESLint() method.', |
| ); |
| } |
| } |
|
|
| |
| |
| |
| |
| |
| |
| function validateLanguageOptions(languageOptions) { |
| if (!isNonArrayObject(languageOptions)) { |
| throw new TypeError("Expected an object."); |
| } |
|
|
| const { |
| ecmaVersion, |
| sourceType, |
| globals, |
| parser, |
| parserOptions, |
| ...otherOptions |
| } = languageOptions; |
|
|
| if ("ecmaVersion" in languageOptions) { |
| validateEcmaVersion(ecmaVersion); |
| } |
|
|
| if ("sourceType" in languageOptions) { |
| validateSourceType(sourceType); |
| } |
|
|
| if ("globals" in languageOptions) { |
| validateGlobals(globals); |
| } |
|
|
| if ("parser" in languageOptions) { |
| validateParser(parser); |
| } |
|
|
| if ("parserOptions" in languageOptions) { |
| if (!isNonArrayObject(parserOptions)) { |
| throw new TypeError('Key "parserOptions": Expected an object.'); |
| } |
| } |
|
|
| const otherOptionKeys = Object.keys(otherOptions); |
|
|
| if (otherOptionKeys.length > 0) { |
| throw new TypeError(`Unexpected key "${otherOptionKeys[0]}" found.`); |
| } |
| } |
|
|
| module.exports = { validateLanguageOptions }; |
|
|