-
Notifications
You must be signed in to change notification settings - Fork 91
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
base: master
Are you sure you want to change the base?
Conversation
This issue has been open for 8 days now. Does someone can take a look at it please ? 😃 |
wow thx, i really need that |
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)); |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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 ?
There was a problem hiding this comment.
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]; |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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"); |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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 () { |
There was a problem hiding this comment.
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
😅
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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...
As described in #61 I added the tests for
root
anscope
.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