Skip to content

Commit

Permalink
add configs for ModuleSpecifier{Opening,Closing}Brace and ModuleSpeci…
Browse files Browse the repository at this point in the history
…fierComma. closes #402
  • Loading branch information
millermedeiros committed Jul 15, 2016
1 parent 23c15ad commit 1cad7ea
Show file tree
Hide file tree
Showing 10 changed files with 110 additions and 23 deletions.
4 changes: 4 additions & 0 deletions lib/hooks/ExportNamedDeclaration.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ var _br = require('rocambole-linebreak');
var _tk = require('rocambole-token');
var _ws = require('rocambole-whitespace');

var ImportDeclaration = require('./ImportDeclaration');

exports.format = function ExportNamedDeclaration(node) {
_br.limitAfter(node.startToken, 0);
_ws.limitAfter(node.startToken, 1);
Expand All @@ -19,3 +21,5 @@ exports.format = function ExportNamedDeclaration(node) {
_ws.limit(fromKeyword, 1);
}
};

exports.getIndentEdges = ImportDeclaration.getIndentEdges;
20 changes: 20 additions & 0 deletions lib/hooks/ImportDeclaration.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,23 @@ exports.format = function ImportDeclaration(node) {
_br.limit(fromKeyword, 0);
_ws.limit(fromKeyword, 1);
};

exports.getIndentEdges = function(node) {
// IMPORTANT: getIndentEdges logic is reused by ExportNamedDeclaration
var braceStart;
node.specifiers.some(function(spec) {
var prev = _tk.findPrev(spec.startToken, _tk.isCode);
if (prev.value === '{') {
braceStart = prev;
return true;
}
});
if (!braceStart) {
return;
}

return {
startToken: braceStart,
endToken: _tk.findNext(node.specifiers[node.specifiers.length - 1].endToken, '}')
};
};
32 changes: 19 additions & 13 deletions lib/hooks/ImportSpecifier.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,35 +5,41 @@
var _br = require('rocambole-linebreak');
var _tk = require('rocambole-token');
var _ws = require('rocambole-whitespace');
var limit = require('../limit');

