Skip to content

[no-undef] False positive with optional chaining #1116

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

Closed
dannybloe opened this issue Oct 21, 2019 · 6 comments · Fixed by #1169
Closed

[no-undef] False positive with optional chaining #1116

dannybloe opened this issue Oct 21, 2019 · 6 comments · Fixed by #1169
Labels
bug Something isn't working has pr there is a PR raised to close this package: eslint-plugin Issues related to @typescript-eslint/eslint-plugin scope analyser Issues that are caused by bugs/incomplete cases in the scope analyser

Comments

@dannybloe
Copy link

Hi there,

Just trying the latest eslint-plugin (2.4.0) with optional chaining and this code:

const a: { x: number; y: number; z?: number} = { x: 1, y: 2 };
const b = a?.z ?? 3;

console.log(b);

Gives a no-undef error on .z

module.exports = {
	'globals': {
		'Class': true,
		'SyntheticMouseEvent': true,
		'SyntheticChangeEvent': true,
		'TimeoutID': true,
		'IntervalID': true,
		'Blob': true
	},

	'extends': [
		'eslint-config-airbnb',
		'plugin:@typescript-eslint/recommended'
	],

	parser: '@typescript-eslint/parser',
	plugins: [
		'@typescript-eslint',
		'@typescript-eslint/eslint-plugin',
		'react'
	],

	'env': {
		'browser': true,
		'node': true,
		'es2017': true,
		'mocha': true
	},
	'rules': {
		'no-bitwise': 'off',
		'no-console': 'off',
		'spaced-comment': 'off',
		'no-param-reassign': 'off',
		'no-return-assign': 'warn',
		'no-nested-ternary': 'off',
		'no-restricted-globals': 'off',
		'class-methods-use-this': 'off',
		'sort-keys': 0,
		'indent': [
			'error',
			'tab',
			{
				'SwitchCase': 1
			}
		],
		'no-confusing-arrow': 'off',
		'implicit-arrow-linebreak': 'off',
		'lines-between-class-members': 'off',
		'prefer-destructuring': 'off',

		'arrow-body-style': 'off',
		'operator-linebreak': 'off',
		'no-underscore-dangle': 0,
		'no-tabs': 0,
		'no-shadow': 0,
		'no-useless-escape': 0,
		'padded-blocks': 0,
		'space-in-parens': 0,
		'template-curly-spacing': [
			'error',
			'always'
		],
		'object-curly-newline': 'off',
		'comma-dangle': [
			'error',
			'never'
		],
		'no-trailing-spaces': 'off',
		'no-unused-vars': 'warn',
		'no-unused-expressions': 'off',
		'linebreak-style': [
			'error',
			'unix'
		],
		'max-len': [
			'error',
			400,
			2,
			{
				'ignoreUrls': true,
				'ignoreComments': false,
				'ignoreRegExpLiterals': true,
				'ignoreStrings': true,
				'ignoreTemplateLiterals': true
			}
		],

		'import/prefer-default-export': 0,
		'import/no-unresolved': 0,
		'import/no-cycle': 'off',
		'import/no-extraneous-dependencies': 'off',
		'import/no-webpack-loader-syntax': 0,

		'react/prefer-stateless-function': 'off',
		'react/no-multi-comp': 'off',
		'react/jsx-boolean-value': 'off',
		'react/jsx-closing-tag-location': 'off',
		'react/jsx-closing-bracket-location': 'off',
		'react/jsx-curly-spacing': [
			'error',
			{
				'when': 'always'
			}
		],
		'react/jsx-filename-extension': [
			'warn',
			{
				'extensions': [
					'.ts',
					'.tsx'
				]
			}
		],
		'react/jsx-indent-props': [
			'error',
			'tab'
		],
		'react/jsx-one-expression-per-line': 'off',
		'react/jsx-indent': [
			'error',
			'tab'
		],
		'react/button-has-type': 'off',
		'react/no-unescaped-entities': 'off',
		'react/prop-types': 0,
		'react/require-default-props': 0,
		'react/destructuring-assignment': 'off',
		'react/sort-comp': 'off',
		'react/state-in-constructor': 'off',
		'react/static-property-placement': 'off',
		'react/no-array-index-key': 'off',

		'@typescript-eslint/class-name-casing': 'off',
		'@typescript-eslint/explicit-function-return-type': 'off',
		'@typescript-eslint/ban-ts-ignore': 'off',
		'@typescript-eslint/no-explicit-any': 'off',

		'jsx-a11y/anchor-is-valid': 'off',
		'jsx-a11y/click-events-have-key-events': 'off',
		'jsx-a11y/href-no-hash': 'off',
		'jsx-a11y/label-has-for': 'off',
		'jsx-a11y/mouse-events-have-key-events': 'off',
		'jsx-a11y/no-autofocus': 'off',
		'jsx-a11y/no-noninteractive-element-interactions': 'off',
		'jsx-a11y/no-noninteractive-tabindex': 'off',
		'jsx-a11y/no-static-element-interactions': 'off',
		'jsx-a11y/show-errors': 'off',
		'jsx-a11y/tabindex-no-positive': 'off'
	}
};

Versions

package version
@typescript-eslint/eslint-plugin 2.4.0
@typescript-eslint/parser 2..4.0
eslint-config-airbnb 18.0.1
TypeScript 3.7.0-beta
ESLint 6.5.1
node 10.16.2
npm 6.10.3
@dannybloe dannybloe added package: eslint-plugin Issues related to @typescript-eslint/eslint-plugin triage Waiting for team members to take a look labels Oct 21, 2019
@bradzacher bradzacher changed the title Optional chaining gives no-undef error (2.4.0) [no-undef] False positive with optional chaining Oct 21, 2019
@bradzacher bradzacher added bug Something isn't working and removed triage Waiting for team members to take a look labels Oct 21, 2019
@bradzacher
Copy link
Member

Same problems as #1104 and #1090

@bradzacher bradzacher added the scope analyser Issues that are caused by bugs/incomplete cases in the scope analyser label Oct 21, 2019
@sdudley
Copy link

sdudley commented Oct 28, 2019

Until this gets fixed, for what it's worth, downgrading the @typescript-eslint/eslint-plugin module to version 2.3.2 seems to work OK for me alongside TS 3.7. That version of the plugin does throw out a big typescript-estree banner about the TS version not being supported, but at least it doesn't emit the no-undef errors.

@bradzacher
Copy link
Member

We have undefined behaviour for new syntax on old versions - the parser shouldn't crash, but linting may not work as expected, as the AST structure may not be correct..

Happy to accept a PR to fix it.

@skoshy
Copy link

skoshy commented Nov 3, 2019

Another temporary workaround for objects is to use bracket syntax instead of .prop

image

vs.

image

@rhyek
Copy link

rhyek commented Nov 7, 2019

I just disabled the no-undef rule since Typescript reports those errors anyways.

@bradzacher bradzacher added the has pr there is a PR raised to close this label Nov 8, 2019
@ra2dev
Copy link

ra2dev commented Nov 10, 2019

@rhyek is right, just disable no-undef: 0 and in tsconfig.json add noUnusedLocals: true (Report errors on unused locals.)

@bradzacher bradzacher added this to the scope analysis rewrite milestone Apr 6, 2020
@github-actions github-actions bot locked as resolved and limited conversation to collaborators May 7, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
bug Something isn't working has pr there is a PR raised to close this package: eslint-plugin Issues related to @typescript-eslint/eslint-plugin scope analyser Issues that are caused by bugs/incomplete cases in the scope analyser
Projects
None yet
Development

Successfully merging a pull request may close this issue.

6 participants