Add tests for :scope and :root selectors#69
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 ? |
| */ | ||
| function parse(selector) { | ||
| return parser.parse(selector); | ||
| return transform(parser.parse(selector)); |
There was a problem hiding this comment.
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.
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.
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.
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.
Probably, I guess we could do that as a separate PR though?
|
|
||
| case 'has': | ||
| var a, collector = []; | ||
| var a, collector = [], parent = ancestry[0]; |
There was a problem hiding this comment.
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.
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.
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.
Or is it actually a typo, since the next file does the same test?
|
|
||
| test.defineSuite("scope selector", { | ||
|
|
||
| "select only first level function": function () { |
There was a problem hiding this comment.
Is this name a valid description? I'm really confused about the difference between :scope and :root 😅
There was a problem hiding this comment.
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.
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
rootanscope.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