Skip to content

Commit

Permalink
feat(extract-component): added extraction to the same file
Browse files Browse the repository at this point in the history
  • Loading branch information
Boris Litvinsky committed Mar 26, 2019
1 parent 32c71f6 commit 63911e7
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 5 deletions.
11 changes: 6 additions & 5 deletions src/modules/extract-to-component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { importReactIfNeeded } from "./jsx";

import { appendSelectedTextToFile, prependImportsToFileIfNeeded, replaceSelectionWith, switchToDestinationFileIfRequired, handleError, ProcessedSelection } from "../code-actions";
import * as t from "@babel/types";
import { persistFileSystemChanges } from "../file-system";
import { persistFileSystemChanges, appendTextToFile } from "../file-system";
import { jsxToAst } from "../parsing";
import { capitalizeFirstLetter } from "../utils";
import { transformFromAst } from "@babel/core";
Expand Down Expand Up @@ -45,9 +45,8 @@ export function wrapWithComponent(componentName, jsx): ProcessedSelection {
}
},
MemberExpression(path) {
if (!path.node.wasVisited) {
if (!path.node.wasVisited && t.isThisExpression(path.node.object.object)) {
if (
t.isThisExpression(path.node.object.object) &&
(path.node.object.property.name === "props" ||
path.node.object.property.name === "state")
) {
Expand All @@ -58,11 +57,13 @@ export function wrapWithComponent(componentName, jsx): ProcessedSelection {
path.node.object.property.name = "props";
componentProperties.state.add(path.node.property.name);
}

path.replaceWith(t.identifier(path.node.property.name));

} else {
componentProperties.componentMembers.add(path.node.property.name);
}
path.node.wasVisited = true;
path.replaceWith(t.identifier(path.node.property.name));
path.skip();
}
}
Expand Down Expand Up @@ -140,7 +141,7 @@ export async function extractJSXToComponent() {
const componentName = await showInputBox(null, 'Select Component Name');

const selectionProccessingResult = await wrapWithComponent(componentName, selectedText());
await appendSelectedTextToFile(selectionProccessingResult, activeFileName());
await appendTextToFile(selectionProccessingResult.text, activeFileName());
const componentInstance = createComponentInstance(selectionProccessingResult.metadata.name, selectionProccessingResult.metadata.componentProperties);
await persistFileSystemChanges(replaceSelectionWith(componentInstance));
} catch (e) {
Expand Down
2 changes: 2 additions & 0 deletions src/modules/stateful-to-stateless.ts
Original file line number Diff line number Diff line change
Expand Up @@ -259,6 +259,8 @@ export function statefulToStateless(component) {
// lifecycleEffectHook.expression.arguments.push(t.arrayExpression([]));
// }

lifecycleEffectHook.expression.arguments.push(t.arrayExpression([]));

functionBody.unshift(lifecycleEffectHook);
}

Expand Down

0 comments on commit 63911e7

Please sign in to comment.