|
| 1 | +import pako from "pako" |
| 2 | +import defaultCode from "./app-state/default-code.js" |
| 3 | +import defaultConfig from "./app-state/default-config.js" |
| 4 | +import { linter, ruleCategories } from "./app-state/eslint.js" |
| 5 | + |
| 6 | +/** |
| 7 | + * Convert an Unicode string to base64. |
| 8 | + * @param {string} text The string to convert. |
| 9 | + * @returns {string} Base64 string. |
| 10 | + * @see https://developer.mozilla.org/en-US/docs/Web/API/WindowOrWorkerGlobalScope/btoa#Unicode_strings |
| 11 | + */ |
| 12 | +function encodeToBase64(text) { |
| 13 | + return window.btoa(unescape(encodeURIComponent(text))) |
| 14 | +} |
| 15 | + |
| 16 | +/** |
| 17 | + * Convert a base64 string to Unicode. |
| 18 | + * @param {string} base64 The string to convert. |
| 19 | + * @returns {string} Unicode string. |
| 20 | + * @see https://developer.mozilla.org/en-US/docs/Web/API/WindowOrWorkerGlobalScope/btoa#Unicode_strings |
| 21 | + */ |
| 22 | +function decodeFromBase64(base64) { |
| 23 | + return decodeURIComponent(escape(window.atob(base64))) |
| 24 | +} |
| 25 | + |
| 26 | +/** |
| 27 | + * The state object for this application. |
| 28 | + */ |
| 29 | +export default class PlaygroundState { |
| 30 | + /** |
| 31 | + * Initialize this state. |
| 32 | + * @param {string} [serializedString] A serialized string to restore the state. |
| 33 | + */ |
| 34 | + constructor(serializedString = "") { |
| 35 | + const code = defaultCode |
| 36 | + const config = Object.assign({}, defaultConfig) |
| 37 | + config.rules = Object.assign({}, defaultConfig.rules) |
| 38 | + |
| 39 | + this.code = code |
| 40 | + this.config = config |
| 41 | + this.indentSize = 2 |
| 42 | + this.indentType = "space" |
| 43 | + this.editorType = "codeAndFixedCode" |
| 44 | + this.messages = [] |
| 45 | + this.fixedCode = code |
| 46 | + this.fixedMessages = [] |
| 47 | + |
| 48 | + const deserialized = |
| 49 | + typeof serializedString === "string" && |
| 50 | + this.deserialize(serializedString) |
| 51 | + |
| 52 | + // Ensure the correct messages, fixedCode, and fixedMessages. |
| 53 | + if (!deserialized) { |
| 54 | + this.lint() |
| 55 | + } |
| 56 | + } |
| 57 | + |
| 58 | + /** |
| 59 | + * Enabled rules. |
| 60 | + * @returns {object} The map-like object. Keys are the ID of enabled rules. Every value is 2. |
| 61 | + */ |
| 62 | + get _enabledRules() { |
| 63 | + const allRules = this.config.rules |
| 64 | + return Object.keys(allRules).reduce( |
| 65 | + (map, id) => { |
| 66 | + if (allRules[id] === 2) { |
| 67 | + map[id] = 2 |
| 68 | + } |
| 69 | + return map |
| 70 | + }, |
| 71 | + {} |
| 72 | + ) |
| 73 | + } |
| 74 | + |
| 75 | + /** |
| 76 | + * Actual config object to lint. |
| 77 | + * @type {object} |
| 78 | + */ |
| 79 | + get _configToLint() { |
| 80 | + // Adjust the indentation options to the editor settings. |
| 81 | + const config = Object.assign({}, this.config) |
| 82 | + const rules = config.rules = Object.assign({}, this.config.rules) |
| 83 | + const indentType = (this.indentType === "space") ? this.indentSize : "tab" |
| 84 | + rules.indent = [rules.indent, indentType] |
| 85 | + rules["vue/html-indent"] = [rules["vue/html-indent"], indentType] |
| 86 | + |
| 87 | + return config |
| 88 | + } |
| 89 | + |
| 90 | + /** |
| 91 | + * Serialize this state as a base64 string. |
| 92 | + * @returns {string} The serialized string. |
| 93 | + */ |
| 94 | + serialize() { |
| 95 | + const jsonString = JSON.stringify({ |
| 96 | + code: this.code, |
| 97 | + rules: this._enabledRules, |
| 98 | + indentSize: this.indentSize, |
| 99 | + indentType: this.indentType, |
| 100 | + editorType: this.editorType, |
| 101 | + }) |
| 102 | + const compressedString = pako.deflate(jsonString, { to: "string" }) |
| 103 | + |
| 104 | + return encodeToBase64(compressedString) |
| 105 | + } |
| 106 | + |
| 107 | + /** |
| 108 | + * Deserialize a given serialized string then update this object. |
| 109 | + * @param {string} serializedString A serialized string. |
| 110 | + * @returns {boolean} `true` if this state object was changed. |
| 111 | + */ |
| 112 | + deserialize(serializedString) { //eslint-disable-line complexity |
| 113 | + if (serializedString === "") { |
| 114 | + return false |
| 115 | + } |
| 116 | + try { |
| 117 | + // For backward compatibility, it can address non-compressed data. |
| 118 | + const compressed = !serializedString.startsWith("eyJj") |
| 119 | + const decodedText = decodeFromBase64(serializedString) |
| 120 | + const jsonText = compressed ? pako.inflate(decodedText, { to: "string" }) : decodedText |
| 121 | + const json = JSON.parse(jsonText) |
| 122 | + let changed = false |
| 123 | + |
| 124 | + if (typeof json === "object" && json !== null) { |
| 125 | + if (typeof json.code === "string" && json.code !== this.code) { |
| 126 | + this.code = json.code |
| 127 | + changed = true |
| 128 | + } |
| 129 | + if (typeof json.rules === "object" && json.rules !== null) { |
| 130 | + for (const id of Object.keys(this.config.rules)) { |
| 131 | + const value = json.rules[id] ? 2 : 0 |
| 132 | + if (value !== this.config.rules[id]) { |
| 133 | + this.config.rules[id] = value |
| 134 | + changed = true |
| 135 | + } |
| 136 | + } |
| 137 | + } |
| 138 | + } |
| 139 | + if ((json.indentSize === 2 || json.indentSize === 4 || json.indentSize === 8) && json.indentSize !== this.indentSize) { |
| 140 | + this.indentSize = json.indentSize |
| 141 | + changed = true |
| 142 | + } |
| 143 | + if ((json.indentType === "space" || json.indentType === "tab") && json.indentType !== this.indentType) { |
| 144 | + this.indentType = json.indentType |
| 145 | + changed = true |
| 146 | + } |
| 147 | + if ((json.editorType === "codeAndFixedCode" || json.editorType === "codeOnly") && json.editorType !== this.editorType) { |
| 148 | + this.editorType = json.editorType |
| 149 | + changed = true |
| 150 | + } |
| 151 | + |
| 152 | + // Update messages, fixedCode, and fixedMessages. |
| 153 | + if (changed) { |
| 154 | + this.lint() |
| 155 | + } |
| 156 | + |
| 157 | + return changed |
| 158 | + } |
| 159 | + catch (error) { |
| 160 | + console.error(error) //eslint-disable-line no-console |
| 161 | + return false |
| 162 | + } |
| 163 | + } |
| 164 | + |
| 165 | + /** |
| 166 | + * Execute ESLint to update messages, fixedCode, and fixedMessages. |
| 167 | + * @returns {void} |
| 168 | + */ |
| 169 | + lint() { |
| 170 | + const config = this._configToLint |
| 171 | + |
| 172 | + // Fix |
| 173 | + try { |
| 174 | + // At first, fix because `linter.verifyAndFix` does not accept SourceCode object. |
| 175 | + const ret = linter.verifyAndFix(this.code, config, "vue-eslint-demo.vue") |
| 176 | + this.fixedCode = ret.output |
| 177 | + this.fixedMessages = ret.messages |
| 178 | + } |
| 179 | + catch (err) { |
| 180 | + this.fixedCode = this.code |
| 181 | + this.fixedMessages = [{ |
| 182 | + fatal: true, |
| 183 | + severity: 2, |
| 184 | + message: err.message, |
| 185 | + line: 1, |
| 186 | + column: 0, |
| 187 | + }] |
| 188 | + } |
| 189 | + |
| 190 | + // Lint |
| 191 | + try { |
| 192 | + this.messages = linter.verify( |
| 193 | + // Cannot reuse until https://github.com/eslint/eslint/pull/8755 is merged. |
| 194 | + // linter.getSourceCode(), // Reuse the AST of the previous `linter.verifyAndFix`. |
| 195 | + this.code, |
| 196 | + config, |
| 197 | + "vue-eslint-demo.vue" |
| 198 | + ) |
| 199 | + } |
| 200 | + catch (err) { |
| 201 | + this.messages = [{ |
| 202 | + fatal: true, |
| 203 | + severity: 2, |
| 204 | + message: err.message, |
| 205 | + line: 1, |
| 206 | + column: 0, |
| 207 | + }] |
| 208 | + } |
| 209 | + } |
| 210 | +} |
| 211 | + |
| 212 | +export { ruleCategories } |
0 commit comments