🎩 You're Invited:Meet the Socket team at Black Hat in Las Vegas, August 3-6.RSVP
Sign In

magic-string-ast

Package Overview
Dependencies
Maintainers
1
Versions
25
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

magic-string-ast - npm Package Compare versions

Comparing version
0.9.1
to
1.0.0
+48
-35
dist/index.d.ts
import MagicString, { MagicStringOptions, OverwriteOptions } from "magic-string";
export * from "magic-string";
export * from "magic-string"
//#region dist/.tsdown-types-es/index.d.ts
/**
* MagicString with AST manipulation
*/
//#region src/index.d.ts
interface Node {
start?: number | null;
end?: number | null;
start?: number | null;
end?: number | null;
}
/**
* MagicString with AST manipulation
*/
// eslint-disable-next-line @typescript-eslint/no-unsafe-declaration-merging
interface MagicStringAST extends MagicString {}

@@ -20,24 +14,44 @@ /**

*/
// eslint-disable-next-line @typescript-eslint/no-unsafe-declaration-merging
declare class MagicStringAST implements MagicString {
private prototype;
s: MagicString;
constructor(str: string | MagicString, options?: MagicStringOptions, prototype?: typeof MagicString);
private getNodePos;
removeNode(node: Node | Node[], { offset }?: {
offset?: number
}): this;
moveNode(node: Node | Node[], index: number, { offset }?: {
offset?: number
}): this;
sliceNode(node: Node | Node[], { offset }?: {
offset?: number
}): string;
overwriteNode(node: Node | Node[], content: string | Node | Node[], { offset,...options }?: OverwriteOptions & {
offset?: number
}): this;
snipNode(node: Node | Node[], { offset }?: {
offset?: number
}): MagicStringAST;
clone(): this;
toString(): string;
private prototype;
s: MagicString;
constructor(str: string | MagicString, options?: MagicStringOptions, prototype?: typeof MagicString);
private getNodePos;
removeNode(node: Node | Node[], {
offset
}?: {
offset?: number;
}): this;
moveNode(node: Node | Node[], index: number, {
offset
}?: {
offset?: number;
}): this;
sliceNode(node: Node | Node[], {
offset
}?: {
offset?: number;
}): string;
overwriteNode(node: Node | Node[], content: string | Node | Node[], {
offset,
...options
}?: OverwriteOptions & {
offset?: number;
}): this;
snipNode(node: Node | Node[], {
offset
}?: {
offset?: number;
}): MagicStringAST;
clone(): this;
toString(): string;
private replaceRangeState;
/**
* Replace a range of text with new nodes.
* @param start The start index of the range to replace.
* @param end The end index of the range to replace.
* @param nodes The nodes or strings to insert into the range.
*/
replaceRange(start: number, end: number, ...nodes: (string | Node)[]): this;
}

@@ -48,4 +62,4 @@ /**

interface CodeTransform {
code: string;
map: any;
code: string;
map: any;
}

@@ -56,4 +70,3 @@ /**

declare function generateTransform(s: MagicString | undefined, id: string): CodeTransform | undefined;
//#endregion
export { CodeTransform, MagicString, MagicStringAST, generateTransform };

@@ -6,2 +6,5 @@ import MagicString from "magic-string";

//#region src/index.ts
/**
* MagicString with AST manipulation
*/
var MagicStringAST = class MagicStringAST {

@@ -55,2 +58,42 @@ s;

}
replaceRangeState = {};
/**
* Replace a range of text with new nodes.
* @param start The start index of the range to replace.
* @param end The end index of the range to replace.
* @param nodes The nodes or strings to insert into the range.
*/
replaceRange(start, end, ...nodes) {
const state = this.replaceRangeState[this.offset] || (this.replaceRangeState[this.offset] = {
nodes: [],
indexes: {}
});
if (nodes.length) {
let index = state.indexes[start] || 0;
let intro = "";
let prevNode;
for (const node of nodes) if (typeof node === "string") node && (intro += node);
else {
this.move(node.start, node.end, start);
index = node.start;
prevNode = node;
if (intro) {
this.appendRight(index, intro);
intro = "";
}
state.nodes.push(node);
}
if (intro) this.appendLeft(prevNode?.end || start, intro);
state.indexes[start] = index;
}
if (end > start) {
let index = start;
state.nodes.filter((node) => node.start >= start && node.end <= end).sort((a, b) => a.start - b.start).forEach((node) => {
if (node.start > index) this.remove(index, node.start);
index = node.end;
});
this.remove(index, end);
}
return this;
}
};

@@ -60,2 +103,5 @@ function isEmptyNodes(nodes) {

}
/**
* Generate an object of code and source map from MagicString.
*/
function generateTransform(s, id) {

@@ -62,0 +108,0 @@ if (s?.hasChanged()) return {

{
"name": "magic-string-ast",
"version": "0.9.1",
"version": "1.0.0",
"description": "magic-string with AST shortcut.",

@@ -34,14 +34,14 @@ "type": "module",

"devDependencies": {
"@sxzz/eslint-config": "^6.1.1",
"@sxzz/prettier-config": "^2.2.1",
"@types/node": "^22.14.0",
"bumpp": "^10.1.0",
"eslint": "^9.23.0",
"@sxzz/eslint-config": "^7.0.4",
"@sxzz/prettier-config": "^2.2.3",
"@types/node": "^24.0.4",
"bumpp": "^10.2.0",
"eslint": "^9.29.0",
"fast-glob": "^3.3.3",
"magic-string-stack": "^1.0.0",
"prettier": "^3.5.3",
"tsdown": "^0.7.3",
"tsx": "^4.19.3",
"typescript": "^5.8.2",
"vitest": "^3.1.1"
"prettier": "^3.6.1",
"tsdown": "^0.12.9",
"tsx": "^4.20.3",
"typescript": "^5.8.3",
"vitest": "^3.2.4"
},

@@ -48,0 +48,0 @@ "engines": {

@@ -35,2 +35,7 @@ # magic-string-ast [![npm](https://img.shields.io/npm/v/magic-string-ast.svg)](https://npmjs.com/package/magic-string-ast) [![JSR](https://jsr.io/badges/@sxzz/magic-string-ast)](https://jsr.io/@sxzz/magic-string-ast)

s.overwriteNode(node, 'foo')
// support source-map, inspired by muggle-string.
s.replaceRange(5, 5, '(', expression, ')') // appendLeft
s.replaceRange(5, 8, '(', expression, ')') // overwrite
s.replaceRange(5, 8) // remove
```

@@ -37,0 +42,0 @@