Skip to content

Commit

Permalink
TypeScript plugin to add macro import attribute to auto imports (#7141)
Browse files Browse the repository at this point in the history
* TypeScript plugin to add macro import attribute to auto imports

* fix lockfile
  • Loading branch information
devongovett authored Oct 10, 2024
1 parent 454da05 commit 8a7cb15
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 0 deletions.
16 changes: 16 additions & 0 deletions packages/dev/ts-plugin/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"name": "@react-spectrum/ts-plugin",
"version": "0.1.0",
"main": "./src/index.js",
"license": "Apache-2.0",
"repository": {
"type": "git",
"url": "https://github.com/adobe/react-spectrum"
},
"publishConfig": {
"access": "public"
},
"rsp": {
"type": "cli"
}
}
49 changes: 49 additions & 0 deletions packages/dev/ts-plugin/src/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
/*
* Copyright 2024 Adobe. All rights reserved.
* This file is licensed to you under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. You may obtain a copy
* of the License at http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under
* the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS
* OF ANY KIND, either express or implied. See the License for the specific language
* governing permissions and limitations under the License.
*/

module.exports = function () {
/** @param info {import('typescript').server.PluginCreateInfo} */
function create(info) {
const proxy = Object.create(null);
for (let k of Object.keys(info.languageService)) {
const x = info.languageService[k];
proxy[k] = (...args) => x.apply(info.languageService, args);
}

proxy.getCompletionEntryDetails = (...args) => {
let result = info.languageService.getCompletionEntryDetails(...args);
if (!result.codeActions) {
return result;
}

// Override auto import of style macro to add `with {type: 'macro'}` automatically.
for (let action of result.codeActions) {
for (let change of action.changes) {
for (let textChange of change.textChanges) {
if (change.fileName.includes('@react-spectrum/s2')) {
// For files inside S2 itself, import specifier will be '../style', not '@react-spectrum/s2/style'.
textChange.newText = textChange.newText.replace(/(import\s*\{.*?\}\s*from\s*['"]\.\.\/style['"]);/g, '$1 with {type: \'macro\'};');
} else {
textChange.newText = textChange.newText.replace(/(import\s*\{.*?\}\s*from\s*['"]@react-spectrum\/s2\/style['"]);/g, '$1 with {type: \'macro\'};');
}
}
}
}

return result;
};

return proxy;
}

return {create};
};
3 changes: 3 additions & 0 deletions tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,9 @@
"skipLibCheck": false,
"strict": false,
"plugins": [
{
"name": "@react-spectrum/ts-plugin"
},
{
"name": "typescript-strict-plugin",
"paths": [
Expand Down
6 changes: 6 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -8028,6 +8028,12 @@ __metadata:
languageName: unknown
linkType: soft

"@react-spectrum/ts-plugin@workspace:packages/dev/ts-plugin":
version: 0.0.0-use.local
resolution: "@react-spectrum/ts-plugin@workspace:packages/dev/ts-plugin"
languageName: unknown
linkType: soft

"@react-spectrum/utils@npm:^3.1.0, @react-spectrum/utils@npm:^3.11.11, @react-spectrum/utils@npm:^3.8.1, @react-spectrum/utils@workspace:packages/@react-spectrum/utils":
version: 0.0.0-use.local
resolution: "@react-spectrum/utils@workspace:packages/@react-spectrum/utils"
Expand Down

1 comment on commit 8a7cb15

@rspbot
Copy link

@rspbot rspbot commented on 8a7cb15 Oct 10, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.