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

Add tests for :scope and :root selectors #69

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

Conversation

run1t
Copy link

@run1t run1t commented Nov 12, 2018

As described in #61 I added the tests for root an scope.
Tell me If I need to add more tests.

I also addded a specific test for the use case described in this issue:
phenomnomnominal/tsquery#23

@run1t run1t changed the title Scope root tests Add tests for :scope and :root selectors Nov 12, 2018
@run1t
Copy link
Author

run1t commented Nov 20, 2018

This issue has been open for 8 days now. Does someone can take a look at it please ? 😃

@EduardoRFS
Copy link

wow thx, i really need that

@run1t
Copy link
Author

run1t commented May 24, 2019

any news ?

@@ -308,7 +390,7 @@
* Parse a selector string and return its AST.
*/
function parse(selector) {
return parser.parse(selector);
return transform(parser.parse(selector));

Choose a reason for hiding this comment

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

This is my main concern about the changes. Is the transform necessary? Or is it just preventing this from being a breaking change? Could we just stop supporting the subject indicator?

Copy link
Author

Choose a reason for hiding this comment

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

It's just to prevent this from being a breaking change.
The subject indicators is deprecated for a long time now, It maybe a good thing to remove it.
I can make the change, if needed.

Choose a reason for hiding this comment

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

I think it would be okay to make this a breaking change, I can imagine there being issues with the transform which might be annoying. Perhaps we can just document how a user would need to change their query?

Copy link
Author

Choose a reason for hiding this comment

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

It's sounds like a good idea, but I we drop the support for subject indicator we probably want to change the parser too and remove the related tests no ?

Choose a reason for hiding this comment

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

Probably, I guess we could do that as a separate PR though?

}
return true;

case 'has':
var a, collector = [];
var a, collector = [], parent = ancestry[0];

Choose a reason for hiding this comment

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

The naming here is a bit unclear as it's clobbered by the parent in the traverse method. Are they the same parent?

Copy link
Author

Choose a reason for hiding this comment

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

It's not, the parent in the traverse method is the parent of the node being traversed.
I agree with you it's a bit confusing. But I have no idea with which name we can change that 😅


"select only first level return": function () {
var firstBlock = esquery(nestedFunctionsWithReturns, "BlockStatement")[0];
var matches = esquery(firstBlock, ":scope > ReturnStatement");

Choose a reason for hiding this comment

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

I don't really understand how this tests something different than if it was :root > ReturnStatement, could you please explain it to me?

Choose a reason for hiding this comment

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

Or is it actually a typo, since the next file does the same test?

Copy link
Author

Choose a reason for hiding this comment

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

It was a typo ... 😓


test.defineSuite("scope selector", {

"select only first level function": function () {

Choose a reason for hiding this comment

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

Is this name a valid description? I'm really confused about the difference between :scope and :root 😅

Copy link
Author

Choose a reason for hiding this comment

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

I need to check that, but the difference between :scope and :root is pretty simple.

If we look at the css documentation (root) for selector :root it says that in a stylesheet :root select the root element of the tree.

So, in our case we should be able to do this :

const example = `
function hi(){}
`
// We are selecting the function
const identifier = esquery(example, 'Identifier');

// From the identifier hi we are selecting the `:root` element
const root = esquery(identifier, ':root');

// From the root we select the identifier` hi` again and log it's name
console.log(esquery(root, 'Identifier').name)
// => hi

For example with :scope we can do this :

const example = `
function hi(){}
`
// We are selecting the identifier `hi`
const functionDeclaration = esquery(example, 'FunctionDeclaration')[0];

// From the identifier hi we are selecting the direct child of `:scope` which are `BlockStatement`.
const block = esquery(identifier, ':scope > BlockStatement');

// One block statement has been selected
console.log(block.length)
// => 1

If we try to do the same thing with :root, it can't match any BlockStatement :

const example = `
function hi(){}
`
// We are selecting the function
const functionDeclaration = esquery(example, 'FunctionDeclaration')[0];

// From the function we are selecting the direct child of `:root` which are `BlockStatement`.
const block = esquery(identifier, ':root > BlockStatement');

// There is no `BlockStatement` has direct child of the root document
console.log(block.length)
// => 0

For me it should work like this, I will recheck the tests to see if it's match the exact explanation below.

Copy link
Author

Choose a reason for hiding this comment

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

After checking the test again and reading the code, it does not match what I was thinking. After a little bit of investigation, the :scope and :root selector seems to be equivalent.
I can't see why there is a difference in the code between :scope and :root thought...

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

Successfully merging this pull request may close these issues.

4 participants