File size: 4,687 Bytes
443c22e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
/**
 * @fileoverview The schema to validate language options
 * @author Nicholas C. Zakas
 */

"use strict";

//-----------------------------------------------------------------------------
// Data
//-----------------------------------------------------------------------------

const globalVariablesValues = new Set([
	true,
	"true",
	"writable",
	"writeable",
	false,
	"false",
	"readonly",
	"readable",
	null,
	"off",
]);

//------------------------------------------------------------------------------
// Helpers
//------------------------------------------------------------------------------

/**
 * Check if a value is a non-null object.
 * @param {any} value The value to check.
 * @returns {boolean} `true` if the value is a non-null object.
 */
function isNonNullObject(value) {
	return typeof value === "object" && value !== null;
}

/**
 * Check if a value is a non-null non-array object.
 * @param {any} value The value to check.
 * @returns {boolean} `true` if the value is a non-null non-array object.
 */
function isNonArrayObject(value) {
	return isNonNullObject(value) && !Array.isArray(value);
}

/**
 * Check if a value is undefined.
 * @param {any} value The value to check.
 * @returns {boolean} `true` if the value is undefined.
 */
function isUndefined(value) {
	return typeof value === "undefined";
}

//-----------------------------------------------------------------------------
// Schemas
//-----------------------------------------------------------------------------

/**
 * Validates the ecmaVersion property.
 * @param {string|number} ecmaVersion The value to check.
 * @returns {void}
 * @throws {TypeError} If the value is invalid.
 */
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".',
		);
	}
}

/**
 * Validates the sourceType property.
 * @param {string} sourceType The value to check.
 * @returns {void}
 * @throws {TypeError} If the value is invalid.
 */
function validateSourceType(sourceType) {
	if (
		typeof sourceType !== "string" ||
		!/^(?:script|module|commonjs)$/u.test(sourceType)
	) {
		throw new TypeError(
			'Key "sourceType": Expected "script", "module", or "commonjs".',
		);
	}
}

/**
 * Validates the globals property.
 * @param {Object} globals The value to check.
 * @returns {void}
 * @throws {TypeError} If the value is invalid.
 */
function validateGlobals(globals) {
	if (!isNonArrayObject(globals)) {
		throw new TypeError('Key "globals": Expected an object.');
	}

	for (const key of Object.keys(globals)) {
		// avoid hairy edge case
		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".`,
			);
		}
	}
}

/**
 * Validates the parser property.
 * @param {Object} parser The value to check.
 * @returns {void}
 * @throws {TypeError} If the value is invalid.
 */
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.',
		);
	}
}

/**
 * Validates the language options.
 * @param {Object} languageOptions The language options to validate.
 * @returns {void}
 * @throws {TypeError} If the language options are invalid.
 */
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 };