.gitignore added
[dotfiles/.git] / .config / coc / extensions / node_modules / coc-prettier / node_modules / postcss-selector-parser / dist / processor.js
1 "use strict";
2
3 exports.__esModule = true;
4
5 var _parser = require("./parser");
6
7 var _parser2 = _interopRequireDefault(_parser);
8
9 function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
10
11 function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
12
13 var Processor = function () {
14     function Processor(func, options) {
15         _classCallCheck(this, Processor);
16
17         this.func = func || function noop() {};
18         this.funcRes = null;
19         this.options = options;
20     }
21
22     Processor.prototype._shouldUpdateSelector = function _shouldUpdateSelector(rule) {
23         var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
24
25         var merged = Object.assign({}, this.options, options);
26         if (merged.updateSelector === false) {
27             return false;
28         } else {
29             return typeof rule !== "string";
30         }
31     };
32
33     Processor.prototype._isLossy = function _isLossy() {
34         var options = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
35
36         var merged = Object.assign({}, this.options, options);
37         if (merged.lossless === false) {
38             return true;
39         } else {
40             return false;
41         }
42     };
43
44     Processor.prototype._root = function _root(rule) {
45         var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
46
47         var parser = new _parser2.default(rule, this._parseOptions(options));
48         return parser.root;
49     };
50
51     Processor.prototype._parseOptions = function _parseOptions(options) {
52         return {
53             lossy: this._isLossy(options)
54         };
55     };
56
57     Processor.prototype._run = function _run(rule) {
58         var _this = this;
59
60         var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
61
62         return new Promise(function (resolve, reject) {
63             try {
64                 var root = _this._root(rule, options);
65                 Promise.resolve(_this.func(root)).then(function (transform) {
66                     var string = undefined;
67                     if (_this._shouldUpdateSelector(rule, options)) {
68                         string = root.toString();
69                         rule.selector = string;
70                     }
71                     return { transform: transform, root: root, string: string };
72                 }).then(resolve, reject);
73             } catch (e) {
74                 reject(e);
75                 return;
76             }
77         });
78     };
79
80     Processor.prototype._runSync = function _runSync(rule) {
81         var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
82
83         var root = this._root(rule, options);
84         var transform = this.func(root);
85         if (transform && typeof transform.then === "function") {
86             throw new Error("Selector processor returned a promise to a synchronous call.");
87         }
88         var string = undefined;
89         if (options.updateSelector && typeof rule !== "string") {
90             string = root.toString();
91             rule.selector = string;
92         }
93         return { transform: transform, root: root, string: string };
94     };
95
96     /**
97      * Process rule into a selector AST.
98      * 
99      * @param rule {postcss.Rule | string} The css selector to be processed
100      * @param options The options for processing
101      * @returns {Promise<parser.Root>} The AST of the selector after processing it.
102      */
103
104
105     Processor.prototype.ast = function ast(rule, options) {
106         return this._run(rule, options).then(function (result) {
107             return result.root;
108         });
109     };
110
111     /**
112      * Process rule into a selector AST synchronously.
113      * 
114      * @param rule {postcss.Rule | string} The css selector to be processed
115      * @param options The options for processing
116      * @returns {parser.Root} The AST of the selector after processing it.
117      */
118
119
120     Processor.prototype.astSync = function astSync(rule, options) {
121         return this._runSync(rule, options).root;
122     };
123
124     /**
125      * Process a selector into a transformed value asynchronously
126      * 
127      * @param rule {postcss.Rule | string} The css selector to be processed
128      * @param options The options for processing
129      * @returns {Promise<any>} The value returned by the processor.
130      */
131
132
133     Processor.prototype.transform = function transform(rule, options) {
134         return this._run(rule, options).then(function (result) {
135             return result.transform;
136         });
137     };
138
139     /**
140      * Process a selector into a transformed value synchronously.
141      * 
142      * @param rule {postcss.Rule | string} The css selector to be processed
143      * @param options The options for processing
144      * @returns {any} The value returned by the processor.
145      */
146
147
148     Processor.prototype.transformSync = function transformSync(rule, options) {
149         return this._runSync(rule, options).transform;
150     };
151
152     /**
153      * Process a selector into a new selector string asynchronously.
154      * 
155      * @param rule {postcss.Rule | string} The css selector to be processed
156      * @param options The options for processing
157      * @returns {string} the selector after processing.
158      */
159
160
161     Processor.prototype.process = function process(rule, options) {
162         return this._run(rule, options).then(function (result) {
163             return result.string || result.root.toString();
164         });
165     };
166
167     /**
168      * Process a selector into a new selector string synchronously.
169      * 
170      * @param rule {postcss.Rule | string} The css selector to be processed
171      * @param options The options for processing
172      * @returns {string} the selector after processing.
173      */
174
175
176     Processor.prototype.processSync = function processSync(rule, options) {
177         var result = this._runSync(rule, options);
178         return result.string || result.root.toString();
179     };
180
181     return Processor;
182 }();
183
184 exports.default = Processor;
185 module.exports = exports["default"];