From 5c1f475152312a2f8d2db5d71cc594483902c930 Mon Sep 17 00:00:00 2001 From: Miller Medeiros Date: Sun, 7 Aug 2016 12:46:58 -0700 Subject: [PATCH] uses esformatter-parser, improves line break handling. closes #5. closes #3 this should fix our unit tests (see #4) --- README.md | 22 ++++++++++++++++- esformatter-semicolon-first.js | 43 ++++++++++------------------------ package.json | 3 ++- test/compare/input.js | 1 + test/compare/output.js | 1 + 5 files changed, 38 insertions(+), 32 deletions(-) diff --git a/README.md b/README.md index e549218..86cafb6 100644 --- a/README.md +++ b/README.md @@ -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: @@ -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: diff --git a/esformatter-semicolon-first.js b/esformatter-semicolon-first.js index f4a0110..f8fac8d 100644 --- a/esformatter-semicolon-first.js +++ b/esformatter-semicolon-first.js @@ -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(); }; @@ -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'); } }; diff --git a/package.json b/package.json index bb99189..41c254b 100644 --- a/package.json +++ b/package.json @@ -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" } } diff --git a/test/compare/input.js b/test/compare/input.js index b0b5aca..f541687 100644 --- a/test/compare/input.js +++ b/test/compare/input.js @@ -43,3 +43,4 @@ callFunctionLotsOfArgs( // test ES6 support import * as lib from 'lib' +const noop = async () => undefined diff --git a/test/compare/output.js b/test/compare/output.js index 04cdeb3..139c4ef 100644 --- a/test/compare/output.js +++ b/test/compare/output.js @@ -43,3 +43,4 @@ callFunctionLotsOfArgs( // test ES6 support import * as lib from 'lib' +const noop = async () => undefined