question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Bug: `no-use-before-define` in combo with `export` keywords.

See original GitHub issue

Environment

Node version: v17.7.2 npm version: v8.5.2 Local ESLint version: v7.32.0 (Currently used) Global ESLint version: Not found Operating System: linux 5.10.0-9-amd64 (Debian 10 Live-USB)

What parser are you using?

@typescript-eslint/parser

What did you do?

Configuration
{
	"env": {
		"browser": true
		,"es2021": true
	}
	,"extends": [
		"standard"
	]
	,"parser": "@typescript-eslint/parser"
	,"parserOptions": {
		"ecmaVersion": "latest"
		,"sourceType": "module"
	}
	,"plugins": [
		"@typescript-eslint"
	]
	,"rules": {
		"no-tabs": 0
		,"indent": ["error" ,"tab",{
			"SwitchCase": 1
			,"flatTernaryExpressions": false
			,"offsetTernaryExpressions": true
		}]
		,"brace-style": ["error" ,"stroustrup" ,{
			"allowSingleLine": true
		}]
		,"camelcase": ["error" ,{
			"properties": "always"
			,"ignoreGlobals": true
			,"allow": [
				"^UNSAFE_"
			 ]
		}]
		,"comma-spacing": ["error" ,{
			"before": true
			,"after": false
		}]
		// Manually disable/enable blocks until this bug is fixed...
		,"comma-style": ["error" ,"first"]
		// ,"comma-style": ["error" ,"first", {
		// 	"exceptions": {
		// 		"VariableDeclaration": true
		// 	}
		// }]
		// /Manually disable/enable blocks until this bug is fixed...

		,"eol-last": ["error", "never"]
		,"one-var": ["error" ,"consecutive"]
		,"operator-linebreak": ["error" ,"before" ,{
				"overrides": {
					"=": "after"
				}
			}
		]
		,"space-before-blocks": ["error" ,"never"]
		,"space-before-function-paren": ["error" ,"never"]
	}
}
import { x }	from	'./y.mjs'
export {
	logger as default
	,SomeClass
	,x
}

class SomeClass{
	// code
}

const logger = new SomeClass()

What did you expect to happen?

No complaint from eslint.

What actually happened?

  • logger was used before it was defined. eslint(no-use-before-define)
  • SomeClass was used before it was defined. eslint(no-use-before-define)

See also demo.

Participation

  • I am willing to submit a pull request for this issue.

Additional comments

What is needed for eslint to allow exports at top of modules in combination with no-use-before-define? Because JavaScript processes those keywords before the actual code they represent in modules. The style works perfectly without errors in browsers, but eslint forces you to place the export part at end of the file. Yes i know i could use the export keywords on the same line as the definition of them, but it is better for overview to place them at start like shown in code above.

That’s why i propose an exports flag in no-use-before-define#options) to control the wanted behaviour.


I’ll currently use this work-around for my exports at top until this is fixed/added: 😞

/* eslint-disable no-use-before-define */
export {
	logger as default
	,SomeClass
	,x
}
/* eslint-enable no-use-before-define */

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Reactions:1
  • Comments:16 (10 by maintainers)

github_iconTop GitHub Comments

2reactions
mdjermanoviccommented, Mar 20, 2022

@TriMoon ljharb is referring to our policies about stylistic rules.

@ljharb this rule was originally purely stylistic. As of es6, it can also be used to prevent TDZ errors, and the documentation for this rule does mention TDZ as one of the rationales for using the rule. Since this rule can be used for different purposes, I’m supportive of an option to disable a stylistic-only exports check so that the rule can be configured to only warn about statically determinable TDZ errors.

1reaction
mdjermanoviccommented, Oct 31, 2022

I’m just wondering why you choose to default to false in f6d7920#diff-c6cfabe41e087014db02d66b884c50157300e6309034ba79d548c2b421468f8eR128 Because the behavior of the language allows it by definition by hoisting them to top of the code (I would prefer it to defaulting to true to better reflect es6+ standard parsing)

This rule is primarily stylistic, so by default any reference that appears before the declaration is disallowed. References to function declarations are also disallowed by default, although they are hoisted.

Read more comments on GitHub >

github_iconTop Results From Across the Web

no-use-before-define - ESLint - Pluggable JavaScript Linter
A pluggable and configurable linter tool for identifying and reporting on patterns in JavaScript. Maintain your code quality with ease.
Read more >
array-type | typescript-eslint
Combination Matrix​ ... This matrix lists all possible option combinations and their expected results for different types of Arrays. defaultOption, readonlyOption ...
Read more >
Code Issues - Embold Help Center
When a system is expected to behave in a certain way and it fails to meet the result, the issue occurs. Similarly, any...
Read more >
Bug List - Bugs - Eclipse.org
ID Product Comp Assignee△ Status△ Resolution Changed 513256 Orion JS Tools curtis.windatt.public RESO FIXE 2017‑03‑08 512990 Orion Client grant_gayed RESO FIXE 2017‑03‑13 513276 Orion Node grant_gayed...
Read more >
ovotech/ts-compose - npm
A "Document" here is a combination of a typescript node and its context. ... Format a single field * @param context * @param...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found