Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

els-component-extraction-addon

Package Overview
Dependencies
Maintainers
1
Versions
13
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

els-component-extraction-addon - npm Package Compare versions

Comparing version 0.1.8 to 0.1.9

testing-transformers.js

119

index.js
"use strict";
const { TextEdit, Position, Command } = require("vscode-languageserver");
const { TextEdit, Position, Range, Command } = require("vscode-languageserver");
const fs = require("fs");
const { URI } = require("vscode-uri");
const {
normalizeToAngleBracketComponent,
waitForFileNameContains,
watcherFn,
} = require("./utils");
const { transformSelection } = require("./transformers");
const { transformTests } = require("./testing-transformers");
function normalizeToAngleBracketComponent(name) {
const SIMPLE_DASHERIZE_REGEXP = /[a-z]|\/|-/g;
const ALPHA = /[A-Za-z0-9]/;
if (name.includes(".")) {
return name;
}
return name.replace(SIMPLE_DASHERIZE_REGEXP, (char, index) => {
if (char === "/") {
return "::";
}
if (index === 0 || !ALPHA.test(name[index - 1])) {
return char.toUpperCase();
}
// Remove all occurrences of '-'s from the name that aren't starting with `-`
return char === "-" ? "" : char.toLowerCase();
});
}
module.exports = {

@@ -34,2 +20,3 @@ onInit(_, project) {

}
project.addWatcher(watcherFn);
project.executors["els.extractSourceCodeToComponent"] = async (

@@ -45,4 +32,34 @@ server,

try {
// const ast = server.templateCompletionProvider.getAST(document.getText(range));
// const focusPath = server.templateCompletionProvider.createFocusPath(ast, ast.loc.start, text);
let result = {
code: source,
args: [],
};
let rootRegistry = server.getRegistry(project.root);
try {
const helpers = Object.keys(rootRegistry["helper"]);
const components = Object.keys(rootRegistry["component"]);
const modifiers = Object.keys(rootRegistry["modifier"]);
result = transformSelection(
source,
[].concat(helpers, components, modifiers)
);
} catch (e) {
console.log(e.toString());
}
let { code, args } = result;
let argNames = args
.slice(0)
.map((el) => el.split("=")[0].replace("@", ""));
if (args.length) {
args = " " + args.join("\n ");
args = ` \n${args} \n`;
} else {
args = "";
}
const componentName = rawComponentName.trim().split(" ").pop();
const tagName = normalizeToAngleBracketComponent(componentName);
const waiter = waitForFileNameContains(componentName);
await server.onExecute({

@@ -52,5 +69,7 @@ command: "els.executeInEmberCLI",

});
const componentName = rawComponentName.trim().split(" ").pop();
// going to wait for file changes api
await new Promise((resolve) => setTimeout(resolve, 2000));
try {
await waiter;
} catch (e) {
console.log("unable to find document change event");
}
const registry = server.getRegistry(project.root);

@@ -65,5 +84,9 @@ if (!(componentName in registry.component)) {

}
const fileName = registry["component"][componentName].find((file) =>
const componentRegistry = registry["component"][componentName];
const fileName = componentRegistry.find((file) =>
file.endsWith(".hbs")
);
const testFileName = componentRegistry.find(
(file) => file.includes("test") && file.endsWith(".js")
);
if (!fileName) {

@@ -76,16 +99,44 @@ console.log(

const fileUri = URI.file(fileName).toString();
const edit = {
changes: {
[uri]: [
[uri]: [TextEdit.replace(range, `<${tagName} ${args}/>`)],
[fileUri]: [
TextEdit.replace(
range,
`<${normalizeToAngleBracketComponent(componentName)} />`
Range.create(
Position.create(0, 0),
Position.create(0, code.length)
),
code
),
],
[fileUri]: [TextEdit.insert(Position.create(0, 0), source)],
},
};
if (testFileName) {
try {
const testContent = fs.readFileSync(testFileName, "utf8");
const newTestContent = transformTests(
testContent,
tagName,
argNames
);
edit.changes[URI.file(testFileName).toString()] = [
TextEdit.replace(
Range.create(
Position.create(0, 0),
Position.create(
testContent.split("\n").length,
testContent.length
)
),
newTestContent
),
];
} catch (e) {
console.log(e.toString());
}
}
await server.connection.workspace.applyEdit(edit);
} catch (e) {
console.error(e);
console.log(e.toString());
}

@@ -92,0 +143,0 @@ };

{
"name": "els-component-extraction-addon",
"version": "0.1.8",
"version": "0.1.9",
"description": "Ember Language Server Templates Code Actions extension",

@@ -21,2 +21,8 @@ "main": "index.js",

"dependencies": {
"@babel/core": "^7.9.6",
"@babel/parser": "^7.9.6",
"@babel/template": "^7.8.6",
"@babel/traverse": "^7.9.6",
"@babel/types": "^7.9.6",
"ember-template-recast": "^4.1.4",
"vscode-languageserver": "^6.1.1",

@@ -23,0 +29,0 @@ "vscode-uri": "^2.1.1"

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc