Skip to content

Conversation

@saschanaz
Copy link
Contributor

Fixes #4516, fixes #6924.

@saschanaz saschanaz changed the title Fix export import formatting Fix named export/import formatting Sep 2, 2015
@DanielRosenwasser
Copy link
Member

@saschanaz tests/cases/fourslash/formatNamedExportImport.ts seems to test templates, not imports (or exports).

@saschanaz
Copy link
Contributor Author

@DanielRosenwasser I just fixed it, sorry! ;)

@mhegazy
Copy link
Contributor

mhegazy commented Sep 3, 2015

looks like we have a marge conflict with the other formatting changes.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm having trouble understanding why these were removed.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@DanielRosenwasser The SpaceAfterAwaitKeyword and SpaceAfterTypeKeyword rules are merged into SpaceAfterCertainKeywords and SpaceAfterCertainTypeScriptKeywords rules respectively.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What about the NoSpaceAfterAwaitKeyword one? Is that just encompassed by SpaceAfterCertainKeywords?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The other NoSpace ones should not exist. I once thought they will work together with Space rules but then found they should only exist when there are some valid formatting options.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I once thought NoSpace rules would first remove those whitespaces and after that Space rules would add one. However, a Space rule can remove excessive whitespaces by itself.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I once thought NoSpace rules would first remove those whitespaces and after that Space rules would add one. However, a Space rule can remove excessive whitespaces by itself.

That's what I thought as well - it does make sense that one doesn't necessarily need the other though.

@DanielRosenwasser
Copy link
Member

Looks good to me - @vladima can you take a quick peek?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure why this is needed? Can we just do what is done for all other kinds of blocks, namely:

  • add SyntaxKind.NamedImports, SyntaxKind.NamedExports to the list in shouldIndentChildNode function so its content will be indented
  • update isCompletedNode function to correctly process case when cursor is in incompleted NamesImport / NamedExport node by using hasChildOfKind(n, SyntaxKind.CloseBraceToken, sourceFile);

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hasBraceShell function is not to check completeness as ImportClause node can omit its optional braces. I think isCompletedNode function is to check a node has its required children.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@vladima I was wrong and the braces are not optional. I just removed hasBraceShell and added a simpler check.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@saschanaz currently nodeIsCompleted handles cases when code is incomplete i.e.

import {$ // hit enter at $, caret on the next line should be indented
```.

I would expect imports and export to follow the same technique that we already use for all other blocks\block like constructions

saschanaz added a commit to saschanaz/TypeScript that referenced this pull request Sep 9, 2015
@saschanaz saschanaz mentioned this pull request Sep 9, 2015
3 tasks
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you move this comment down? Also, is it correct?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I later changed it to // NamedImports has its own braces as Block does (so do not give it additional indentation)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@vladima Former hasBraceShell (and current namedBindings check) is needed to prevent unwanted excessive indentation. Consider this code:

import
{
    x as xx
}
from "foo"

import
    * as Foo from "foo"

ImportDeclaration has ImportClause node and then ImportClause node can have two different kind of nodes: NamedImports and NamespaceImport.

NamedImports, as you noted, looks like a block, but NamespaceImport doesn't. If I decide to always indent ImportClause or always not to do it, formatting will look bad:

import
    {
        x as xx
    } // when ImportClause is always indented
from "foo"

import
* as Foo from "foo" // when always not

Thus I added this check.

Anyway I agree that isCompletedNode should be updated. I'll push a change for it.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we don't use nulls - use undefined instead

@vladima
Copy link
Contributor

vladima commented Oct 8, 2015

LGTM after comments are addresses

@saschanaz
Copy link
Contributor Author

@vladima Can you also review #4757? shouldInheritParentIndentation part of this PR is in fact duplication of that one. I'll remove the duplication after #4757 is merged.

@mhegazy
Copy link
Contributor

mhegazy commented Dec 9, 2015

looks good. please address the comments about use of null.

@saschanaz
Copy link
Contributor Author

I want to format export/imports and object destructuring (not in this PR) with similar rules but I'm not sure what should I do in this case:

export
{ // Export block in this PR can optionally have a newline before open brace as `if` block can
    x, y
} from "foo"

let
{ // Will anyone want this?
    x, y
} = foo;

@mhegazy
Copy link
Contributor

mhegazy commented Dec 9, 2015

👍
@vladima can you take a look.

@saschanaz saschanaz force-pushed the fixExportImportFormatting branch from d8a8843 to a9245c6 Compare February 19, 2016 04:46
@mhegazy
Copy link
Contributor

mhegazy commented Jun 27, 2016

thanks @saschanaz. can you also add a condition for #9224 to allow users to opt in/out.

@saschanaz saschanaz deleted the fixExportImportFormatting branch September 20, 2016 23:45
@microsoft microsoft locked and limited conversation to collaborators Jun 19, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Format code unindents multi-line import statement Support formatting for named imports/export clauses

5 participants