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

Plugins: Selector support for visitors #118

Open
hzoo opened this issue Jun 4, 2020 · 1 comment
Open

Plugins: Selector support for visitors #118

hzoo opened this issue Jun 4, 2020 · 1 comment

Comments

@hzoo
Copy link
Member

hzoo commented Jun 4, 2020

Writing down some idea(s):

https://eslint.org/docs/developer-guide/selectors

{
  "IfStatement > BlockStatement": function(blockStatementNode) {
	// ...your logic here
  },
}
"no-restricted-syntax": ["error", "IfStatement > :not(BlockStatement).consequent"]

Not sure if a lot of people use this, but it could be convenient for simpler transforms. ESLint uses https://github.com/estools/esquery but not sure if we can just use it or we need to change something based on the AST (ideally it would be generic enough). (also found https://github.com/phenomnomnominal/tsquery)

We sorta have something you can do with "virtual ast nodes"/"aliases" but not sure how that works.

https://github.com/babel/babel/blob/a3f00896f710a95ed38f2f9fb3a6e048148b0b98/packages/babel-traverse/src/path/lib/virtual-types.js#L80-L84

export const BlockScoped = {
  checkPath(path: NodePath): boolean {
    return t.isBlockScoped(path.node);
  },
};

Was just sorta thinking if you wanted to target an IIFE for example (give the thing a name, it's not a normal AST node), you could do this (assuming you defined what an IIFE was somewhere):

visitor: {
	IIFE(path) {
		// code here
	}
}

Could certainly add this as a feature but I would just implement this as a babel plugin for people to try out before actually adding it in? Just seems better to do it this way. Would be something like this for the input/output:

visitor: {
-    "FunctionDeclaration[params.length>3]"(path) {
-        // code
-    },
+    FunctionDeclaration(path) {
+        if (path.node.params.length > 3) {
+            // code
+        }
    }
}

I see there's also https://github.com/dfilatov/jspath

@hzoo hzoo changed the title Selector support for visitors Plugins: Selector support for visitors Jun 4, 2020
@ljharb
Copy link
Member

ljharb commented Jun 4, 2020

(If you can't use esquery because the AST is different, please make a reusable babyquery package that can traverse babylon trees)

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