简体   繁体   中英

Get Visual Studio Code to give intellisense for custom Javascript imports

I am writing automated testing scripts using TestComplete. TestComplete mostly supports Ecmascript 2015, but it has a few quirks that are causing intellisense to not work as I would expect.

Here is two examples of code files:

File: UsefulStuffFileName.js

class UsefulStuff {
    constructor(initValue) {
        this.initValue = initValue;
    }

    get someValue(){
        return this.initValue + " someValue";
    }

    doStuff(input) {
        return input + " stuff done";
    }
}

module.exports.UsefullStuff = UsefullStuff;

File: WorkingHere.js

var useful = require('UsefulStuffFileName');

class WorkingHere {
    constructor() {
        this.usefullStuff = new useful.UsefulStuff("Hello");
    }

    doCoolStuff() {
        // I want intellisense options when I type the period after this.usefulStuff
        // The options would be someValue, doStuff() and initValue.
        //                                |
        //                                |
        //                                V
        let myVariable = this.usefullStuff.someValue;                
    }
}

The quirks, as I see them are:

  1. The export is done via this style: module.exports.UsefullStuff = UsefullStuff; . (This makes it work with TestComplete.)
  2. The "import" assigns to a variable ( var useful = require('UsefulStuffFileName'); )
  3. The "new"ing of the object uses the variable to access the class ( new useful.UsefulStuff("Hello"); ).

Is there anyway to configure Visual Studio Code to understand how these files are related and give me intellisense?

Note: If I try the more standard import {UsefulStuff} from './UsefulStuffFileName'; I get an error saying "Unexpected token import" from TestComplete.

This can be done via these steps.

  1. Change the import to look like this:

    var { UsefulStuff } = require('UsefulStuff');

  2. Change the instantiation of the class to look like this:

    this.usefulStuff = new UsefulStuff("Hello");

  3. Add a file called jsconfig.json and put this in it:

.

{
    "compilerOptions": {
      "baseUrl": "."      
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM