Skip to content
This repository has been archived by the owner on Jul 15, 2023. It is now read-only.

The 'import-name' rule threw an error. Cannot read property 'importClause' of undefined #667

Closed
joseSantacruz opened this issue Dec 4, 2018 · 14 comments

Comments

@joseSantacruz
Copy link

joseSantacruz commented Dec 4, 2018

Bug Report

  • tslint-microsoft-contrib version: 6.0.0
  • TSLint version:5.11.0
  • TypeScript version:3.1.6
  • Running TSLint via: CLI

TypeScript code being linted

// code snippet

with tslint.json configuration:

{
  "extends": ["tslint-microsoft-contrib"]
}

Actual behavior

The 'import-name' rule threw an error in '/Users/zerocooljs/Kushki/repo/kushki-usrv-snr/src/handler/SejiValidationHandler.ts':
TypeError: Cannot read property 'importClause' of undefined
    at ImportNameRuleWalker.visitImportDeclaration (/Users/zerocooljs/Kushki/repo/kushki-usrv-snr/node_modules/tslint-microsoft-contrib/importNameRule.js:138:54)
    at ImportNameRuleWalker.SyntaxWalker.visitNode (/Users/zerocooljs/Kushki/repo/kushki-usrv-snr/node_modules/tslint/lib/language/walker/syntaxWalker.js:392:22)
    at /Users/zerocooljs/Kushki/repo/kushki-usrv-snr/node_modules/tslint/lib/language/walker/syntaxWalker.js:535:63
    at visitNodes (/Users/zerocooljs/Kushki/repo/kushki-usrv-snr/node_modules/typescript/lib/typescript.js:15788:30)
    at Object.forEachChild (/Users/zerocooljs/Kushki/repo/kushki-usrv-snr/node_modules/typescript/lib/typescript.js:16016:24)
    at ImportNameRuleWalker.SyntaxWalker.walkChildren (/Users/zerocooljs/Kushki/repo/kushki-usrv-snr/node_modules/tslint/lib/language/walker/syntaxWalker.js:535:12)
    at ImportNameRuleWalker.SyntaxWalker.visitSourceFile (/Users/zerocooljs/Kushki/repo/kushki-usrv-snr/node_modules/tslint/lib/language/walker/syntaxWalker.js:228:14)
    at ImportNameRuleWalker.SyntaxWalker.visitNode (/Users/zerocooljs/Kushki/repo/kushki-usrv-snr/node_modules/tslint/lib/language/walker/syntaxWalker.js:482:22)
    at ImportNameRuleWalker.SyntaxWalker.walk (/Users/zerocooljs/Kushki/repo/kushki-usrv-snr/node_modules/tslint/lib/language/walker/syntaxWalker.js:24:14)
    at Rule.AbstractRule.applyWithWalker (/Users/zerocooljs/Kushki/repo/kushki-usrv-snr/node_modules/tslint/lib/language/rule/abstractRule.js:31:16)

Expected behavior

Rule should not crash and no linting issue should be found.

@IllusionMH
Copy link
Contributor

IllusionMH commented Dec 4, 2018

Hi @joseSantacruz , could you please provide provide reduced test case from SejiValidationHandler.ts that throws this error? Do you have dynamic imports in file?

I've tried to reproduce it, but got another exception:

import 'some-module';

leads to

The 'import-name' rule threw an error in 'C:/web/projects/tslint-microsoft-contrib/tests/import-name/test.ts':
TypeError: Cannot read property 'name' of undefined
    at ImportNameRuleWalker.visitImportDeclaration (C:\web\projects\tslint-microsoft-contrib\dist\src\importNameRule.js:127:31)
    at ImportNameRuleWalker.SyntaxWalker.visitNode (C:\web\projects\tslint-microsoft-contrib\node_modules\tslint\lib\language\walker\syntaxWalker.js:392:22)
    at C:\web\projects\tslint-microsoft-contrib\node_modules\tslint\lib\language\walker\syntaxWalker.js:535:63
    at visitNodes (C:\web\projects\tslint-microsoft-contrib\node_modules\typescript\lib\typescript.js:15788:30)
    at Object.forEachChild (C:\web\projects\tslint-microsoft-contrib\node_modules\typescript\lib\typescript.js:16016:24)
    at ImportNameRuleWalker.SyntaxWalker.walkChildren (C:\web\projects\tslint-microsoft-contrib\node_modules\tslint\lib\language\walker\syntaxWalker.js:535:12)
    at ImportNameRuleWalker.SyntaxWalker.visitSourceFile (C:\web\projects\tslint-microsoft-contrib\node_modules\tslint\lib\language\walker\syntaxWalker.js:228:14)
    at ImportNameRuleWalker.SyntaxWalker.visitNode (C:\web\projects\tslint-microsoft-contrib\node_modules\tslint\lib\language\walker\syntaxWalker.js:482:22)
    at ImportNameRuleWalker.SyntaxWalker.walk (C:\web\projects\tslint-microsoft-contrib\node_modules\tslint\lib\language\walker\syntaxWalker.js:24:14)
    at Rule.AbstractRule.applyWithWalker (C:\web\projects\tslint-microsoft-contrib\node_modules\tslint\lib\language\rule\abstractRule.js:31:16)

