-
Notifications
You must be signed in to change notification settings - Fork 1.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[flow][match] Quickfix for invalid object pattern shorthand
Summary: Adds quickfixes for invalid object pattern shorthand error, one for each intended possibility. Future work will include adding more support for match expressions/statements in the ast differ so that we get more fine-grained changes. Changelog: [internal] Reviewed By: panagosg7 Differential Revision: D70727295 fbshipit-source-id: d4d097cefb54ea5351f80f3169dd18d7a3259644
- Loading branch information
1 parent
0e6fb6b
commit 9248060
Showing
6 changed files
with
177 additions
and
0 deletions.
There are no files selected for viewing
71 changes: 71 additions & 0 deletions
71
newtests/lsp/code-action/quickfix/__snapshots__/fix-match-invalid-object-shorthand.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
{ | ||
"method": "textDocument/codeAction", | ||
"result": [ | ||
{ | ||
"title": "Convert to `const foo`", | ||
"kind": "quickfix", | ||
"diagnostics": [], | ||
"edit": { | ||
"changes": { | ||
"<PLACEHOLDER_PROJECT_URL>/fix-match-invalid-object-shorthand.js": [ | ||
{ | ||
"range": { | ||
"start": { | ||
"line": 4, | ||
"character": 12 | ||
}, | ||
"end": { | ||
"line": 7, | ||
"character": 1 | ||
} | ||
}, | ||
"newText": "match (x) {\n {const foo}: 0,\n _: 0,\n}" | ||
} | ||
] | ||
} | ||
}, | ||
"command": { | ||
"title": "", | ||
"command": "log:org.flow:<PLACEHOLDER_PROJECT_URL>", | ||
"arguments": [ | ||
"textDocument/codeAction", | ||
"convert_match_object_shorthand_to_const", | ||
"Convert to `const foo`" | ||
] | ||
} | ||
}, | ||
{ | ||
"title": "Convert to `foo: foo`", | ||
"kind": "quickfix", | ||
"diagnostics": [], | ||
"edit": { | ||
"changes": { | ||
"<PLACEHOLDER_PROJECT_URL>/fix-match-invalid-object-shorthand.js": [ | ||
{ | ||
"range": { | ||
"start": { | ||
"line": 4, | ||
"character": 12 | ||
}, | ||
"end": { | ||
"line": 7, | ||
"character": 1 | ||
} | ||
}, | ||
"newText": "match (x) {\n {foo: foo}: 0,\n _: 0,\n}" | ||
} | ||
] | ||
} | ||
}, | ||
"command": { | ||
"title": "", | ||
"command": "log:org.flow:<PLACEHOLDER_PROJECT_URL>", | ||
"arguments": [ | ||
"textDocument/codeAction", | ||
"convert_match_object_shorthand_to_reference", | ||
"Convert to `foo: foo`" | ||
] | ||
} | ||
} | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
[options] | ||
experimental.pattern_matching=true |
8 changes: 8 additions & 0 deletions
8
newtests/lsp/code-action/quickfix/fix-match-invalid-object-shorthand.js.ignored
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
// @flow | ||
|
||
declare const x: {foo: 1}; | ||
|
||
const out = match (x) { | ||
{foo}: 0, | ||
_: 0, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
(* | ||
* Copyright (c) Meta Platforms, Inc. and affiliates. | ||
* | ||
* This source code is licensed under the MIT license found in the | ||
* LICENSE file in the root directory of this source tree. | ||
*) | ||
|
||
type kind = | ||
| ObjShorthandToConst | ||
| ObjShorthandToReference | ||
|
||
class mapper target_loc ~kind = | ||
object (this) | ||
inherit Flow_ast_contains_mapper.mapper target_loc as super | ||
|
||
method! match_object_pattern_property prop = | ||
let open Flow_ast.MatchPattern.ObjectPattern in | ||
let (loc, _) = prop in | ||
if not @@ this#is_target loc then | ||
super#match_object_pattern_property prop | ||
else | ||
match prop with | ||
| (_, Property.Valid _) -> super#match_object_pattern_property prop | ||
| (_, Property.InvalidShorthand id) -> | ||
let key = Property.Identifier id in | ||
(match kind with | ||
| ObjShorthandToConst -> | ||
let pattern = | ||
( Loc.none, | ||
Flow_ast.MatchPattern.BindingPattern | ||
{ | ||
Flow_ast.MatchPattern.BindingPattern.kind = Flow_ast.Variable.Const; | ||
id; | ||
comments = None; | ||
} | ||
) | ||
in | ||
(Loc.none, Property.Valid { Property.key; pattern; shorthand = true; comments = None }) | ||
| ObjShorthandToReference -> | ||
let pattern = (Loc.none, Flow_ast.MatchPattern.IdentifierPattern id) in | ||
(Loc.none, Property.Valid { Property.key; pattern; shorthand = false; comments = None })) | ||
end | ||
|
||
let convert_object_shorthand_to_const ast loc = | ||
let mapper = new mapper loc ~kind:ObjShorthandToConst in | ||
mapper#program ast | ||
|
||
let convert_object_shorthand_to_reference ast loc = | ||
let mapper = new mapper loc ~kind:ObjShorthandToReference in | ||
mapper#program ast |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters