els-component-extraction-addon
Advanced tools
Comparing version 0.1.8 to 0.1.9
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" |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
Filesystem access
Supply chain riskAccesses the file system, and could potentially read sensitive data.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
125701
9
505
8
1
1
+ Added@babel/core@^7.9.6
+ Added@babel/parser@^7.9.6
+ Added@babel/template@^7.8.6
+ Added@babel/traverse@^7.9.6
+ Added@babel/types@^7.9.6
+ Addedember-template-recast@^4.1.4
+ Added@ampproject/remapping@2.3.0(transitive)
+ Added@babel/code-frame@7.26.2(transitive)
+ Added@babel/compat-data@7.26.3(transitive)
+ Added@babel/core@7.26.0(transitive)
+ Added@babel/generator@7.26.3(transitive)
+ Added@babel/helper-compilation-targets@7.25.9(transitive)
+ Added@babel/helper-module-imports@7.25.9(transitive)
+ Added@babel/helper-module-transforms@7.26.0(transitive)
+ Added@babel/helper-string-parser@7.25.9(transitive)
+ Added@babel/helper-validator-identifier@7.25.9(transitive)
+ Added@babel/helper-validator-option@7.25.9(transitive)
+ Added@babel/helpers@7.26.0(transitive)
+ Added@babel/parser@7.26.3(transitive)
+ Added@babel/template@7.25.9(transitive)
+ Added@babel/traverse@7.26.4(transitive)
+ Added@babel/types@7.26.3(transitive)
+ Added@glimmer/env@0.1.7(transitive)
+ Added@glimmer/global-context@0.62.5(transitive)
+ Added@glimmer/interfaces@0.62.5(transitive)
+ Added@glimmer/reference@0.62.5(transitive)
+ Added@glimmer/syntax@0.62.5(transitive)
+ Added@glimmer/util@0.62.5(transitive)
+ Added@glimmer/validator@0.62.5(transitive)
+ Added@handlebars/parser@1.1.0(transitive)
+ Added@jridgewell/gen-mapping@0.3.8(transitive)
+ Added@jridgewell/resolve-uri@3.1.2(transitive)
+ Added@jridgewell/set-array@1.2.1(transitive)
+ Added@jridgewell/sourcemap-codec@1.5.0(transitive)
+ Added@jridgewell/trace-mapping@0.3.25(transitive)
+ Added@nodelib/fs.scandir@2.1.5(transitive)
+ Added@nodelib/fs.stat@2.0.5(transitive)
+ Added@nodelib/fs.walk@1.2.8(transitive)
+ Added@simple-dom/interface@1.4.0(transitive)
+ Addedansi-regex@5.0.1(transitive)
+ Addedansi-styles@4.3.0(transitive)
+ Addedarray-union@2.1.0(transitive)
+ Addedasync@2.6.4(transitive)
+ Addedasync-promise-queue@1.0.5(transitive)
+ Addedbase64-js@1.5.1(transitive)
+ Addedbl@4.1.0(transitive)
+ Addedbraces@3.0.3(transitive)
+ Addedbrowserslist@4.24.3(transitive)
+ Addedbuffer@5.7.1(transitive)
+ Addedcaniuse-lite@1.0.30001690(transitive)
+ Addedchalk@4.1.2(transitive)
+ Addedcli-cursor@3.1.0(transitive)
+ Addedcli-spinners@2.9.2(transitive)
+ Addedclone@1.0.4(transitive)
+ Addedcolor-convert@2.0.1(transitive)
+ Addedcolor-name@1.1.4(transitive)
+ Addedcolors@1.4.0(transitive)
+ Addedcommander@6.2.1(transitive)
+ Addedconvert-source-map@2.0.0(transitive)
+ Addeddebug@2.6.94.4.0(transitive)
+ Addeddefaults@1.0.4(transitive)
+ Addeddir-glob@3.0.1(transitive)
+ Addedelectron-to-chromium@1.5.76(transitive)
+ Addedember-template-recast@4.3.0(transitive)
+ Addedescalade@3.2.0(transitive)
+ Addedfast-glob@3.3.2(transitive)
+ Addedfastq@1.18.0(transitive)
+ Addedfill-range@7.1.1(transitive)
+ Addedgensync@1.0.0-beta.2(transitive)
+ Addedglob-parent@5.1.2(transitive)
+ Addedglobals@11.12.0(transitive)
+ Addedglobby@11.1.0(transitive)
+ Addedhas-flag@4.0.0(transitive)
+ Addedieee754@1.2.1(transitive)
+ Addedignore@5.3.2(transitive)
+ Addedinherits@2.0.4(transitive)
+ Addedis-extglob@2.1.1(transitive)
+ Addedis-glob@4.0.3(transitive)
+ Addedis-interactive@1.0.0(transitive)
+ Addedis-number@7.0.0(transitive)
+ Addedis-unicode-supported@0.1.0(transitive)
+ Addedjs-tokens@4.0.0(transitive)
+ Addedjsesc@3.1.0(transitive)
+ Addedjson5@2.2.3(transitive)
+ Addedlodash@4.17.21(transitive)
+ Addedlog-symbols@4.1.0(transitive)
+ Addedlru-cache@5.1.1(transitive)
+ Addedmerge2@1.4.1(transitive)
+ Addedmicromatch@4.0.8(transitive)
+ Addedmimic-fn@2.1.0(transitive)
+ Addedms@2.0.02.1.3(transitive)
+ Addednode-releases@2.0.19(transitive)
+ Addedonetime@5.1.2(transitive)
+ Addedora@5.4.1(transitive)
+ Addedpath-type@4.0.0(transitive)
+ Addedpicocolors@1.1.1(transitive)
+ Addedpicomatch@2.3.1(transitive)
+ Addedqueue-microtask@1.2.3(transitive)
+ Addedreadable-stream@3.6.2(transitive)
+ Addedrestore-cursor@3.1.0(transitive)
+ Addedreusify@1.0.4(transitive)
+ Addedrun-parallel@1.2.0(transitive)
+ Addedsafe-buffer@5.2.1(transitive)
+ Addedsemver@6.3.1(transitive)
+ Addedsignal-exit@3.0.7(transitive)
+ Addedsimple-html-tokenizer@0.5.11(transitive)
+ Addedslash@3.0.0(transitive)
+ Addedstring_decoder@1.3.0(transitive)
+ Addedstrip-ansi@6.0.1(transitive)
+ Addedsupports-color@7.2.0(transitive)
+ Addedtmp@0.2.3(transitive)
+ Addedto-regex-range@5.0.1(transitive)
+ Addedupdate-browserslist-db@1.1.1(transitive)
+ Addedutil-deprecate@1.0.2(transitive)
+ Addedwcwidth@1.0.1(transitive)
+ Addedworkerpool@6.5.1(transitive)
+ Addedyallist@3.1.1(transitive)