Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Is there a plan to support inserting declaration statements? #45

Open
1 task done
imguolao opened this issue Apr 13, 2023 · 1 comment
Open
1 task done

Is there a plan to support inserting declaration statements? #45

imguolao opened this issue Apr 13, 2023 · 1 comment

Comments

@imguolao
Copy link

Describe the feature

expect

Insert a new statement before (or after) a statement.

import { parseModule, generateCode } from "magicast";

// Parse to AST
const mod = parseModule(`export default { }`);

// Insert a declaration statement before the `export default` statement
mod.exports.before(`const a = "VariableDeclarator";`);

// Ensure foo is an array
mod.exports.default.foo ||= [];

// Add a variable as a new array member
mod.exports.default.foo.push("a", "var");

/**
 * const a = "VariableDeclarator";
 * export default {
 *   foo: [a],
 * };
 */
console.log(generateCode(mod).code);

Additional information

  • Would you be willing to help implement this feature?
@antfu
Copy link
Member

antfu commented Apr 13, 2023

Sounds reasonable. I think we could use $append and $prepend on every node? Prefix with $ indicates it's a special operation and avoids conflicts with the namespace.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants