Skip to content

Commit

Permalink
uses esformatter-parser, improves line break handling. closes #5. closes
Browse files Browse the repository at this point in the history
 #3

this should fix our unit tests (see #4)
  • Loading branch information
millermedeiros committed Aug 7, 2016
1 parent 030f123 commit 5c1f475
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 32 deletions.
22 changes: 21 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ semicolon before `[` and `(` if they are the first things on the line.

created mainly to be used by [standard-format](https://github.com/maxogden/standard-format)

## usage
## Usage

Add to your esformatter config file:

Expand All @@ -24,6 +24,26 @@ Or you can manually register the plugin if not using `esformatter` directly:
esformatter.register(require('esformatter-semicolon-first'));
```

# Config

By default we add at least one line break before the semicolon and remove line
breaks afterwards, but you can use the
`lineBreak.before['esformatter-semicolon-first']` and
`lineBreak.after['esformatter-semicolon-first']` to configure it tho.

```json
{
"lineBreak": {
"before": {
"esformatter-semicolon-first": ">0"
},
"after": {
"esformatter-semicolon-first": 0
}
}
}
```

## Examples

Given this input program:
Expand Down
43 changes: 13 additions & 30 deletions esformatter-semicolon-first.js
Original file line number Diff line number Diff line change
@@ -1,42 +1,25 @@
//jshint node:true, eqnull:true
'use strict';

var tk = require('rocambole-token');
var br = require('rocambole-linebreak');
var parser = require('esformatter-parser');
var rocambole = require('rocambole');
var espree = require('espree');

var parseOptions = {
ecmaFeatures: {
arrowFunctions: true,
blockBindings: true,
destructuring: true,
regexYFlag: true,
regexUFlag: true,
templateStrings: true,
binaryLiterals: true,
octalLiterals: true,
unicodeCodePointEscapes: true,
defaultParams: true,
restParams: true,
forOf: true,
objectLiteralComputedProperties: true,
objectLiteralShorthandMethods: true,
objectLiteralShorthandProperties: true,
objectLiteralDuplicateProperties: true,
generators: true,
spread: true,
classes: true,
modules: true,
jsx: true,
globalReturn: true
var tk = require('rocambole-token');

exports.setOptions = function(options) {
if (!('esformatter-semicolon-first' in options.lineBreak.before)) {
options.lineBreak.before['esformatter-semicolon-first'] = '>0';
}
if (!('esformatter-semicolon-first' in options.lineBreak.after)) {
options.lineBreak.after['esformatter-semicolon-first'] = 0;
}
br.setOptions(options.lineBreak);
};

// need to use `stringBefore` since we are actually changing the
// behavior/structure of the program by introducing the semi-colons
exports.stringBefore = function(str) {
rocambole.parseFn = espree.parse.bind(espree);
var ast = rocambole.parse(str, parseOptions);
var ast = parser.parse(str);
tk.eachInBetween(ast.startToken, ast.endToken, processToken);
return ast.toString();
};
Expand All @@ -48,7 +31,7 @@ exports.tokenAfter = function(token) {

var prev = tk.findPrevNonEmpty(token);
if (prev && prev.value === ';') {
tk.removeEmptyInBetween(prev, token);
br.limit(prev, 'esformatter-semicolon-first');
}
};

Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,9 @@
"esformatter": ">=0.7.0 <2.0"
},
"dependencies": {
"espree": "^2.0.3",
"esformatter-parser": "^1.0",
"rocambole": ">=0.6.0 <2.0",
"rocambole-linebreak": "^1.0.2",
"rocambole-token": "^1.2.1"
}
}
1 change: 1 addition & 0 deletions test/compare/input.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,3 +43,4 @@ callFunctionLotsOfArgs(

// test ES6 support
import * as lib from 'lib'
const noop = async () => undefined
1 change: 1 addition & 0 deletions test/compare/output.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,3 +43,4 @@ callFunctionLotsOfArgs(

// test ES6 support
import * as lib from 'lib'
const noop = async () => undefined

0 comments on commit 5c1f475

Please sign in to comment.