Skip to content

Commit 8a7cb15

Browse files
authored
TypeScript plugin to add macro import attribute to auto imports (#7141)
* TypeScript plugin to add macro import attribute to auto imports * fix lockfile
1 parent 454da05 commit 8a7cb15

File tree

4 files changed

+74
-0
lines changed

4 files changed

+74
-0
lines changed

packages/dev/ts-plugin/package.json

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
{
2+
"name": "@react-spectrum/ts-plugin",
3+
"version": "0.1.0",
4+
"main": "./src/index.js",
5+
"license": "Apache-2.0",
6+
"repository": {
7+
"type": "git",
8+
"url": "https://github.com/adobe/react-spectrum"
9+
},
10+
"publishConfig": {
11+
"access": "public"
12+
},
13+
"rsp": {
14+
"type": "cli"
15+
}
16+
}

packages/dev/ts-plugin/src/index.js

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
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+
};

tsconfig.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,9 @@
3535
"skipLibCheck": false,
3636
"strict": false,
3737
"plugins": [
38+
{
39+
"name": "@react-spectrum/ts-plugin"
40+
},
3841
{
3942
"name": "typescript-strict-plugin",
4043
"paths": [

yarn.lock

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8028,6 +8028,12 @@ __metadata:
80288028
languageName: unknown
80298029
linkType: soft
80308030

8031+
"@react-spectrum/ts-plugin@workspace:packages/dev/ts-plugin":
8032+
version: 0.0.0-use.local
8033+
resolution: "@react-spectrum/ts-plugin@workspace:packages/dev/ts-plugin"
8034+
languageName: unknown
8035+
linkType: soft
8036+
80318037
"@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":
80328038
version: 0.0.0-use.local
80338039
resolution: "@react-spectrum/utils@workspace:packages/@react-spectrum/utils"

0 commit comments

Comments
 (0)