4

In my code I have a conditional export:

if (process.env.NODE_ENV === 'testing')
  export myFunc;

In es6 this statement is not allowed because imports and exports should be top-level, but using babel with some plugins and webpack this condition is eliminated at build-time, so in my resulting code this export is either top-level or not present.

But eslint reports a parsing error and I want to get rid of it. /*eslint-disable */ doesn't work, because, well, it's not a rule violation, it's a parsing error. Is there a possible workaround?

P.S.: I know I can commonjs it, but I want to stick with es6.

2
  • Is your eslint configured to parse es6? eslint.org/docs/user-guide/… Commented Jan 31, 2016 at 13:29
  • @RadosławM, sure, that's why it reports about wrong es6 syntax Commented Jan 31, 2016 at 13:35

2 Answers 2

2

I know I can commonjs it, but I want to stick with es6.

Really, you aren't compliant with ES6 specification, so... you need to commonjs it.

The Idea behind a transpiler is that you'll can disable it when the next specification comes actual.

In this case, your code won't work in a full ES6 environment.

Sign up to request clarification or add additional context in comments.

Comments

1

There is no workaround.

Using non-top level imports will not work with native ES6 implementations. Babel allows it because it transpiles to CommonJS require, but it is technically not allowed in ES6.

eslint issue and according espree issue

5 Comments

I don't rely on transpiling to commonjs to have conditional export, I use it as a preprocessor directive.
Yes, but eslint parser (espree) see the not transpiled code - if (...) {import ...}
I agree. That's why I want to find a way to make him pretend he doesn't see it the same way I can ask him to pretend that he doesn't see a rule violation.
But it's eslint issue, but espree. You can't ignore espree errors and warnings through eslint-disable rules.
But it's not eslint issue, but espree. *

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.