Skip to content
This repository was archived by the owner on Jan 19, 2019. It is now read-only.

Commit b08576e

Browse files
committed
[FEAT] Add getParserServices function from #245
1 parent 4d85404 commit b08576e

File tree

1 file changed

+21
-7
lines changed

1 file changed

+21
-7
lines changed

lib/util.js

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ exports.deepMerge = deepMerge;
7575
* @param {any[]} userOptions the user opts
7676
* @returns {TOptions} the options with defaults
7777
*/
78-
function applyDefault(defaultOptions, userOptions) {
78+
exports.applyDefault = (defaultOptions, userOptions) => {
7979
// clone defaults
8080
const options = JSON.parse(JSON.stringify(defaultOptions));
8181

@@ -97,15 +97,29 @@ function applyDefault(defaultOptions, userOptions) {
9797
});
9898

9999
return options;
100-
}
101-
exports.applyDefault = applyDefault;
100+
};
102101

103102
/**
104103
* Upper cases the first character or the string
105104
* @param {string} str a string
106105
* @returns {string} upper case first
107106
*/
108-
function upperCaseFirst(str) {
109-
return str[0].toUpperCase() + str.slice(1);
110-
}
111-
exports.upperCaseFirst = upperCaseFirst;
107+
exports.upperCaseFirst = str => str[0].toUpperCase() + str.slice(1);
108+
109+
/**
110+
* Try to retrieve typescript parser service from context
111+
* @param {RuleContext} context Rule context
112+
* @returns {{esTreeNodeToTSNodeMap}|{program}|Object|*} parserServices
113+
*/
114+
exports.getParserServices = context => {
115+
if (
116+
!context.parserServices ||
117+
!context.parserServices.program ||
118+
!context.parserServices.esTreeNodeToTSNodeMap
119+
) {
120+
throw new Error(
121+
"This rule requires you to use `typescript-eslint-parser`."
122+
);
123+
}
124+
return context.parserServices;
125+
};

0 commit comments

Comments
 (0)