@jvanoostveen
Copy link

I'm seeing the Cannot read property 'name' of undefined as well.
When I check the files, I have some imports like:

import './polyfills';

and

import * as dependencies from '../page-dependencies';

@IllusionMH
Copy link
Contributor

@jvanoostveen first one causes your exceptions, because there are no importClause in ImportStatement element.
Second should be processed just fine. Do you have problems with linter if you comment import './polyfills'; and similar lines?

@jvanoostveen
Copy link

For this import, we disable the linting rule: 'no-import-side-effect'. No other issues with crashing rules. But not all Microsoft contrib rules are enabled, so not all are tested.

@IllusionMH
Copy link
Contributor

I'm waiting for some code that triggers initial exception and then will work on fix.

@joseSantacruz
Copy link
Author

Hi @IllusionMH ,

this is the code of the SejiValidationHandler.ts

/**
 * Seji Validation Handler
 */
import {
  BUILDER_API_GATEWAY_MIDDLEWARE,
  ContentTypeEnum,
  ERROR_API_MIDDLEWARE,
  IDENTIFIERS as ID,
  IHandler,
  INPUT_OUTPUT_LOGS,
  IRollbar,
  SETUP_MIDDLEWARE,
  SSM_MIDDLEWARE,
  StatusCodeEnum,
  VALIDATION_MIDDLEWARE,
} from "@kushki/core";
import { IDENTIFIERS } from "constant/Identifiers";
import { CONTAINER } from "infrastructure/Container";
import * as middy from "middy";
import "reflect-metadata";
import { ISejiService } from "repository/ISejiService";
import * as Rollbar from "rollbar";
import "source-map-support/register";

const CORE: IHandler = CONTAINER.get<IHandler>(ID.IHandler);
const ROLLBAR: Rollbar = CONTAINER.get<IRollbar>(ID.IRollbar).init();
const HANDLER: middy.IMiddy = middy(
  ROLLBAR.lambdaHandler(
    CORE.run<
      ISejiService, // Service Definition
      object
    >( // Service observable resolve type
      IDENTIFIERS.SejiService, // Service instance
      "validate", // Service Method,
      CONTAINER,
      ROLLBAR
    )
  )
)
  .use(SETUP_MIDDLEWARE(ROLLBAR))
  .use(INPUT_OUTPUT_LOGS(ROLLBAR))
  .use(ERROR_API_MIDDLEWARE(ROLLBAR))
  .use(SSM_MIDDLEWARE(ROLLBAR))
  .use(
    BUILDER_API_GATEWAY_MIDDLEWARE(
      ROLLBAR,
      ContentTypeEnum.JSON,
      StatusCodeEnum.OK,
      ContentTypeEnum.JSON,
      true
    )
  )
  .use(
    VALIDATION_MIDDLEWARE(ROLLBAR, {
      path: "seji_validate_pathparameters",
    })
  );

export { HANDLER };

And in case is usefull this is my current tsconfig

