|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const SUPPORTED_VERSIONS = [ |
|
3, |
|
5, |
|
6, |
|
7, |
|
8, |
|
9, |
|
10, |
|
11, |
|
12, |
|
13, |
|
14, |
|
15 |
|
]; |
|
|
|
|
|
|
|
|
|
|
|
export function getLatestEcmaVersion() { |
|
return SUPPORTED_VERSIONS[SUPPORTED_VERSIONS.length - 1]; |
|
} |
|
|
|
|
|
|
|
|
|
|
|
export function getSupportedEcmaVersions() { |
|
return [...SUPPORTED_VERSIONS]; |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
function normalizeEcmaVersion(ecmaVersion = 5) { |
|
|
|
let version = ecmaVersion === "latest" ? getLatestEcmaVersion() : ecmaVersion; |
|
|
|
if (typeof version !== "number") { |
|
throw new Error(`ecmaVersion must be a number or "latest". Received value of type ${typeof ecmaVersion} instead.`); |
|
} |
|
|
|
|
|
|
|
if (version >= 2015) { |
|
version -= 2009; |
|
} |
|
|
|
if (!SUPPORTED_VERSIONS.includes(version)) { |
|
throw new Error("Invalid ecmaVersion."); |
|
} |
|
|
|
return version; |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
function normalizeSourceType(sourceType = "script") { |
|
if (sourceType === "script" || sourceType === "module") { |
|
return sourceType; |
|
} |
|
|
|
if (sourceType === "commonjs") { |
|
return "script"; |
|
} |
|
|
|
throw new Error("Invalid sourceType."); |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
export function normalizeOptions(options) { |
|
const ecmaVersion = normalizeEcmaVersion(options.ecmaVersion); |
|
const sourceType = normalizeSourceType(options.sourceType); |
|
const ranges = options.range === true; |
|
const locations = options.loc === true; |
|
|
|
if (ecmaVersion !== 3 && options.allowReserved) { |
|
|
|
|
|
throw new Error("`allowReserved` is only supported when ecmaVersion is 3"); |
|
} |
|
if (typeof options.allowReserved !== "undefined" && typeof options.allowReserved !== "boolean") { |
|
throw new Error("`allowReserved`, when present, must be `true` or `false`"); |
|
} |
|
const allowReserved = ecmaVersion === 3 ? (options.allowReserved || "never") : false; |
|
const ecmaFeatures = options.ecmaFeatures || {}; |
|
const allowReturnOutsideFunction = options.sourceType === "commonjs" || |
|
Boolean(ecmaFeatures.globalReturn); |
|
|
|
if (sourceType === "module" && ecmaVersion < 6) { |
|
throw new Error("sourceType 'module' is not supported when ecmaVersion < 2015. Consider adding `{ ecmaVersion: 2015 }` to the parser options."); |
|
} |
|
|
|
return Object.assign({}, options, { |
|
ecmaVersion, |
|
sourceType, |
|
ranges, |
|
locations, |
|
allowReserved, |
|
allowReturnOutsideFunction |
|
}); |
|
} |
|
|