diff --git a/index.js b/index.js index 844cec4f4..615952e04 100644 --- a/index.js +++ b/index.js @@ -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 + const jsSourceElements = [ + "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; } diff --git a/tests/range/__snapshots__/jsfmt.spec.js.snap b/tests/range/__snapshots__/jsfmt.spec.js.snap index 46dece52d..a27633efc 100644 --- a/tests/range/__snapshots__/jsfmt.spec.js.snap +++ b/tests/range/__snapshots__/jsfmt.spec.js.snap @@ -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 } ) { diff --git a/tests/range/object-expression.js b/tests/range/object-expression.js new file mode 100644 index 000000000..e6545f75f --- /dev/null +++ b/tests/range/object-expression.js @@ -0,0 +1 @@ +const y = {<<>>a:1<<>>, b:2} diff --git a/tests/range/object-expression2.js b/tests/range/object-expression2.js new file mode 100644 index 000000000..526e6e4bf --- /dev/null +++ b/tests/range/object-expression2.js @@ -0,0 +1,10 @@ + +const y = [ + { + a: 1, + },<<>> + { + a: 1, + b:2 + },<<>> +]