mgechev / codelyzer

Static analysis for Angular projects.
http://codelyzer.com/
MIT License
2.45k stars 234 forks source link

useLifeCycleInterfaceRule causing warning about getFullWidth() on undefined #253

Closed jessezhang91 closed 7 years ago

jessezhang91 commented 7 years ago

I noticed a new warning show up when using 3.0.0-beta.0: Warning: Cannot read property 'getFullWidth' of undefined

Tracing the warning back to the source, it seems the sourceFile at https://github.com/mgechev/codelyzer/blob/3.0.0-beta.0/src/useLifeCycleInterfaceRule.ts#L30 is undefined for a select few barrel index.ts files. They just contain a few export * from "...". The odd thing is I have other barrel files that do not cause the warning to trigger.

I am using: tslint: 4.5.1 typescript: 2.2.1 codelyzer: 3.0.0-beta.0

mgechev commented 7 years ago

Thanks for pointing this out. If you provide a code snippet which allows me to reproduce the problem it'll be even more helpful.

jessezhang91 commented 7 years ago

I ended up logging languageService.getProgram().getSourceFiles() and sourceFile.fileName and saw that sourceFile.fileName was a relative path from root of project while languageService.getProgram().getSourceFiles() contained largely absolute paths with some relative paths.

I think this might be due to paths aliasing in tsconfig.json . I noticed that the few barrels that did end up exhibiting this warning were imported in some cases with relative path and in some cases with the path aliases. Given that, though, I have not been able to generate any snippets to repro this. I'll keep trying.

Edit: I rewrote all imports for one of the files that generated that warning to use just relative paths and that one file stopped triggering the warning. I'll try to get a repro snippet.

jessezhang91 commented 7 years ago

I managed to repro with:

tsconfig.json

{
  "compilerOptions": {
    "noEmit": true,

    "rootDir": "src",
    "baseUrl": "src",
    "paths": {
      "@alias/*": [
        "./*"
      ]
    }
  },
  "includes": [
    "src/**/*.ts"
  ],
  "exclude": [
    "node_modules"
  ]
}

package.json

{
  "name": "codelyzer-253-repro",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "test": "tslint --config tslint.json --project tsconfig.json --type-check"
  },
  "author": "",
  "license": "ISC",
  "devDependencies": {
    "@angular/compiler": "^2.4.8",
    "@angular/core": "^2.4.8",
    "codelyzer": "^3.0.0-beta.0",
    "rxjs": "^5.2.0",
    "tslint": "^4.5.1",
    "typescript": "^2.2.1",
    "zone.js": "^0.7.7"
  }
}

src/test-0.ts

export { Test1 } from "@alias/test-1";

export class Test0 {

}

src/test-1.ts

export { Test0 } from "./test-0";

export class Test1 {

}

Running npm run test causes: Warning: Cannot read property 'getFullWidth' of undefined

hccampos commented 7 years ago

Same error here:

/usr/local/lib/node_modules/tslint/lib/runner.js:117
            throw error;
            ^

TypeError: Cannot read property 'getFullWidth' of undefined
    at Ng2Walker.RuleWalker [as constructor] (/home/hugo/pix4d/spa/node_modules/tslint/lib/language/walker/ruleWalker.js:38:39)
    at new Ng2Walker (/home/hugo/pix4d/spa/node_modules/codelyzer/angular/ng2Walker.js:35:28)
    at Rule.applyWithProgram (/home/hugo/pix4d/spa/node_modules/codelyzer/templatesUsePublicRule.js:94:37)
    at Linter.applyRule (/usr/local/lib/node_modules/tslint/lib/linter.js:135:33)
    at Linter.lint (/usr/local/lib/node_modules/tslint/lib/linter.js:104:41)
    at Runner.processFiles (/usr/local/lib/node_modules/tslint/lib/runner.js:154:20)
    at Runner.run (/usr/local/lib/node_modules/tslint/lib/runner.js:109:18)
    at Object.<anonymous> (/usr/local/lib/node_modules/tslint/lib/tslint-cli.js:138:6)
    at Module._compile (module.js:571:32)
    at Object.Module._extensions..js (module.js:580:10)
mgechev commented 7 years ago

Wasn't able to reproduce. Closing for now.