This is an archive of the discontinued LLVM Phabricator instance.

clang-format: [JS] do not wrap ES6 imports/exports.
ClosedPublic

Authored by mprobst on Feb 19 2016, 1:58 AM.

Details

Reviewers
djasper
Summary

"import ... from '...';" and "export ... from '...';" should be treated the same
as goog.require/provide/module/forwardDeclare calls.

Diff Detail

Event Timeline

mprobst updated this revision to Diff 48465.Feb 19 2016, 1:58 AM
mprobst retitled this revision from to clang-format: [JS] do not wrap ES6 imports/exports..
mprobst updated this object.
mprobst added a reviewer: djasper.
mprobst added subscribers: cfe-commits, klimek.

FYI this should not be submitted yet, there are still style discussions going on.

djasper edited edge metadata.Feb 22 2016, 7:14 AM

I am missing a decent explanation here. In contrast to C++ #includes as well as goog.require, etc. The import/export statements actually provide syntactic structure and can name multiple entities. Why would it be a good idea to always write them into a single line?

unittests/Format/FormatTestJS.cpp
867

Really?

mprobst marked an inline comment as done.EditedMar 21 2016, 10:04 AM

As discussed offline, this matches existing very similar behaviour for destructured goog.require calls:

const {X, Y, Z} = goog.require('a'); // won't ever wrap
import {X, Y, Z} from 'a';  // Shouldn't ever wrap

It also has the nice side effect that it prevents IWYU comments of the form:

import {X, Y, Z} from 'a';  // from //some/location

... from wrapping.

djasper added inline comments.Mar 22 2016, 3:09 AM
lib/Format/TokenAnnotator.cpp
760

Can you actually have:

export {a as from};

?

763

Not a big fan of the code duplication in this loop. How about doing:

if (Style.Language == FormatStyle::LK_JavaScript &&
    ((Line.First->is(tok::kw_export) && CurrentToken->is(Keywords.kw_from)) ||
     isClosureImportStatement(*CurrentToken)))
  return LT_ImportStatement;

in the lower loop? The ImportStatement variable can likely go away then.

mprobst marked an inline comment as done.Mar 22 2016, 4:22 AM
mprobst added inline comments.
lib/Format/TokenAnnotator.cpp
760

Good observation. Fixed by specifically looking for from 'some tring';.

763

Merged with the code below. My intent was to not do this check for JavaScript code that doesn't start with import or export, but merging seems fair, too.

mprobst updated this revision to Diff 51267.Mar 22 2016, 4:23 AM
mprobst marked an inline comment as done.
mprobst edited edge metadata.
  • Address review comments:
mprobst updated this revision to Diff 51268.Mar 22 2016, 4:24 AM

Rebase diff on current master.

mprobst updated this revision to Diff 51269.Mar 22 2016, 4:24 AM
  • Address review comments:
  • Add one more explanatory comment.
mprobst marked 3 inline comments as done.Mar 22 2016, 4:25 AM
djasper accepted this revision.Mar 22 2016, 7:14 AM
djasper edited edge metadata.

Looks good. Do you have commit access now?

This revision is now accepted and ready to land.Mar 22 2016, 7:14 AM

I've asked for it, but don't have it yet. Feel free to commit on by behalf.

djasper closed this revision.Mar 22 2016, 7:37 AM

Submitted as r264055.