Skip to content
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

Split source elements relative to their language. #3069

Merged
merged 3 commits into from Oct 22, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
120 changes: 67 additions & 53 deletions index.js
Expand Up @@ -204,59 +204,73 @@ function isSourceElement(opts, node) {
if (node == null) {
return false;
}
switch (node.type || node.kind) {
case "ObjectExpression": // JSON
case "ArrayExpression": // JSON
case "StringLiteral": // JSON
case "NumericLiteral": // JSON
case "BooleanLiteral": // JSON
case "NullLiteral": // JSON
case "FunctionDeclaration":
case "BlockStatement":
case "BreakStatement":
case "ContinueStatement":
case "DebuggerStatement":
case "DoWhileStatement":
case "EmptyStatement":
case "ExpressionStatement":
case "ForInStatement":
case "ForStatement":
case "IfStatement":
case "LabeledStatement":
case "ReturnStatement":
case "SwitchStatement":
case "ThrowStatement":
case "TryStatement":
case "VariableDeclaration":
case "WhileStatement":
case "WithStatement":
case "ClassDeclaration": // ES 2015
case "ImportDeclaration": // Module
case "ExportDefaultDeclaration": // Module
case "ExportNamedDeclaration": // Module
case "ExportAllDeclaration": // Module
case "TypeAlias": // Flow
case "InterfaceDeclaration": // Flow, Typescript
case "TypeAliasDeclaration": // Typescript
case "ExportAssignment": // Typescript
case "ExportDeclaration": // Typescript
case "OperationDefinition": // GraphQL
case "FragmentDefinition": // GraphQL
case "VariableDefinition": // GraphQL
case "TypeExtensionDefinition": // GraphQL
case "ObjectTypeDefinition": // GraphQL
case "FieldDefinition": // GraphQL
case "DirectiveDefinition": // GraphQL
case "EnumTypeDefinition": // GraphQL
case "EnumValueDefinition": // GraphQL
case "InputValueDefinition": // GraphQL
case "InputObjectTypeDefinition": // GraphQL
case "SchemaDefinition": // GraphQL
case "OperationTypeDefinition": // GraphQL
case "InterfaceTypeDefinition": // GraphQL
case "UnionTypeDefinition": // GraphQL
case "ScalarTypeDefinition": // GraphQL
return true;
// JS and JS like to avoid repetitions
Copy link
Collaborator

Choose a reason for hiding this comment

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

Is this supposed to be "JS and TS like to avoid repetitions"?

Copy link
Member

Choose a reason for hiding this comment

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

I interpreted as JS-like = flow, TypeScript.

const jsSourceElements = [
Copy link
Member Author

@CiGit CiGit Oct 19, 2017

Choose a reason for hiding this comment

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

What's the rule with global variables? those const ...SourceElements could be defined outside.

"FunctionDeclaration",
"BlockStatement",
"BreakStatement",
"ContinueStatement",
"DebuggerStatement",
"DoWhileStatement",
"EmptyStatement",
"ExpressionStatement",
"ForInStatement",
"ForStatement",
"IfStatement",
"LabeledStatement",
"ReturnStatement",
"SwitchStatement",
"ThrowStatement",
"TryStatement",
"VariableDeclaration",
"WhileStatement",
"WithStatement",
"ClassDeclaration", // ES 2015
"ImportDeclaration", // Module
"ExportDefaultDeclaration", // Module
"ExportNamedDeclaration", // Module
"ExportAllDeclaration", // Module
"TypeAlias", // Flow
"InterfaceDeclaration", // Flow, Typescript
"TypeAliasDeclaration", // Typescript
"ExportAssignment", // Typescript
"ExportDeclaration" // Typescript
];
const jsonSourceElements = [
"ObjectExpression",
"ArrayExpression",
"StringLiteral",
"NumericLiteral",
"BooleanLiteral",
"NullLiteral"
];
const graphqlSourceElements = [
"OperationDefinition",
"FragmentDefinition",
"VariableDefinition",
"TypeExtensionDefinition",
"ObjectTypeDefinition",
"FieldDefinition",
"DirectiveDefinition",
"EnumTypeDefinition",
"EnumValueDefinition",
"InputValueDefinition",
"InputObjectTypeDefinition",
"SchemaDefinition",
"OperationTypeDefinition",
"InterfaceTypeDefinition",
"UnionTypeDefinition",
"ScalarTypeDefinition"
];
switch (opts.parser) {
case "flow":
case "babylon":
case "typescript":
return jsSourceElements.indexOf(node.type) > -1;
case "json":
return jsonSourceElements.indexOf(node.type) > -1;
case "graphql":
return graphqlSourceElements.indexOf(node.kind) > -1;
}
return false;
}
Expand Down
32 changes: 32 additions & 0 deletions tests/range/__snapshots__/jsfmt.spec.js.snap
Expand Up @@ -263,6 +263,38 @@ catch (err) {}

`;

exports[`object-expression.js 1`] = `
const y = {a:1, b:2}
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
const y = { a: 1, b: 2 };

`;

exports[`object-expression2.js 1`] = `

const y = [
{
a: 1,
},
{
a: 1,
b:2
},
]
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

const y = [
{
a: 1
},
{
a: 1,
b: 2
}
];

`;

exports[`range.js 1`] = `
function ugly ( {a=1, b = 2 } ) {
function ugly ( {a=1, b = 2 } ) {
Expand Down
1 change: 1 addition & 0 deletions tests/range/object-expression.js
@@ -0,0 +1 @@
const y = {<<<PRETTIER_RANGE_START>>>a:1<<<PRETTIER_RANGE_END>>>, b:2}
10 changes: 10 additions & 0 deletions tests/range/object-expression2.js
@@ -0,0 +1,10 @@

const y = [
{
a: 1,
},<<<PRETTIER_RANGE_START>>>
{
a: 1,
Copy link
Member

Choose a reason for hiding this comment

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

Could you add an additional property to this object? Otherwise it could be treated as a block + label.

Copy link
Member Author

Choose a reason for hiding this comment

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

Right, it's certainly clearer than trying to find a semicolon {a: 1;}.
Done.

b:2
},<<<PRETTIER_RANGE_END>>>
]