{
  "compilerOptions": {
    "module": "commonjs",
    "moduleResolution": "node",
    "noImplicitAny": false,
    "target": "es2016",
    "lib": ["es2017", "dom"],
    "sourceMap": true,
    "alwaysStrict": true,
    "baseUrl": ".",
    "types": ["mocha", "node", "reflect-metadata"],
    "experimentalDecorators": true,
    "allowSyntheticDefaultImports": true,
    "emitDecoratorMetadata": true,
    "noStrictGenericChecks": true,
    "paths": {
      "handler/*": ["src/handler/*"],
      "constant/*": ["src/constant/*"],
      "gateway/*": ["src/gateway/*"],
      "infrastructure/*": ["src/infrastructure/*"],
      "model/*": ["src/model/*"],
      "repository/*": ["src/repository/*"],
      "service/*": ["src/service/*"],
      "typings": ["typings.d.ts"],
      "utils/*": ["src/utils/*"],
      "error/*": ["src/error/*"],
      "types/*": ["types/*"]
    }
  },
  "exclude": ["node_modules"]
}

@IllusionMH
Copy link
Contributor

@joseSantacruz thanks for your code.
Strange but I receive different exception: TypeError: Cannot read property 'name' of undefined (which I've mentioned earlier).

Could you please comment lines

import "reflect-metadata";

and

import "source-map-support/register";

and try lint again.

And what node.js version do you use? Maybe something changed in exceptions reports.

@joseSantacruz
Copy link
Author

@IllusionMH after removing the imports the error disappears, my node version is 8.10.0. So I think the error should be the same

@IllusionMH
Copy link
Contributor

IllusionMH commented Dec 5, 2018

Really strange.

$ node -v
v8.10.0

$ ./node_modules/.bin/tslint -v
5.11.0

$ ./node_modules/.bin/tsc -v
Version 3.1.6

$ cat ./node_modules/tslint-microsoft-contrib/package.json | grep version
    "type": "version",
  "version": "6.0.0"

But still I can't get TypeError: Cannot read property 'importClause' of undefined. Will try to run in Linux container later.

The other strange thing is stack trace. Where traces are identical except only line numbers of last string:

    at ImportNameRuleWalker.visitImportDeclaration (/Users/zerocooljs/Kushki/repo/kushki-usrv-snr/node_modules/tslint-microsoft-contrib/importNameRule.js:138:54)

And in my case (for 6.0.0) I have

    at ImportNameRuleWalker.visitImportDeclaration (C:\web\test\import-repro\node_modules\tslint-microsoft-contrib\importNameRule.js:127:31)

and visitImportDeclaration ends on 135 line, so exception cannot be on line 138 (can be only if there are some padding at the top of the module).

I'm out of ideas at this moment.

@joseSantacruz could you please show what params do you pass in tslint when run it from CLI? Is there a chance that there is \r line ending instead of \n ?
Added. And may be content of your node_modules/tslint-microsoft-contrib/importNameRule.js if it is 6.0.0 version in node_modules/tslint-microsoft-contrib/package.json (I'm understand that list of requests is long and annoying, but I'm ran out of ideas how to reproduce your case 😞 )

UPD. No luck with Linux. Tried it Docker container - still same error as on Windows.
So may be there is some version mismatch or node_modules/tslint-microsoft-contrib/importNameRule.js file somehow altered (is there any difference with fresh package install in separate folder?) or IDK

Will proceed with fix for TypeError: Cannot read property 'name' of undefined. Maybe it will help with original one, or someone will find a way to reproduce it.

@joseSantacruz
Copy link
Author

joseSantacruz commented Dec 10, 2018

Hi @IllusionMH

this is the command that I'm running npx tslint -t stylish --project tsconfig.json -c tslint.json 'src/**/*.ts'

@dfee
Copy link

dfee commented Jan 3, 2019

I just came across this too. It was because my index.sass file is imported like this: import './index.sass'.

I guess I'll wait for this to be merged into the next release.

@startswithaj
Copy link
Contributor

I'm also finding I have the same error since updating package.

The 'import-name' rule threw an error in '/Users/jake/dev/citrus/citrus-client/src/app/app.module.ts':
TypeError: Cannot read property 'name' of undefined

It's imports like these
import 'rxjs/add/operator/switchMap';

I've tried disabling the rule for that line, but it must try and parse it anyway.

@Cayan
Copy link

Cayan commented Feb 16, 2019

I receive this error when I try to import core-js

@JoshuaKGoldberg
Copy link

💀 It's time! 💀

TSLint is deprecated and no longer accepting pull requests other than security fixes. See #876. ☠️
We recommend you instead use typescript-eslint to lint your TypeScript code with ESLint. ✅

👋 It was a pleasure open sourcing with you!

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

7 participants