Skip to content

Commit

Permalink
[flow][document-paste] Handle jsx_identifier
Browse files Browse the repository at this point in the history
Summary:
Here we got the classic mistake again: jsx_identifier is not identifier, so if you copy contains jsx, the component name won't be auto imported.

This diff includes the handling for jsx_identifier as well.

Changelog: [internal]

Reviewed By: gkz

Differential Revision: D68279586

fbshipit-source-id: 506e870bb553063de6b72ab2a67d028a75eac124
  • Loading branch information
SamChou19815 authored and facebook-github-bot committed Jan 16, 2025
1 parent ce9682c commit cbed4fc
Show file tree
Hide file tree
Showing 9 changed files with 72 additions and 1 deletion.
2 changes: 2 additions & 0 deletions newtests/lsp/document_paste/__fixtures__/import_source.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,5 @@ declare export opaque type A;
declare const foo: A;
declare export const bar: string;
export default foo;

declare export component Foo();
3 changes: 2 additions & 1 deletion newtests/lsp/document_paste/__fixtures__/source.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import type {A} from './import_source';
import foo, {type A as B} from './import_source';
import {bar} from './import_source';
import {bar, Foo} from './import_source';
import * as NS from './import_source';
import typeof * as NST from './import_source';
import typeof fooType from './import_source';
Expand All @@ -21,4 +21,5 @@ bad;

function nested(b: barType) {
b as barType;
<Foo />;
}
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,12 @@
"importSource": "<PLACEHOLDER_PROJECT_URL>/__fixtures__/import_source.js",
"importSourceIsResolved": true
},
{
"remoteName": "Foo",
"importType": "ImportNamedValue",
"importSource": "<PLACEHOLDER_PROJECT_URL>/__fixtures__/import_source.js",
"importSourceIsResolved": true
},
{
"remoteName": "bar",
"importType": "ImportNamedValue",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,12 @@
"importType": "ImportNamedTypeOf",
"importSource": "<PLACEHOLDER_PROJECT_URL>/__fixtures__/import_source.js",
"importSourceIsResolved": true
},
{
"remoteName": "Foo",
"importType": "ImportNamedValue",
"importSource": "<PLACEHOLDER_PROJECT_URL>/__fixtures__/import_source.js",
"importSourceIsResolved": true
}
]
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,19 @@
},
"newText": "import * as NS from \"./import_source\";\n"
},
{
"range": {
"start": {
"line": 1,
"character": 0
},
"end": {
"line": 1,
"character": 0
}
},
"newText": "import { Foo } from \"./import_source\";\n"
},
{
"range": {
"start": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,19 @@
},
"newText": "\nimport * as NS from \"./import_source\";"
},
{
"range": {
"start": {
"line": 0,
"character": 8
},
"end": {
"line": 0,
"character": 8
}
},
"newText": "\nimport { Foo } from \"./import_source\";"
},
{
"range": {
"start": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,19 @@
},
"newText": "import * as NS from \"../import_source\";\n"
},
{
"range": {
"start": {
"line": 1,
"character": 0
},
"end": {
"line": 1,
"character": 0
}
},
"newText": "import { Foo } from \"../import_source\";\n"
},
{
"range": {
"start": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,19 @@
},
"newText": "import * as NS from \"./import_source\";\n"
},
{
"range": {
"start": {
"line": 1,
"character": 0
},
"end": {
"line": 1,
"character": 0
}
},
"newText": "import { Foo } from \"./import_source\";\n"
},
{
"range": {
"start": {
Expand Down
4 changes: 4 additions & 0 deletions src/services/code_action/document_paste.ml
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,10 @@ class imported_def_collector ~scope ~ranges =
method! identifier ((loc, _) as id) =
this#collect_relevant_def_loc_of_imported_identifier loc;
id

method! jsx_identifier ((loc, _) as id) =
this#collect_relevant_def_loc_of_imported_identifier loc;
id
end

class import_information_extractor ~cx ~loc_of_aloc ~relevant_imported_defs =
Expand Down

0 comments on commit cbed4fc

Please sign in to comment.