diff --git a/lib/util.js b/lib/util.js index facacc7..3adfcad 100644 --- a/lib/util.js +++ b/lib/util.js @@ -75,7 +75,7 @@ exports.deepMerge = deepMerge; * @param {any[]} userOptions the user opts * @returns {TOptions} the options with defaults */ -function applyDefault(defaultOptions, userOptions) { +exports.applyDefault = (defaultOptions, userOptions) => { // clone defaults const options = JSON.parse(JSON.stringify(defaultOptions)); @@ -97,15 +97,29 @@ function applyDefault(defaultOptions, userOptions) { }); return options; -} -exports.applyDefault = applyDefault; +}; /** * Upper cases the first character or the string * @param {string} str a string * @returns {string} upper case first */ -function upperCaseFirst(str) { - return str[0].toUpperCase() + str.slice(1); -} -exports.upperCaseFirst = upperCaseFirst; +exports.upperCaseFirst = str => str[0].toUpperCase() + str.slice(1); + +/** + * Try to retrieve typescript parser service from context + * @param {RuleContext} context Rule context + * @returns {{esTreeNodeToTSNodeMap}|{program}|Object|*} parserServices + */ +exports.getParserServices = context => { + if ( + !context.parserServices || + !context.parserServices.program || + !context.parserServices.esTreeNodeToTSNodeMap + ) { + throw new Error( + "This rule requires you to use `typescript-eslint-parser`." + ); + } + return context.parserServices; +};