|
| 1 | +/* |
| 2 | + * Copyright 2024 Adobe. All rights reserved. |
| 3 | + * This file is licensed to you under the Apache License, Version 2.0 (the "License"); |
| 4 | + * you may not use this file except in compliance with the License. You may obtain a copy |
| 5 | + * of the License at http://www.apache.org/licenses/LICENSE-2.0 |
| 6 | + * |
| 7 | + * Unless required by applicable law or agreed to in writing, software distributed under |
| 8 | + * the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS |
| 9 | + * OF ANY KIND, either express or implied. See the License for the specific language |
| 10 | + * governing permissions and limitations under the License. |
| 11 | + */ |
| 12 | + |
| 13 | +module.exports = function () { |
| 14 | + /** @param info {import('typescript').server.PluginCreateInfo} */ |
| 15 | + function create(info) { |
| 16 | + const proxy = Object.create(null); |
| 17 | + for (let k of Object.keys(info.languageService)) { |
| 18 | + const x = info.languageService[k]; |
| 19 | + proxy[k] = (...args) => x.apply(info.languageService, args); |
| 20 | + } |
| 21 | + |
| 22 | + proxy.getCompletionEntryDetails = (...args) => { |
| 23 | + let result = info.languageService.getCompletionEntryDetails(...args); |
| 24 | + if (!result.codeActions) { |
| 25 | + return result; |
| 26 | + } |
| 27 | + |
| 28 | + // Override auto import of style macro to add `with {type: 'macro'}` automatically. |
| 29 | + for (let action of result.codeActions) { |
| 30 | + for (let change of action.changes) { |
| 31 | + for (let textChange of change.textChanges) { |
| 32 | + if (change.fileName.includes('@react-spectrum/s2')) { |
| 33 | + // For files inside S2 itself, import specifier will be '../style', not '@react-spectrum/s2/style'. |
| 34 | + textChange.newText = textChange.newText.replace(/(import\s*\{.*?\}\s*from\s*['"]\.\.\/style['"]);/g, '$1 with {type: \'macro\'};'); |
| 35 | + } else { |
| 36 | + textChange.newText = textChange.newText.replace(/(import\s*\{.*?\}\s*from\s*['"]@react-spectrum\/s2\/style['"]);/g, '$1 with {type: \'macro\'};'); |
| 37 | + } |
| 38 | + } |
| 39 | + } |
| 40 | + } |
| 41 | + |
| 42 | + return result; |
| 43 | + }; |
| 44 | + |
| 45 | + return proxy; |
| 46 | + } |
| 47 | + |
| 48 | + return {create}; |
| 49 | +}; |
0 commit comments