magic-string-ast
Advanced tools
Comparing version 0.1.3 to 0.2.0
@@ -41,27 +41,39 @@ "use strict"; | ||
var MagicString = class extends import_magic_string.default { | ||
removeNode(node, { offset = 0 } = {}) { | ||
offset; | ||
constructor(str, options) { | ||
super(str, options); | ||
this.offset = options?.offset ?? 0; | ||
} | ||
getNodePos(nodes, offset) { | ||
const _offset = offset ?? this.offset; | ||
if (Array.isArray(nodes)) | ||
return [_offset + nodes[0].start, _offset + nodes.slice(-1)[0].end]; | ||
else | ||
return [_offset + nodes.start, _offset + nodes.end]; | ||
} | ||
removeNode(node, { offset } = {}) { | ||
if (isEmptyNodes(node)) | ||
return this; | ||
super.remove(...getNodePos(node, offset)); | ||
super.remove(...this.getNodePos(node, offset)); | ||
return this; | ||
} | ||
moveNode(node, index, { offset = 0 } = {}) { | ||
moveNode(node, index, { offset } = {}) { | ||
if (isEmptyNodes(node)) | ||
return this; | ||
super.move(...getNodePos(node, offset), index); | ||
super.move(...this.getNodePos(node, offset), index); | ||
return this; | ||
} | ||
sliceNode(node, { offset = 0 } = {}) { | ||
sliceNode(node, { offset } = {}) { | ||
if (isEmptyNodes(node)) | ||
return ""; | ||
return super.slice(...getNodePos(node, offset)); | ||
return super.slice(...this.getNodePos(node, offset)); | ||
} | ||
overwriteNode(node, content, { offset = 0, ...options } = {}) { | ||
overwriteNode(node, content, { offset, ...options } = {}) { | ||
if (isEmptyNodes(node)) | ||
return this; | ||
const _content = typeof content === "string" ? content : this.sliceNode(content); | ||
super.overwrite(...getNodePos(node, offset), _content, options); | ||
super.overwrite(...this.getNodePos(node, offset), _content, options); | ||
return this; | ||
} | ||
snipNode(node, { offset = 0 } = {}) { | ||
snipNode(node, { offset } = {}) { | ||
if (isEmptyNodes(node)) | ||
@@ -72,11 +84,5 @@ return new import_magic_string.default("", { | ||
}); | ||
return super.snip(...getNodePos(node, offset)); | ||
return super.snip(...this.getNodePos(node, offset)); | ||
} | ||
}; | ||
function getNodePos(nodes, offset) { | ||
if (Array.isArray(nodes)) | ||
return [offset + nodes[0].start, offset + nodes.slice(-1)[0].end]; | ||
else | ||
return [offset + nodes.start, offset + nodes.end]; | ||
} | ||
function isEmptyNodes(nodes) { | ||
@@ -83,0 +89,0 @@ return Array.isArray(nodes) && nodes.length === 0; |
@@ -1,2 +0,2 @@ | ||
import MagicStringBase__default, { OverwriteOptions } from 'magic-string'; | ||
import MagicStringBase__default, { MagicStringOptions, OverwriteOptions } from 'magic-string'; | ||
export * from 'magic-string'; | ||
@@ -7,2 +7,8 @@ export { default as MagicStringBase } from 'magic-string'; | ||
declare class MagicString extends MagicStringBase__default { | ||
offset: number; | ||
constructor(str: string, options?: MagicStringOptions & { | ||
/** offset of node */ | ||
offset?: number; | ||
}); | ||
private getNodePos; | ||
removeNode(node: Node | Node[], { offset }?: { | ||
@@ -9,0 +15,0 @@ offset?: number; |
@@ -41,27 +41,39 @@ "use strict"; | ||
var MagicString = class extends import_magic_string.default { | ||
removeNode(node, { offset = 0 } = {}) { | ||
offset; | ||
constructor(str, options) { | ||
super(str, options); | ||
this.offset = options?.offset ?? 0; | ||
} | ||
getNodePos(nodes, offset) { | ||
const _offset = offset ?? this.offset; | ||
if (Array.isArray(nodes)) | ||
return [_offset + nodes[0].start, _offset + nodes.slice(-1)[0].end]; | ||
else | ||
return [_offset + nodes.start, _offset + nodes.end]; | ||
} | ||
removeNode(node, { offset } = {}) { | ||
if (isEmptyNodes(node)) | ||
return this; | ||
super.remove(...getNodePos(node, offset)); | ||
super.remove(...this.getNodePos(node, offset)); | ||
return this; | ||
} | ||
moveNode(node, index, { offset = 0 } = {}) { | ||
moveNode(node, index, { offset } = {}) { | ||
if (isEmptyNodes(node)) | ||
return this; | ||
super.move(...getNodePos(node, offset), index); | ||
super.move(...this.getNodePos(node, offset), index); | ||
return this; | ||
} | ||
sliceNode(node, { offset = 0 } = {}) { | ||
sliceNode(node, { offset } = {}) { | ||
if (isEmptyNodes(node)) | ||
return ""; | ||
return super.slice(...getNodePos(node, offset)); | ||
return super.slice(...this.getNodePos(node, offset)); | ||
} | ||
overwriteNode(node, content, { offset = 0, ...options } = {}) { | ||
overwriteNode(node, content, { offset, ...options } = {}) { | ||
if (isEmptyNodes(node)) | ||
return this; | ||
const _content = typeof content === "string" ? content : this.sliceNode(content); | ||
super.overwrite(...getNodePos(node, offset), _content, options); | ||
super.overwrite(...this.getNodePos(node, offset), _content, options); | ||
return this; | ||
} | ||
snipNode(node, { offset = 0 } = {}) { | ||
snipNode(node, { offset } = {}) { | ||
if (isEmptyNodes(node)) | ||
@@ -72,11 +84,5 @@ return new import_magic_string.default("", { | ||
}); | ||
return super.snip(...getNodePos(node, offset)); | ||
return super.snip(...this.getNodePos(node, offset)); | ||
} | ||
}; | ||
function getNodePos(nodes, offset) { | ||
if (Array.isArray(nodes)) | ||
return [offset + nodes[0].start, offset + nodes.slice(-1)[0].end]; | ||
else | ||
return [offset + nodes.start, offset + nodes.end]; | ||
} | ||
function isEmptyNodes(nodes) { | ||
@@ -83,0 +89,0 @@ return Array.isArray(nodes) && nodes.length === 0; |
{ | ||
"name": "magic-string-ast", | ||
"version": "0.1.3", | ||
"packageManager": "pnpm@8.6.5", | ||
"version": "0.2.0", | ||
"packageManager": "pnpm@8.6.7", | ||
"description": "Extend Babel AST for magic-string.", | ||
@@ -36,3 +36,3 @@ "license": "MIT", | ||
"dependencies": { | ||
"magic-string": "^0.30.0" | ||
"magic-string": "^0.30.1" | ||
}, | ||
@@ -45,10 +45,10 @@ "devDependencies": { | ||
"bumpp": "^9.1.1", | ||
"eslint": "^8.44.0", | ||
"eslint": "^8.45.0", | ||
"eslint-define-config": "^1.21.0", | ||
"fast-glob": "^3.3.0", | ||
"prettier": "^2.8.8", | ||
"prettier": "^3.0.0", | ||
"tsup": "^7.1.0", | ||
"tsx": "^3.12.7", | ||
"typescript": "^5.1.6", | ||
"vitest": "^0.32.2" | ||
"vitest": "^0.33.0" | ||
}, | ||
@@ -55,0 +55,0 @@ "engines": { |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
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
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
14511
270
Updatedmagic-string@^0.30.1