简体   繁体   English

获取Visual Studio代码以为自定义Javascript导入提供智能感知

[英]Get Visual Studio Code to give intellisense for custom Javascript imports

I am writing automated testing scripts using TestComplete. 我正在使用TestComplete编写自动化测试脚本。 TestComplete mostly supports Ecmascript 2015, but it has a few quirks that are causing intellisense to not work as I would expect. TestComplete主要支持Ecmascript 2015,但是它有一些古怪之处,导致智能感知无法正常工作。

Here is two examples of code files: 这是代码文件的两个示例:

File: UsefulStuffFileName.js 文件: 有用的StuffFileName.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 文件: 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; 导出是通过以下样式完成的: module.exports.UsefullStuff = UsefullStuff; . (This makes it work with TestComplete.) (这使其可以与TestComplete一起使用。)
  2. The "import" assigns to a variable ( var useful = require('UsefulStuffFileName'); ) “导入”分配给变量( var useful = require('UsefulStuffFileName');
  3. The "new"ing of the object uses the variable to access the class ( new useful.UsefulStuff("Hello"); ). 对象的“新建”使用变量访问类( new useful.UsefulStuff("Hello"); )。

Is there anyway to configure Visual Studio Code to understand how these files are related and give me intellisense? 无论如何,有没有配置Visual Studio Code来了解这些文件之间的关系并给我带来智能?

Note: If I try the more standard import {UsefulStuff} from './UsefulStuffFileName'; 注意:如果我尝试import {UsefulStuff} from './UsefulStuffFileName';更标准的import {UsefulStuff} from './UsefulStuffFileName'; I get an error saying "Unexpected token import" from TestComplete. 我从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: 添加一个名为jsconfig.json的文件,并将其放入其中:

.

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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