Skip to content

Auto-import named export from default exported #19877

Closed

Description

@mjbvz

From Jan Hartmann (@janhartmann) on November 9, 2017 5:0

  • VSCode Version: 1.18.0-insider
  • OS Version: MacOS High Sierra

Steps to Reproduce:

  1. Create a file with a default export
  2. Create another file where the default exported is exported as a named export
  3. Try to import it

Reproduces without extensions: Yes

Thank you for this auto-import feature in JavaScript! This is something I have been awaiting for a long time! I am not sure of the limitations of this, but I have trouble auto-importing default export's from an index.js file.

Take for example:

// File: ProfilePhoto.js
import React from "react";
import PropTypes from "prop-types";
import injectSheet from "react-jss";

const ProfilePhoto = ({ classes, picture }) => {
    return <img src={"data:image/jpeg;base64," + picture} className={classes.photo} />
};

const styles = () => ({
    photo: {
       // Styling
    }
});

export default injectSheet(styles)(ProfilePhoto); // injects the classes argument



// File: index.js
export { default as ProfilePhoto } from "./ProfilePhoto";

What I am doing here is that I am using an higher order component to wrap the function and use that as the default export. But this will never be picked up from the auto-import suggestions?

Is this a limitation or is something which can be integrated in VSCode?

Copied from original issue: microsoft/vscode#37874

Activity

  1. mjbvz commented on Nov 9, 2017

    @mjbvz
    ContributorAuthor

    From Zoltan Feher (@zfeher) on November 9, 2017 8:40

    Also want to thank you for the new auto import feature. It would be nice if you can make it work with default exports as well in the future. Here are some suggestions:

    We have many default exports in the following forms

    // some-func/index.js
    const someFunc = () => { ... }
    export default someFunc
    
    // other-func/index.js
    export default function() { ... }
    
    // other-func-with-name.js
    export default function otherFuncWithName () { ... }

    So sometimes a default name for the exported default function can be aquired from the module (this could be an extra bonus feature).

    However in general cases the module name (folder-name, folderName, FolderName) or the filename can be used (converted camelCase/PascalCase version if name is kebab-case) as a default name for auto import. So if I type someFunction, SomeFunction or some-function it could find the module to import. Plus it could show us an extra option like import as which could ask for a name.

    Maybe the whole auto import could be improved later via using (fuzzy) search so we could narrow the results eg typing: utils/feature/some-func, u/f/sf, etc ... which eventually will be replaced with the found import if accepted.

  2. added
    VS Code Tracked
    and removed on Nov 9, 2017
  3. removed their assignment
    on Nov 9, 2017
  4. mjbvz commented on Nov 9, 2017

    @mjbvz
    ContributorAuthor

    Auto-import does work for default exports but only if there is an understandable symbol name currently. Example:

    export default class Foo { }
    
    export default function foo() { }

    A basic example of Jan Hartmann (@janhartmann)'s request would recognizing exports such as :

    const id = x => x;
    
    class Foo { }
    
    export default id(Foo);

    As Zoltan Feher (@zfeher) mentioned, instead of trying to get the symbol name in this case, we also could use the filename instead

  5. mhegazy commented on Nov 9, 2017

    @mhegazy
    Contributor

    Should be fixed by #19875

  6. added
    Domain: LS: Quick Fixes
    on Nov 9, 2017
  7. locked and limited conversation to collaborators on Jun 14, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    BugDomain: LS: Quick FixesVS Code Tracked

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions