Skip to content
New issue

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

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

Already on GitHub? # to your account

(WIP) feat: Add :scope and :root selectors #37

Open
wants to merge 14 commits into
base: master
Choose a base branch
from

Conversation

run1t
Copy link

@run1t run1t commented Nov 17, 2018

As said in #23 I thought the :root selector work. But after adding tests cases discussed with @petebacondarwin the implementation of :root from ESquery is not sufficient to cover the use cases defined below.

However I added the tests to cover the use cases.

Base example

function a() {
  function b() {
    return 'bar';
  }
  return 'foo';
};

Uses cases

:root

  • Assuming we are in the root of the example:
    • ✅ Executing this query :root > FunctionDeclaration should return the function a
  • Assuming we are in the BLOCK of the function a:
    • ✅ Executing this query :root > FunctionDeclaration should return the function a
    • ✅ Executing this query :root FunctionDeclaration should return the function a an function b

:scope

  • Assuming we are in the root of the example:
    • ✅ Executing this query :scope > FunctionDeclaration should return the function a
  • Assuming we are in the BLOCK of the function a:
    • ✅ Executing this query :scope > FunctionDeclaration should return the function b
    • ✅ Executing this query :scope FunctionDeclaration should return the function b

import { TSQueryOptions, TSQuerySelectorNode } from '../tsquery-types';

export function has (node: Node, selector: TSQuerySelectorNode, _: Array<Node>, options: TSQueryOptions): boolean {
export function has (node: Node, selector: TSQuerySelectorNode, ancestry: Array<Node>, {}: Node, {}: TSQueryOptions): boolean {
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I got the :scope selector by moving the traverseChildren function in here. It's probably not the best way to do it.

@@ -6,17 +6,17 @@ import { tsquery } from '../src/index';

describe('tsquery:', () => {
describe('tsquery.project:', () => {
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I had a problem with the tests below. I don't know why but some times when I was launching the test the file.length was incrementing even if I didn't touch the ./tsconfig.json. Do I need to open an issue ?

const results: Array<T> = [];
if (!selector) {
return results;
}

if (selector.left) {
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I added this to be able to go back to the root node when the :root selector is matched.
It's seems incomplete as it will only work when the :root selector is in the left part of the selector AST root.

@@ -25,7 +25,7 @@
"prepublishOnly": "npm run build"
},
"dependencies": {
"esquery": "^1.0.1"
"esquery-scope": "^1.1.0"
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unfortunatly I had to keep the esquery-scope in order to be able to run the ci

@auser
Copy link

auser commented Jul 30, 2019

Is this going to get merged in?

@phenomnomnominal
Copy link
Owner

Not until we get the upstream fix merged I'm afraid. I thought I did a review of the PR in the esquery repo, but it's vanished. I'll do it again and see if we can get @run1t's changes merged there.

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

Successfully merging this pull request may close these issues.

4 participants