Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unknown reason for "range null" with eslintIntegration #842

Closed
orblazer opened this issue May 29, 2019 · 7 comments
Closed

Unknown reason for "range null" with eslintIntegration #842

orblazer opened this issue May 29, 2019 · 7 comments
Labels
locked Please open a new issue and fill out the template instead of commenting.

Comments

@orblazer
Copy link

Hello,
I have an error sometime on some typescript files.
Note: On the editor and with eslint command i don't have lint error

  • Extension version: 1.9.0
  • Project packages versions :
    • @typescript-eslint/eslint-plugin: 1.9.0
    • @typescript-eslint/parser
    • eslint: 5.16.0
    • eslint-config-standard: 12.0.0
    • eslint-plugin-import: 2.17.3
    • eslint-plugin-node: 9.1.0
    • eslint-plugin-promise: 4.1.1
    • eslint-plugin-standard: 4.0.0

The error :

Cannot read property 'range' of null
Occurred while linting /home2/orblazer/dev/nodejs/gestion/api/src/graphql/lib/Auth.ts:1

My file :

import { AuthenticationError, ForbiddenError } from 'apollo-server-core'
import { UserJWT, UserRole } from '@/database/User'

/**
 * Check if user is logged
 *
 * @param user the user info
 * @param throwErr the error is throw
 */
function isAuth (user: UserJWT | false, throwErr: boolean = false): boolean {
  if (user === false && throwErr) {
    throw new AuthenticationError('Access denied! You are not logged !')
  }

  return user !== false
}

/**
 * Check if user has role
 *
 * @param user the user data
 * @param roles the roles
 * @param throwErr thr error is throw
 */
function hasRole (
  user: UserJWT | false,
  roles: UserRole[] | UserRole = UserRole.CLIENT,
  throwErr: boolean = false
): boolean {
  if (!isAuth(user, throwErr)) {
    return false
  }
  user = user as UserJWT // Fix type checking

  if (
    (typeof user.role === 'string' && user.role !== roles) ||
    !roles.includes(user.role)
  ) {
    if (throwErr) {
      throw new ForbiddenError(
        "Access denied! You don't have permission for this action!"
      )
    }

    return false
  }

  return true
}

function hasKey (
  key: string,
  tryKey: string | true = null,
  throwErr: boolean = false
): boolean {
  if ((tryKey === true && key === null) || key !== tryKey) {
    if (throwErr) {
      throw new ForbiddenError(
        "Access denied! You don't have permission for this action! (The key is incorrect)"
      )
    }

    return false
  }

  return true
}

export default {
  isAuth,
  hasRole,
  hasKey
}

My eslintrc :

module.exports = {
  root: true,
  env: {
    node: true
  },
  parser: '@typescript-eslint/parser',
  parserOptions: {
    project: './tsconfig.json'
  },
  extends: [
    'standard',
    'plugin:@typescript-eslint/recommended',
    'plugin:import/errors',
    'plugin:import/warnings',
    'plugin:import/recommended',
    'plugin:import/typescript'
  ],
  plugins: ['@typescript-eslint', 'standard'],
  rules: {
    '@typescript-eslint/indent': [
      'error',
      2,
      {
        SwitchCase: 1,
        VariableDeclarator: 1,
        outerIIFEBody: 1,
        MemberExpression: 1,
        FunctionDeclaration: { parameters: 1, body: 1 },
        FunctionExpression: { parameters: 1, body: 1 },
        CallExpression: { arguments: 1 },
        ArrayExpression: 1,
        ObjectExpression: 1,
        ImportDeclaration: 1,
        flatTernaryExpressions: false,
        ignoreComments: false
      }
    ],

    // Enforce import order
    'import/order': 'error',

    // Imports should come first
    'import/first': 'error',

    // Other import rules
    'import/no-mutable-exports': 'error',

    // Allow unresolved imports
    'import/no-unresolved': 'off',

    // Allow paren-less arrow functions only when there's no braces
    'arrow-parens': ['error', 'as-needed', { requireForBlockBody: true }],

    // Allow async-await
    'generator-star-spacing': 'off',

    // Allow debugger during development
    'no-debugger': process.env.NODE_ENV === 'production' ? 'error' : 'off',
    'no-console': [
      process.env.NODE_ENV === 'production' ? 'error' : 'warn',
      { allow: ['warn', 'error'] }
    ],

    // Prefer const over let
    'prefer-const': [
      'error',
      {
        destructuring: 'any',
        ignoreReadBeforeAssign: false
      }
    ],

    // No single if in an "else" block
    'no-lonely-if': 'error',

    // Force curly braces for control flow,
    // including if blocks with a single statement
    curly: ['error', 'all'],

    // No async function without await
    'require-await': 'error',

    // Force dot notation when possible
    'dot-notation': 'error',

    'no-var': 'error',

    // Force object shorthand where possible
    'object-shorthand': 'error',

    // No useless destructuring/importing/exporting renames
    'no-useless-rename': 'error'
  }
}

