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 item scope to JS #2675

Merged
merged 3 commits into from
Nov 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions data/fixtures/recorded/languages/typescript/takeItem.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,5 +23,5 @@ finalState:

const value = { a: 1, b: 2, c: 3 };
selections:
- anchor: {line: 1, character: 0}
active: {line: 1, character: 35}
- anchor: {line: 1, character: 6}
active: {line: 1, character: 34}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
let foo, bar;
---

[#1 Content] =
[#1 Domain] = 0:4-0:7
>---<
0| let foo, bar;

[#1 Removal] = 0:4-0:9
>-----<
0| let foo, bar;

[#1 Trailing delimiter] = 0:7-0:9
>--<
0| let foo, bar;

[#1 Insertion delimiter] = ", "


[#2 Content] =
[#2 Domain] = 0:9-0:12
>---<
0| let foo, bar;

[#2 Removal] = 0:7-0:12
>-----<
0| let foo, bar;

[#2 Leading delimiter] = 0:7-0:9
>--<
0| let foo, bar;

[#2 Insertion delimiter] = ", "
2 changes: 2 additions & 0 deletions packages/common/src/scopeSupportFacets/javascript.ts
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,8 @@ export const javascriptCoreScopeSupport: LanguageScopeSupportFacetMap = {
"value.return": supported,
"value.return.lambda": supported,
"value.field": supported,

"collectionItem.unenclosed": supported,
};

export const javascriptJsxScopeSupport: LanguageScopeSupportFacetMap = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,23 +45,39 @@ export class ContainingScopeStage implements ModifierStage {
.run(target);
}

const containingScope = getContainingScopeTarget(
const containingScopes = getContainingScopeTarget(
target,
scopeHandler,
ancestorIndex,
);

if (containingScope == null) {
if (scopeType.type === "collectionItem") {
// For `collectionItem`, fall back to generic implementation
return this.modifierStageFactory
if (scopeType.type === "collectionItem") {
// For `collectionItem`, combine with generic implementation
try {
const legacyScopes = this.modifierStageFactory
.getLegacyScopeStage(this.modifier)
.run(target);
if (containingScopes == null) {
return legacyScopes;
}
if (containingScopes.length === 1 && legacyScopes.length === 1) {
const containingRange = containingScopes[0].contentRange;
const legacyRange = legacyScopes[0].contentRange;
if (
containingRange.contains(legacyRange) &&
!containingRange.isRangeEqual(legacyRange)
) {
return legacyScopes;
}
}
} catch (_ex) {
// Do nothing
}
}

if (containingScopes == null) {
throw new NoContainingScopeError(this.modifier.scopeType.type);
}

return containingScope;
return containingScopes;
}
}
13 changes: 13 additions & 0 deletions queries/javascript.core.scm
Original file line number Diff line number Diff line change
Expand Up @@ -272,6 +272,19 @@
(#has-multiple-children-of-type? @_dummy variable_declarator)
)

;;!! let foo, bar;
;;! ^^^ ^^^
(
(lexical_declaration
(variable_declarator)? @_.leading.endOf
.
(variable_declarator) @collectionItem
.
(variable_declarator)? @_.trailing.startOf
)
(#insertion-delimiter! @collectionItem ", ")
)

(expression_statement
[
;; name:
Expand Down
Loading