Skip to content

Commit 9248060

Browse files
gkzfacebook-github-bot
authored andcommitted
[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
1 parent 0e6fb6b commit 9248060

File tree

6 files changed

+177
-0
lines changed

6 files changed

+177
-0
lines changed
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
{
2+
"method": "textDocument/codeAction",
3+
"result": [
4+
{
5+
"title": "Convert to `const foo`",
6+
"kind": "quickfix",
7+
"diagnostics": [],
8+
"edit": {
9+
"changes": {
10+
"<PLACEHOLDER_PROJECT_URL>/fix-match-invalid-object-shorthand.js": [
11+
{
12+
"range": {
13+
"start": {
14+
"line": 4,
15+
"character": 12
16+
},
17+
"end": {
18+
"line": 7,
19+
"character": 1
20+
}
21+
},
22+
"newText": "match (x) {\n {const foo}: 0,\n _: 0,\n}"
23+
}
24+
]
25+
}
26+
},
27+
"command": {
28+
"title": "",
29+
"command": "log:org.flow:<PLACEHOLDER_PROJECT_URL>",
30+
"arguments": [
31+
"textDocument/codeAction",
32+
"convert_match_object_shorthand_to_const",
33+
"Convert to `const foo`"
34+
]
35+
}
36+
},
37+
{
38+
"title": "Convert to `foo: foo`",
39+
"kind": "quickfix",
40+
"diagnostics": [],
41+
"edit": {
42+
"changes": {
43+
"<PLACEHOLDER_PROJECT_URL>/fix-match-invalid-object-shorthand.js": [
44+
{
45+
"range": {
46+
"start": {
47+
"line": 4,
48+
"character": 12
49+
},
50+
"end": {
51+
"line": 7,
52+
"character": 1
53+
}
54+
},
55+
"newText": "match (x) {\n {foo: foo}: 0,\n _: 0,\n}"
56+
}
57+
]
58+
}
59+
},
60+
"command": {
61+
"title": "",
62+
"command": "log:org.flow:<PLACEHOLDER_PROJECT_URL>",
63+
"arguments": [
64+
"textDocument/codeAction",
65+
"convert_match_object_shorthand_to_reference",
66+
"Convert to `foo: foo`"
67+
]
68+
}
69+
}
70+
]
71+
}
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
[options]
2+
experimental.pattern_matching=true
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
// @flow
2+
3+
declare const x: {foo: 1};
4+
5+
const out = match (x) {
6+
{foo}: 0,
7+
_: 0,
8+
};

newtests/lsp/code-action/quickfix/test.js

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1979,5 +1979,32 @@ module.exports = (suite(
19791979
['textDocument/publishDiagnostics', ...lspIgnoreStatusAndCancellation],
19801980
),
19811981
]),
1982+
test('match invalid object shorthand quickfix', [
1983+
addFile(
1984+
'fix-match-invalid-object-shorthand.js.ignored',
1985+
'fix-match-invalid-object-shorthand.js',
1986+
),
1987+
lspStartAndConnect(),
1988+
lspRequestAndWaitUntilResponse('textDocument/codeAction', {
1989+
textDocument: {
1990+
uri: '<PLACEHOLDER_PROJECT_URL>/fix-match-invalid-object-shorthand.js',
1991+
},
1992+
range: {
1993+
start: {line: 5, character: 5},
1994+
end: {line: 5, character: 5},
1995+
},
1996+
context: {
1997+
only: ['quickfix'],
1998+
diagnostics: [],
1999+
},
2000+
}).verifyLSPMessageSnapshot(
2001+
path.join(
2002+
__dirname,
2003+
'__snapshots__',
2004+
'fix-match-invalid-object-shorthand.json',
2005+
),
2006+
['textDocument/publishDiagnostics', ...lspIgnoreStatusAndCancellation],
2007+
),
2008+
]).flowConfig('_flowconfig_match'),
19822009
],
19832010
): SuiteType);
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
(*
2+
* Copyright (c) Meta Platforms, Inc. and affiliates.
3+
*
4+
* This source code is licensed under the MIT license found in the
5+
* LICENSE file in the root directory of this source tree.
6+
*)
7+
8+
type kind =
9+
| ObjShorthandToConst
10+
| ObjShorthandToReference
11+
12+
class mapper target_loc ~kind =
13+
object (this)
14+
inherit Flow_ast_contains_mapper.mapper target_loc as super
15+
16+
method! match_object_pattern_property prop =
17+
let open Flow_ast.MatchPattern.ObjectPattern in
18+
let (loc, _) = prop in
19+
if not @@ this#is_target loc then
20+
super#match_object_pattern_property prop
21+
else
22+
match prop with
23+
| (_, Property.Valid _) -> super#match_object_pattern_property prop
24+
| (_, Property.InvalidShorthand id) ->
25+
let key = Property.Identifier id in
26+
(match kind with
27+
| ObjShorthandToConst ->
28+
let pattern =
29+
( Loc.none,
30+
Flow_ast.MatchPattern.BindingPattern
31+
{
32+
Flow_ast.MatchPattern.BindingPattern.kind = Flow_ast.Variable.Const;
33+
id;
34+
comments = None;
35+
}
36+
)
37+
in
38+
(Loc.none, Property.Valid { Property.key; pattern; shorthand = true; comments = None })
39+
| ObjShorthandToReference ->
40+
let pattern = (Loc.none, Flow_ast.MatchPattern.IdentifierPattern id) in
41+
(Loc.none, Property.Valid { Property.key; pattern; shorthand = false; comments = None }))
42+
end
43+
44+
let convert_object_shorthand_to_const ast loc =
45+
let mapper = new mapper loc ~kind:ObjShorthandToConst in
46+
mapper#program ast
47+
48+
let convert_object_shorthand_to_reference ast loc =
49+
let mapper = new mapper loc ~kind:ObjShorthandToReference in
50+
mapper#program ast

src/services/code_action/code_action_service.ml

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1085,6 +1085,25 @@ let ast_transforms_of_error ~loc_of_aloc ?loc = function
10851085
]
10861086
else
10871087
[]
1088+
| Error_message.EMatchInvalidObjectShorthand { loc = error_loc; name } ->
1089+
if loc_opt_intersects ~error_loc ~loc then
1090+
[
1091+
{
1092+
title = Utils_js.spf "Convert to `const %s`" name;
1093+
diagnostic_title = "convert_match_object_shorthand_to_const";
1094+
transform = untyped_ast_transform Autofix_match_syntax.convert_object_shorthand_to_const;
1095+
target_loc = error_loc;
1096+
};
1097+
{
1098+
title = Utils_js.spf "Convert to `%s: %s`" name name;
1099+
diagnostic_title = "convert_match_object_shorthand_to_reference";
1100+
transform =
1101+
untyped_ast_transform Autofix_match_syntax.convert_object_shorthand_to_reference;
1102+
target_loc = error_loc;
1103+
};
1104+
]
1105+
else
1106+
[]
10881107
| error_message ->
10891108
(match error_message |> Error_message.friendly_message_of_msg with
10901109
| Error_message.PropMissing

0 commit comments

Comments
 (0)