Skip to content

Commit 056e97f

Browse files
author
Aleksey Okhrimenko
committed
Fix for #3, #5
1 parent 546c73e commit 056e97f

File tree

6 files changed

+65
-19
lines changed

6 files changed

+65
-19
lines changed

.vscode/launch.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
"request": "launch",
2525
"runtimeExecutable": "${execPath}",
2626
"args": [
27+
"--disable-extensions",
2728
"--extensionDevelopmentPath=${workspaceFolder}",
2829
"--extensionTestsPath=${workspaceFolder}/out/test/suite/index"
2930
],

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
# Change Log
22

3+
## 0.1.0
4+
5+
- Get style file extension from `@schematics/angular:component`
6+
- Fix extraction of structural directives (ngFor etc..)
7+
38
## 0.0.5
49

510
- Fix wrong contructor keyword placement

package-lock.json

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"name": "arrr",
33
"displayName": "arrr",
44
"description": "The extension provides refactoring tools for your Angular codebase",
5-
"version": "0.0.5",
5+
"version": "0.1.0",
66
"publisher": "obenjiro",
77
"engines": {
88
"vscode": "^1.47.0"

src/modules/extract-to-folder.ts

Lines changed: 43 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,33 @@
1+
import * as fs from "fs";
2+
import * as path from "path";
13
import * as vscode from "vscode";
2-
import {activeFileName, getSelectedText, getSelectionOffsetRange, importMissingDependencies,} from "../editor";
3-
import {getAllTargets,} from "../template-parser";
4-
import {showFilePicker} from "../file-picker";
5-
import {createFileIfDoesntExist, persistFileSystemChanges, replaceTextInFile,} from "../file-system";
6-
import {pascalCase} from "change-case";
7-
import {appendSelectedTextToFile, replaceSelectionWith,} from "../code-actions";
8-
import {showDirectoryPicker} from "../directories-picker";
9-
import {getComponentInstance, getComponentText, getSpecText,} from "./extract-to-folder-template";
10-
11-
const fs = require("fs");
12-
const path = require("path");
4+
import {
5+
activeFileName,
6+
getSelectedText,
7+
getSelectionOffsetRange,
8+
importMissingDependencies,
9+
} from "../editor";
10+
import { getAllTargets } from "../template-parser";
11+
import { showFilePicker } from "../file-picker";
12+
import {
13+
createFileIfDoesntExist,
14+
persistFileSystemChanges,
15+
replaceTextInFile,
16+
} from "../file-system";
17+
import { pascalCase } from "change-case";
18+
import {
19+
appendSelectedTextToFile,
20+
replaceSelectionWith,
21+
} from "../code-actions";
22+
import { showDirectoryPicker } from "../directories-picker";
23+
import {
24+
getComponentInstance,
25+
getComponentText,
26+
getSpecText,
27+
} from "./extract-to-folder-template";
1328

1429
export async function extractToFolder() {
15-
const {start, end} = getSelectionOffsetRange();
30+
const { start, end } = getSelectionOffsetRange();
1631

1732
if (start && end) {
1833
try {
@@ -31,9 +46,10 @@ export async function extractToFolder() {
3146

3247
const componentName = parts[parts.length - 1];
3348

49+
const styleExt = await getStyleExt();
3450

3551
const htmlFilePath = `${filePath}/${componentName}.component.html`;
36-
const cssFilePath = `${filePath}/${componentName}.component.css`;
52+
const cssFilePath = `${filePath}/${componentName}.component.${styleExt}`;
3753
const tsFilePath = `${filePath}/${componentName}.component.ts`;
3854
const specFilePath = `${filePath}/${componentName}.component.spec.ts`;
3955

@@ -42,14 +58,14 @@ export async function extractToFolder() {
4258
await createFileIfDoesntExist(tsFilePath);
4359
await createFileIfDoesntExist(specFilePath);
4460

45-
await appendSelectedTextToFile({text}, htmlFilePath);
46-
await appendSelectedTextToFile({text: ``}, cssFilePath);
61+
await appendSelectedTextToFile({ text }, htmlFilePath);
62+
await appendSelectedTextToFile({ text: `` }, cssFilePath);
4763
await appendSelectedTextToFile(
48-
{text: getComponentText(componentName, targets)},
64+
{ text: getComponentText(componentName, targets) },
4965
tsFilePath
5066
);
5167
await appendSelectedTextToFile(
52-
{text: getSpecText(componentName)},
68+
{ text: getSpecText(componentName) },
5369
specFilePath
5470
);
5571

@@ -120,3 +136,13 @@ async function getComponentNameFromHtmlFile(filePath) {
120136
return (tsContent.match(/export class\s+([\w_]+)/) || [])[1];
121137
}
122138

139+
async function getStyleExt() {
140+
try {
141+
const [angularJsonPath] = await vscode.workspace.findFiles("angular.json");
142+
const config = JSON.parse(fs.readFileSync(angularJsonPath.path, "utf-8"));
143+
144+
return config.schematics["@schematics/angular:component"].styleext;
145+
} catch (e) {
146+
return "css";
147+
}
148+
}

src/template-parser.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,5 +156,19 @@ export function getAllTargets(text) {
156156
}
157157
);
158158

159+
// removing variables refrences
160+
visitTarget(
161+
ast,
162+
(value: any) => {
163+
return getNodeCtor(value) === "Variable";
164+
},
165+
(node: any, parent: any) => {
166+
if (targets.indexOf(node.name) > -1) {
167+
targets.splice(targets.indexOf(node.name));
168+
}
169+
return KEEP_VISIT;
170+
}
171+
);
172+
159173
return [...new Set(targets)];
160174
}

0 commit comments

Comments
 (0)