Comments (13)
@williamluke4 i havent forgotten about this! Its been a bit hectic. Ill get to reviewing this week! Thank you!
from vscode-glean.
@williamluke4 can you create a branch/fork?
from vscode-glean.
@chiptus good point. Its a bit tricky to implement since you may be exporting the component into an existing file. Ill try implementing this ASAP
from vscode-glean.
can look for the import statement, or otherwise if importing to existing file, don't add the statement
from vscode-glean.
@chiptus yep. Also various version of import need to be taken into account. For example, Typescript users usually do import * as React from 'react'
while babel users do a regular default import.
@chiptus wanna do a PR? Ill be glad to help
from vscode-glean.
It would also be nice to import dependences of the extracted code :) If I have time ill start playing around with this
from vscode-glean.
I'm also interested in this, I can also participate to the PR if needed.
from vscode-glean.
I love the initiative! Ill be super glad to help out and point you guys in the right direction.
I'd start here: https://github.com/wix/vscode-glean/blob/7d6791f9595bb9be4e5619f566c7c67276599d87/src/code-actions.ts#L25
This is baiscally the implementation of the "extract to component" refactoring, and the part where extracted component is added to the target file
from vscode-glean.
Managed to get a prototype working.
This does not import React but finds the required imports from the source file. Relative imports will not work yet
code-actions.ts
await appendSelectedTextToFile(selectionProccessingResult, filePath);
await prependImportsToFileIfNeeded(selectionProccessingResult, filePath);
const destinationImports: string = getImports(selectionProccessingResult.text);
prependTextToFile(destinationImports, filePath);
get-imports.ts
import { jsxToAst } from "./modules/jsx";
import { allText } from "./editor";
import traverse from "@babel/traverse";
import { File } from "@babel/types";
import { transformFromAstSync } from "@babel/core";
export default function getImports(code: string): string {
let imports: Array<string> = [];
let sourceAst: File = jsxToAst(allText());
sourceAst.program.body = sourceAst.program.body.filter(bodyItem => bodyItem.type === "ImportDeclaration");
const visitor = {
JSXIdentifier(path) {
console.log(path);
console.log(path.node.type);
if(path.container.type === "JSXOpeningElement") {
imports = [...imports, path.node.name];
}
}
};
const ast: File = jsxToAst(code);
traverse(ast, visitor);
sourceAst.program.body = sourceAst.program.body.filter(imp => {
imp.specifiers = imp.specifiers.filter(spec => imports.includes(spec.local.name));
if(imp.specifiers.length > 0) { return true; }
return false;
});
const importsFromSource: string = transformFromAstSync(sourceAst).code;
return importsFromSource;
}
from vscode-glean.
@williamluke4 Thanks alot! Im having a bit of a hectic week. Ill have look at it ASAP
from vscode-glean.
hey @williamluke4, we are back in business :) Can you please create a branch?
AFAIK, you want to import all missing imports, not just React?
from vscode-glean.
BTW, there are several nuances that we needed take into account:
When Glean creates a stateful component, it extends Component
. So we need to check if there is already an existing import and either add to it (in case the file already contain another component) or add a new one
@williamluke4
from vscode-glean.
Hey @borislit Will have a go this evening
from vscode-glean.
Related Issues (20)
- Cannot read property `tsAction` of undefined
- Extreme Latency & High Profile Usage HOT 3
- npm install command not working after cloning repo
- Extract to arrow function by default? HOT 1
- Settings: Extract to jsx file instead of tsx
- React Native error: [BABEL] unknown: Preset /* your preset */ requires a filename to be set when babel is called directly. HOT 1
- Extension issue
- Extension issue
- No props validation for Typescript users?
- Extension issue HOT 1
- useEffect refactoring suggestion is overzealous
- When converting from class to function components, useCallback doesn't convert the parameters
- Publish plugin to openVSX
- Prettier cannot run on save HOT 1
- "You must pass a scope and parentPath unless traversing a Program/File. Instead of that you tried to traverse a Identifier node without passing scope and parentPath." Error when converting class to functional component.
- This experimental syntax requires enabling the parser plugin: 'nullishCoalescingOperator' (6:77) HOT 1
- Using Extract Component to File in Client component in NextJS13 breaks it
- Takes too long to save files HOT 1
- Incorrect type. Expected one of string in setting.json
- It doesn't seem to be working HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from vscode-glean.