Thanks.

@arno-le
Copy link

arno-le commented May 31, 2019

Happens with JS files, too, if there are backtick string literals in the file.

5/31/2019, 12:32:23 PM:
-----------------------
Cannot read property 'range' of null

@tim0991
Copy link

tim0991 commented Jun 18, 2019

prettier/prettier-eslint#213
Cannot read property 'range' of null may be a bug of prettier-vscode ?

@orblazer
Copy link
Author

Thanks @tim0991 , with prettier-eslint@^9.0.0 its work but now i have wrong format (its probably other problem).

This is formatted to (missing before parenthesis) :

import { AuthenticationError, ForbiddenError } from 'apollo-server-core'
import { UserJWT, UserRole } from '@/database/admin/User'

/**
 * Check if user is logged
 *
 * @param user the user info
 * @param throwErr the error is throw
 */
function isAuth(user: UserJWT | false, throwErr: boolean = false): boolean {
  if (user === false && throwErr) {
    throw new AuthenticationError('Access denied! You are not logged !')
  }

  return user !== false
}

/**
 * Check if user has role
 *
 * @param user the user data
 * @param roles the roles
 * @param throwErr thr error is throw
 */
function hasRole(
  user: UserJWT | false,
  roles: UserRole[] | UserRole = UserRole.CLIENT,
  throwErr: boolean = false
): boolean {
  if (!isAuth(user, throwErr)) {
    return false
  }
  user = user as UserJWT // Fix type checking

  if (
    (typeof user.role === 'string' && user.role !== roles) ||
    !roles.includes(user.role)
  ) {
    if (throwErr) {
      throw new ForbiddenError(
        "Access denied! You don't have permission for this action!"
      )
    }

    return false
  }

  return true
}

function hasKey(
  key: string,
  tryKey: string | true = null,
  throwErr: boolean = false
): boolean {
  if ((tryKey === true && key === null) || key !== tryKey) {
    if (throwErr) {
      throw new ForbiddenError(
        "Access denied! You don't have permission for this action! (The key is incorrect)"
      )
    }

    return false
  }

  return true
}

export default {
  isAuth,
  hasRole,
  hasKey
}

@zavarka
Copy link

zavarka commented Jun 29, 2019

Prettier breaks when "as" cast is present:

user = user as UserJWT // Fix type checking

@WaldoJeffers
Copy link

WaldoJeffers commented Jul 22, 2019

This issue is linked to prettier-eslint (see more details eslint/eslint#11858 (comment))

It can be very easily fixed by upgrading to prettier-eslint v9. Would it be possible to release a new version of prettier-vscode with the newest version?

It's incredibly frustrating if you are using TypeScript with this extension :/

The issue is located here, where prettier-eslint relies on a deprecated module typescript-eslint-parser

The PR to fix the bug is here #861

@ntotten
Copy link
Member

ntotten commented Aug 12, 2019

Next release will have ESLint 9.0.0.

@ntotten ntotten closed this as completed Aug 12, 2019
@github-actions
Copy link

This issue has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

@github-actions github-actions bot added the locked Please open a new issue and fill out the template instead of commenting. label Apr 12, 2020
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Apr 12, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
locked Please open a new issue and fill out the template instead of commenting.
Projects
None yet
Development

No branches or pull requests

6 participants