Giant blob of minor changes
[dotfiles/.git] / .config / coc / extensions / node_modules / coc-prettier / node_modules / @typescript-eslint / experimental-utils / dist / eslint-utils / RuleTester.js
1 "use strict";
2 var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3     if (k2 === undefined) k2 = k;
4     Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
5 }) : (function(o, m, k, k2) {
6     if (k2 === undefined) k2 = k;
7     o[k2] = m[k];
8 }));
9 var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
10     Object.defineProperty(o, "default", { enumerable: true, value: v });
11 }) : function(o, v) {
12     o["default"] = v;
13 });
14 var __importStar = (this && this.__importStar) || function (mod) {
15     if (mod && mod.__esModule) return mod;
16     var result = {};
17     if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
18     __setModuleDefault(result, mod);
19     return result;
20 };
21 var __classPrivateFieldSet = (this && this.__classPrivateFieldSet) || function (receiver, privateMap, value) {
22     if (!privateMap.has(receiver)) {
23         throw new TypeError("attempted to set private field on non-instance");
24     }
25     privateMap.set(receiver, value);
26     return value;
27 };
28 var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, privateMap) {
29     if (!privateMap.has(receiver)) {
30         throw new TypeError("attempted to get private field on non-instance");
31     }
32     return privateMap.get(receiver);
33 };
34 var _options;
35 Object.defineProperty(exports, "__esModule", { value: true });
36 exports.RuleTester = exports.noFormat = void 0;
37 const path = __importStar(require("path"));
38 const TSESLint = __importStar(require("../ts-eslint"));
39 const parser = '@typescript-eslint/parser';
40 class RuleTester extends TSESLint.RuleTester {
41     // as of eslint 6 you have to provide an absolute path to the parser
42     // but that's not as clean to type, this saves us trying to manually enforce
43     // that contributors require.resolve everything
44     constructor(options) {
45         var _a, _b;
46         super(Object.assign(Object.assign({}, options), { parserOptions: Object.assign(Object.assign({}, options.parserOptions), { warnOnUnsupportedTypeScriptVersion: (_b = (_a = options.parserOptions) === null || _a === void 0 ? void 0 : _a.warnOnUnsupportedTypeScriptVersion) !== null && _b !== void 0 ? _b : false }), parser: require.resolve(options.parser) }));
47         _options.set(this, void 0);
48         __classPrivateFieldSet(this, _options, options);
49         // make sure that the parser doesn't hold onto file handles between tests
50         // on linux (i.e. our CI env), there can be very a limited number of watch handles available
51         afterAll(() => {
52             try {
53                 // instead of creating a hard dependency, just use a soft require
54                 // a bit weird, but if they're using this tooling, it'll be installed
55                 require(parser).clearCaches();
56             }
57             catch (_a) {
58                 // ignored
59             }
60         });
61     }
62     getFilename(options) {
63         var _a;
64         if (options) {
65             const filename = `file.ts${((_a = options.ecmaFeatures) === null || _a === void 0 ? void 0 : _a.jsx) ? 'x' : ''}`;
66             if (options.project) {
67                 return path.join(options.tsconfigRootDir != null
68                     ? options.tsconfigRootDir
69                     : process.cwd(), filename);
70             }
71             return filename;
72         }
73         else if (__classPrivateFieldGet(this, _options).parserOptions) {
74             return this.getFilename(__classPrivateFieldGet(this, _options).parserOptions);
75         }
76         return 'file.ts';
77     }
78     // as of eslint 6 you have to provide an absolute path to the parser
79     // If you don't do that at the test level, the test will fail somewhat cryptically...
80     // This is a lot more explicit
81     run(name, rule, testsReadonly) {
82         const errorMessage = `Do not set the parser at the test level unless you want to use a parser other than ${parser}`;
83         const tests = Object.assign({}, testsReadonly);
84         // standardize the valid tests as objects
85         tests.valid = tests.valid.map(test => {
86             if (typeof test === 'string') {
87                 return {
88                     code: test,
89                 };
90             }
91             return test;
92         });
93         tests.valid = tests.valid.map(test => {
94             if (typeof test !== 'string') {
95                 if (test.parser === parser) {
96                     throw new Error(errorMessage);
97                 }
98                 if (!test.filename) {
99                     return Object.assign(Object.assign({}, test), { filename: this.getFilename(test.parserOptions) });
100                 }
101             }
102             return test;
103         });
104         tests.invalid = tests.invalid.map(test => {
105             if (test.parser === parser) {
106                 throw new Error(errorMessage);
107             }
108             if (!test.filename) {
109                 return Object.assign(Object.assign({}, test), { filename: this.getFilename(test.parserOptions) });
110             }
111             return test;
112         });
113         super.run(name, rule, tests);
114     }
115 }
116 exports.RuleTester = RuleTester;
117 _options = new WeakMap();
118 /**
119  * Simple no-op tag to mark code samples as "should not format with prettier"
120  *   for the internal/plugin-test-formatting lint rule
121  */
122 function noFormat(strings, ...keys) {
123     const lastIndex = strings.length - 1;
124     return (strings.slice(0, lastIndex).reduce((p, s, i) => p + s + keys[i], '') +
125         strings[lastIndex]);
126 }
127 exports.noFormat = noFormat;
128 //# sourceMappingURL=RuleTester.js.map