TypeError:无法使用TSLint和TypeScript读取未定义的属性“getFullWidth”

时间:2016-01-14 03:39:36

标签: typescript gulp tslint

我一直在关注Dan Wahlin和在线示例的教程,以配置Gulp和Typescript。我有代码运行,但我无法让tslint()工作。 tslint()调用总是抛出异常:

node_modules\tslint\lib\language\walker\ruleWalker.js:18
    this.limit = this.sourceFile.getFullWidth();
                                ^

TypeError: Cannot read property 'getFullWidth' of undefined
at EnableDisableRulesWalker.RuleWalker [as constructor] (C:\Users\sscott\Development\OntarioDarts.com\node_modules\tslint\lib\language\walker\ruleWalker.js:18:37)
at EnableDisableRulesWalker.SkippableTokenAwareRuleWalker [as constructor] (C:\Users\sscott\Development\OntarioDarts.com\node_modules\tslint\lib\language\walker\skippableTokenAwareRuleWalker.js:11:16)
at new EnableDisableRulesWalker (C:\Users\sscott\Development\OntarioDarts.com\node_modules\tslint\lib\enableDisableRules.js:13:16)
at Linter.lint (C:\Users\sscott\Development\OntarioDarts.com\node_modules\tslint\lib\tslint.js:16:27)
at C:\Users\sscott\Development\OntarioDarts.com\node_modules\gulp-tslint\index.js:96:34
at respond (C:\Users\sscott\Development\OntarioDarts.com\node_modules\rcloader\index.js:73:7)
at respond (C:\Users\sscott\Development\OntarioDarts.com\node_modules\rcfinder\index.js:140:7)
at next (C:\Users\sscott\Development\OntarioDarts.com\node_modules\rcfinder\index.js:164:16)
at nextTickCallbackWith0Args (node.js:433:9)
at process._tickCallback (node.js:362:13)

我使用的是Windows 10.我有typescript,tslint,gulp-typescript和gulp-tslint。

已安装的版本:

├─┬ gulp-typescript@2.10.0
│ └── typescript@1.7.3
├─┬ gulp-uglify@1.5.1
│ └─┬ uglify-js@2.6.0
│   └─┬ yargs@3.10.0
│     └─┬ cliui@2.1.0
│       └── wordwrap@0.0.2
├─┬ tslint@3.2.1
│ └─┬ optimist@0.6.1
│   └── wordwrap@0.0.3
└── typescript@1.7.5

Gulp任务:

module.exports = function (gulp, PlugIns, Settings) {
    return function () {
        gulp.src([Settings.SourceFiles.TypeScript])
            .pipe(PlugIns.plumber())
            .pipe(PlugIns.debug())
            .pipe(PlugIns.typescript())

            .pipe(PlugIns.tslint( {
                configuration: {
                    rules: {
                        "class-name": true,
                        "comment-format": [
                            true,
                            "check-space",
                            "check-uppercase"
                        ],
                        "curly": true,
                        "eofline": true,
                        "indent": [
                            true,
                            "tabs"
                        ],
                        "jsdoc-format": true,
                        "max-line-length": 100,
                        "no-unreachable": true,
                        "no-unused-expression": true,
                        "no-unused-variable": true,
                        "no-use-before-declare": true,

                        "one-line": [
                            true,
                            "check-open-brace",
                            "check-catch",
                            "check-else",
                            "check-whitespace"
                        ],
                        "quotemark": [
                            true,
                            "single",
                            "avoid-escape"
                        ],
                        "semicolon": true,
                        "switch-default": true,

                        "variable-name": [
                            true,
                            "allow-trailing-underscore",
                            "ban-keywords"
                        ],
                        "whitespace": [
                            true,
                            "check-branch",
                            "check-decl",
                            "check-operator",
                            "check-separator",
                            "check-type"
                        ]
                    }
                }
            }))
            .pipe(PlugIns.tslint.report("stylish"))
            .pipe(gulp.dest(Settings.Destination.TSCompiled))
        ;
    }
};

1 个答案:

答案 0 :(得分:8)

如果告知TSLint处理的文件不以.ts.tsx扩展名结尾,则通常会发生此错误。我会确保您不会意外地发送任何带有其他扩展名的.js个文件或文件。将来.js文件正常工作是可能的,但现在他们不会。

此外,我会尝试从命令行运行TSLint,并在一些文件上使用相同的配置。如果它不能正常工作,则可能表示存在TSLint错误。