Skip to content

Commit

Permalink
fix(format): deal with absent .clang-format (#176)
Browse files Browse the repository at this point in the history
This fixes a regression in v0.7.0
  • Loading branch information
ofrobots authored Jun 18, 2018
1 parent eab41e1 commit 074a377
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 7 deletions.
21 changes: 16 additions & 5 deletions src/format.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,16 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import * as fs from 'fs';
import * as path from 'path';
import {Options} from './cli';
import {createProgram} from './lint';

const clangFormat = require('clang-format');

const baseArgs = ['-style=file'];
const BASE_ARGS_FILE = ['-style=file'];
const BASE_ARGS_INLINE =
['-style', '{Language: JavaScript, BasedOnStyle: Google, ColumnLimit: 80}'];

/**
* Run tslint fix and clang fix with the default configuration
Expand All @@ -33,6 +37,13 @@ export async function format(
fix = false;
}

// If the project has a .clang-format – use it. Else use the default as an
// inline argument.
const baseClangFormatArgs =
fs.existsSync(path.join(options.targetRootDir, '.clang-format')) ?
BASE_ARGS_FILE :
BASE_ARGS_INLINE;

const program = createProgram(options);
// Obtain a list of source files to format.
// We use program.getRootFileNames to get only the files that match the
Expand All @@ -45,9 +56,9 @@ export async function format(
program.getRootFileNames().filter(f => !f.endsWith('.d.ts'));

if (fix) {
return await fixFormat(srcFiles);
return await fixFormat(srcFiles, baseClangFormatArgs);
} else {
const result = await checkFormat(srcFiles);
const result = await checkFormat(srcFiles, baseClangFormatArgs);
if (!result) {
options.logger.log(
'clang-format reported errors... run `gts fix` to address.');
Expand All @@ -61,7 +72,7 @@ export async function format(
*
* @param srcFiles list of source files
*/
function fixFormat(srcFiles: string[]): Promise<boolean> {
function fixFormat(srcFiles: string[], baseArgs: string[]): Promise<boolean> {
return new Promise<boolean>((resolve, reject) => {
const args = baseArgs.concat(['-i'], srcFiles);
clangFormat.spawnClangFormat(args, (err?: Error) => {
Expand All @@ -80,7 +91,7 @@ function fixFormat(srcFiles: string[]): Promise<boolean> {
*
* @param srcFiles list of source files
*/
function checkFormat(srcFiles: string[]): Promise<boolean> {
function checkFormat(srcFiles: string[], baseArgs: string[]): Promise<boolean> {
return new Promise<boolean>((resolve, reject) => {
let output = '';
const args = baseArgs.concat(['-output-replacements-xml'], srcFiles);
Expand Down
18 changes: 16 additions & 2 deletions test/test-format.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ import {nop} from '../src/util';
import {withFixtures} from './fixtures';

// clang-format won't pass this code because of trailing spaces.
const BAD_CODE = 'export const foo = 2; ';
const GOOD_CODE = 'export const foo = 2;';
const BAD_CODE = 'export const foo = [ 2 ];';
const GOOD_CODE = 'export const foo = [2];';

const OPTIONS: Options = {
gtsRootDir: path.resolve(__dirname, '../..'),
Expand Down Expand Up @@ -164,3 +164,17 @@ test.serial('format should not auto fix on dry-run', t => {
t.deepEqual(contents, BAD_CODE);
});
});

test.serial('format should use user provided config', t => {
return withFixtures(
{
'tsconfig.json': JSON.stringify({files: ['a.ts']}),
'.clang-format': 'Language: JavaScript',
'a.ts':
BAD_CODE // but actually good under the custom JS format config.
},
async () => {
const result = await format.format(OPTIONS, [], false);
t.true(result);
});
});

0 comments on commit 074a377

Please sign in to comment.