exports.format = function(node) {
var braceStart = _tk.findPrev(node.startToken, _tk.isCode);
var braceEnd = _tk.findNext(node.endToken, _tk.isCode);

// handle `import foo, { lorem, ipsum } from 'lib';`
if (braceStart.value === '{' || braceStart.value === ',') {
_br.limit(braceStart, 0);
_ws.limitBefore(braceStart, braceStart.value === '{' ? 1 : 0);
_ws.limitAfter(braceStart, braceStart.value === '{' ? 'ModuleSpecifierOpeningBrace' : 1);
if (braceStart.value === '{') {
limit.around(braceStart, 'ModuleSpecifierOpeningBrace');
} else if (braceStart.value === ',') {
limit.around(braceStart, 'ModuleSpecifierComma');
}

if (braceEnd.value === '}' || braceEnd.value === ',') {
if (braceEnd.value === ',') {
limit.around(braceEnd, 'ModuleSpecifierComma');
} else if (braceEnd.value === '}') {
limit.before(braceEnd, 'ModuleSpecifierClosingBrace');

var next = _tk.findNextNonEmpty(braceEnd);
var followedBySemi = next && next.value === ';';
if (followedBySemi) {
_br.limit(braceEnd, 0);
if (next && next.value === ';') {
// handle `export {foo, bar};`
_br.limitAfter(braceEnd, 0);
} else if (node.parent.endToken !== braceEnd) {
// we don't want to limit line break for lines that contains just
// `export {foo, bar}` because that would remove undesired line breaks
limit.after(braceEnd, 'ModuleSpecifierClosingBrace');
}
_ws.limitAfter(braceEnd, followedBySemi ? 0 : 1);
_ws.limitBefore(braceEnd, braceEnd.value === '}' ? 'ModuleSpecifierClosingBrace' : 0);
}

_br.limit(node.startToken, 0);

if (node.startToken.value !== node.endToken.value) {
_br.limit(node.endToken, 0);
// handle spaces around "as"
// eg: `import { named1 as myNamed1 } from 'lib'`
// eg: `import * as myLib from 'lib'`
_br.limitAfter(node.startToken, 0);
_br.limitBefore(node.endToken, 0);
_ws.limitAfter(node.startToken, 1);
_ws.limitBefore(node.endToken, 1);
}
Expand Down
2 changes: 2 additions & 0 deletions lib/preset/default-indent.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,14 @@ module.exports = {
"ClassDeclaration": 1,
"ClassExpression": 1,
"DoWhileStatement": 1,
"ExportNamedDeclaration": 1,
"ForInStatement": 1,
"ForOfStatement": 1,
"ForStatement": 1,
"FunctionDeclaration": 1,
"FunctionExpression": 1,
"IfStatement": 1,
"ImportDeclaration": 1,
"MemberExpression": 1,
"MultipleVariableDeclaration": 1,
"NewExpression": 1,
Expand Down
3 changes: 3 additions & 0 deletions lib/preset/default-linebreak-after.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,9 @@ module.exports = {
"MemberExpressionOpening": "<2",
"MemberExpressionClosing": "<2",
"MemberExpressionPeriod": 0,
"ModuleSpecifierClosingBrace": 0,
"ModuleSpecifierComma": 0,
"ModuleSpecifierOpeningBrace": 0,
"ObjectExpressionOpeningBrace" : ">=1",
"ObjectPatternOpeningBrace": 0,
"ObjectPatternClosingBrace": 0,
Expand Down
3 changes: 3 additions & 0 deletions lib/preset/default-linebreak-before.js
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,9 @@ module.exports = {
"MemberExpressionOpening": 0,
"MemberExpressionClosing": "<2",
"MemberExpressionPeriod": -1,
"ModuleSpecifierClosingBrace": 0,
"ModuleSpecifierComma": 0,
"ModuleSpecifierOpeningBrace": 0,
"ObjectExpressionClosingBrace" : ">=1",
"ObjectPatternOpeningBrace": 0,
"ObjectPatternClosingBrace": 0,
Expand Down
3 changes: 3 additions & 0 deletions lib/preset/default-whitespace-after.js
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,9 @@ module.exports = {
"MemberExpressionOpening": 0,
"MemberExpressionPeriod": 0,
"MethodDefinitionName": 0,
"ModuleSpecifierClosingBrace": 0,
"ModuleSpecifierComma": 1,
"ModuleSpecifierOpeningBrace": 1,
"LogicalExpressionOperator" : 1,
"ObjectExpressionOpeningBrace": 0,
"ObjectExpressionClosingBrace": 0,
Expand Down
3 changes: 3 additions & 0 deletions lib/preset/default-whitespace-before.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,9 @@ module.exports = {
"MemberExpressionOpening": 0,
"MemberExpressionClosing": 0,
"MemberExpressionPeriod": 0,
"ModuleSpecifierClosingBrace": 1,
"ModuleSpecifierComma": 0,
"ModuleSpecifierOpeningBrace": 1,
"ObjectExpressionOpeningBrace": -1,
"ObjectExpressionClosingBrace": 0,
"ObjectPatternOpeningBrace": 1,
Expand Down
16 changes: 13 additions & 3 deletions test/compare/custom/module-config.json
Original file line number Diff line number Diff line change
@@ -1,10 +1,20 @@
{
"whiteSpace": {
"lineBreak": {
"before": {
"ModuleSpecifierClosingBrace": 3
"ExportAllDeclaration": 2,
"ExportDefaultDeclaration": 2,
"ExportNamedDeclaration": 2,
"ModuleSpecifierClosingBrace": 1,
"ModuleSpecifierComma": 0
},
"after": {
"ModuleSpecifierOpeningBrace": 3
"ExportAllDeclaration": 2,
"ExportDefaultDeclaration": 2,
"ExportNamedDeclaration": 2,
"ImportDeclaration": 2,
"ModuleSpecifierOpeningBrace": 1,
"ModuleSpecifierClosingBrace": -1,
"ModuleSpecifierComma": 1
}
}
}
47 changes: 40 additions & 7 deletions test/compare/custom/module-out.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,23 @@
// samples borrowed from http://www.2ality.com/2014/09/es6-modules-final.html

// Default exports and named exports
import theDefault, { named1, named2 } from 'src/mylib';
import theDefault, {
named1,
named2
} from 'src/mylib';

import theDefault from 'src/mylib';
import { named1, named2 } from 'src/mylib';

import {
named1,
named2
} from 'src/mylib';

// Renaming: import named1 as myNamed1
import { named1 as myNamed1, named2 } from 'src/mylib';
import {
named1 as myNamed1,
named2
} from 'src/mylib';

// Importing the module as an object
// (with one property per named export)
Expand All @@ -16,7 +27,9 @@ import * as mylib from 'src/mylib';
import 'src/mylib';

export var myVar1 = 123;

export let myVar2 = 'foo';

export const MY_CONST = 'BAR';

export function myFunc() {
Expand All @@ -26,27 +39,47 @@ export function myFunc() {
export function* myGeneratorFunc() {
return yield someStuff();
}

export class MyClass {
constructor() {
this.name = 'exports test';
}
}

export default 123;

export default function (x) {
return x
}

export default x => x;

export default class {
constructor(x, y) {
this.x = x;
this.y = y;
}
}

export { MY_CONST, myFunc };
export { MY_CONST as THE_CONST, myFunc as theFunc };
export {
MY_CONST,
myFunc
};

export {
MY_CONST as THE_CONST,
myFunc as theFunc
};

export * from 'src/other_module';
export { foo, bar } from 'src/other_module';
export { foo as myFoo, bar } from 'src/other_module';

export {
foo,
bar
} from 'src/other_module';

export {
foo as myFoo,
bar
} from 'src/other_module';

0 comments on commit 1cad7ea

Please sign in to comment.