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

Plugin is broken #785

Closed
chenyulun opened this issue May 20, 2020 · 1 comment · Fixed by #789
Closed

Plugin is broken #785

chenyulun opened this issue May 20, 2020 · 1 comment · Fixed by #789
Labels
Milestone

Comments

@chenyulun
Copy link

Describe the bug
The plugin doesn't seem to be working
https://swc-project.github.io/docs/usage-plugin
Input code

const {default: Visitor} = require("@swc/core/Visitor");
const swc = require("@swc/core");
const { plugins } = require('@swc/core')

class ConsoleStripper extends Visitor {
  visitCallExpression(e) {
    if (e.callee.type !== "MemberExpression") {
      return e;
    }

    if (
      e.callee.object.type === "Identifier" &&
      e.callee.object.value === "console"
    ) {
      if (e.callee.property.type === "Identifier") {
        return {
          type: "UnaryExpression",
          span: e.span,
          operator: "void",
          argument: {
            type: "NumericLiteral",
            span: e.span,
            value: 0
          }
        };
      }
    }

    return e;
  }
}
const consoleStripper =  new ConsoleStripper()
const out = swc.transformSync(
  `
if (foo) {
    console.log("Foo")
} else {
    console.log("Bar")
}`,
  {
    plugin: plugins([consoleStripper.visitProgram.bind(consoleStripper)])
  }
);
console.log(out.code)
console.log(out.code ===
  `if (foo) {
    void 0;
} else {
    void 0;
}`);

run shell

➜  swc-issue-779 git:(master) ✗ yarn plugin
yarn run v1.22.0
$ node plugin.js
if (foo) {
    console.log('Foo');
} else {
    console.log('Bar');
}

false
✨  Done in 0.20s.
➜  swc-issue-779 git:(master) ✗ 

Version
The version of @swc/core:
"@swc/core": "^1.1.44"
Additional context
Add any other context about the problem here.
The breakpoint in Visitor.visitModule

visitModule(m) {
        m.body = this.visitModuleItems(m.body);
        return m;
    }

The object M here can see that it has been modified successfully

m.body[0].alternate.stmts[0].expression
Object {type: "UnaryExpression", span: Object, operator: "void", argument: Object}
argument:Object {type: "NumericLiteral", span: Object, value: 0}
operator:"void"
span:Object {start: 48, end: 66, ctxt: 0}
type:"UnaryExpression"
@kdy1 kdy1 changed the title plugin invalid Plugin is broken May 20, 2020
@kdy1 kdy1 added this to the v1.1.46 milestone May 21, 2020
@kdy1 kdy1 removed this from the v1.1.46 milestone May 21, 2020
@kdy1 kdy1 closed this as completed in #789 May 22, 2020
kdy1 added a commit that referenced this issue May 22, 2020
The commit will help

 - Better testing
 - Preventing regression like #785.
 - **Implementing spack** (It requires access to neon api because of the plugin system)
@kdy1 kdy1 added this to the v1.1.48 milestone May 22, 2020
@swc-bot
Copy link
Collaborator

swc-bot commented Oct 27, 2022

This closed issue has been automatically locked because it had no new activity for a month. If you are running into a similar issue, please create a new issue with the steps to reproduce. Thank you.

@swc-project swc-project locked as resolved and limited conversation to collaborators Oct 27, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Development

Successfully merging a pull request may close this issue.

3 participants