Skip to content

Commit

Permalink
Enable compact option in Babel generator
Browse files Browse the repository at this point in the history
  • Loading branch information
Jonathan Gruber committed Jul 3, 2019
1 parent 0935d14 commit c76ad72
Show file tree
Hide file tree
Showing 8 changed files with 37 additions and 15 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "babel-plugin-reflow",
"version": "0.1.0",
"version": "0.1.1",
"description": "Babel plugin to transpile Flow code to TypeScript",
"author": "Jonathan Gruber <gruberjonathan@gmail.com>",
"license": "MIT",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ type Props = {
},
};

class Foo extends Component<Props> {
class RealWorld extends Component<Props> {
get transitions(): Array<TransitionStyle> {
const { children, enterStyle } = this.props;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@

import React, { Component, Children, ReactNode } from 'react';
import { TransitionMotion } from 'react-motion';
import {
Expand Down Expand Up @@ -62,7 +63,7 @@ type Props = {
};
};

class Foo extends Component<Props> {
class RealWorld extends Component<Props> {
get transitions(): Array<TransitionStyle> {
const { children, enterStyle } = this.props;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,12 @@ const pluginOptions: ReflowOptions = {
describe('Optimizations', () => {
runFixtureTests(
resolve('src/plugin/__tests__/__fixtures__/optimizations/'),
getTransformOptions({ pluginOptions }),
getTransformOptions({
transformOptions: {
compact: false,
},
pluginOptions,
}),
pluginOptions,
);
});
6 changes: 5 additions & 1 deletion src/plugin/__tests__/__fixtures__/react/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ import { runFixtureTests } from '../../../util/test-runner';
describe('React', () => {
runFixtureTests(
resolve('src/plugin/__tests__/__fixtures__/react/'),
getTransformOptions(),
getTransformOptions({
transformOptions: {
compact: false,
}
}),
);
});
6 changes: 5 additions & 1 deletion src/plugin/__tests__/__fixtures__/types/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ import { runFixtureTests } from '../../../util/test-runner';
describe('Types', () => {
runFixtureTests(
resolve('src/plugin/__tests__/__fixtures__/types/'),
getTransformOptions(),
getTransformOptions({
transformOptions: {
compact: false,
}
}),
);
});
9 changes: 5 additions & 4 deletions src/plugin/options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,20 @@ import reflowPlugin, { ReflowOptions } from '.';
* Create the Babel configuration for runners using the plugin
*/
export function getTransformOptions(
args: {
overwrites: {
pluginOptions?: ReflowOptions;
transformOptions?: TransformOptions;
} = {},
): TransformOptions {
const defaultOptions: TransformOptions = {
babelrc: false,
configFile: false,
comments: false,
plugins: [[reflowPlugin, args.pluginOptions]],
compact: true,
configFile: false,
plugins: [[reflowPlugin, overwrites.pluginOptions]],
};

return Object.assign({}, defaultOptions, args.transformOptions);
return Object.assign({}, defaultOptions, overwrites.transformOptions);
}

/**
Expand Down
17 changes: 12 additions & 5 deletions src/plugin/util/format.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ function copyComments(
* Since Babel uses an *abstract* synax tree, all information about whitespace
* is lost after parsing. Also Babel's `retainLines` option is not working as
* expected and will produce broken syntax in some cases. Neither will Prettier
* help concerning blank lines, because it won't add additional blank lines:
* help here, because it does not add additional blank lines:
*
* https://prettier.io/docs/en/rationale.html#empty-lines
*
Expand All @@ -101,7 +101,7 @@ function copyComments(
* the source to the output. Comments are also handled here, because Babel does
* not reliably retain their position in the generated code. This approach
* naively assumes that all code transformations will result in (roughly) the
* same amount of lines. It's not perfect, but the best I came up with and it
* same amount of lines. It iss not perfect, but the best I came up with and it
* seems to work reasonably well in practice.
*/
export function syncBlankLinesAndComments(
Expand All @@ -114,11 +114,11 @@ export function syncBlankLinesAndComments(
.split(LINE_BREAK)
.filter(line => !FLOW_DIRECTIVE.test(line));

let outputLines = outputCode.split(LINE_BREAK).filter(line => !BLANK_LINE.test(line));

const flowAst = parseAst(originalLines.join('\n'));
const decoratorList = getDecorators(flowAst);

let outputLines = outputCode.split(LINE_BREAK);

originalLines.forEach((flowLine, lineNumber) => {
if (outputLines[lineNumber] === undefined || flowLine === outputLines[lineNumber]) {
return;
Expand Down Expand Up @@ -177,7 +177,14 @@ export function formatOutputCode(
}),
);

outputCode = prettier.format(outputCode, getPrettierConfig(prettierOptions));
outputCode = prettier.format(
outputCode,
getPrettierConfig({
parser: 'typescript',
...prettierOptions,
}),
);

outputCode = syncBlankLinesAndComments(outputCode, originalCode, pluginOptions);

// Run Prettier one more time to iron out any remaining bad formatting.
Expand Down

0 comments on commit c76ad72

Please sign in to comment.