Skip to content

Commit 074a377

Browse files
authored
fix(format): deal with absent .clang-format (#176)
This fixes a regression in v0.7.0
1 parent eab41e1 commit 074a377

File tree

2 files changed

+32
-7
lines changed

2 files changed

+32
-7
lines changed

src/format.ts

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,16 @@
1313
* See the License for the specific language governing permissions and
1414
* limitations under the License.
1515
*/
16+
import * as fs from 'fs';
17+
import * as path from 'path';
1618
import {Options} from './cli';
1719
import {createProgram} from './lint';
1820

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

21-
const baseArgs = ['-style=file'];
23+
const BASE_ARGS_FILE = ['-style=file'];
24+
const BASE_ARGS_INLINE =
25+
['-style', '{Language: JavaScript, BasedOnStyle: Google, ColumnLimit: 80}'];
2226

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

40+
// If the project has a .clang-format – use it. Else use the default as an
41+
// inline argument.
42+
const baseClangFormatArgs =
43+
fs.existsSync(path.join(options.targetRootDir, '.clang-format')) ?
44+
BASE_ARGS_FILE :
45+
BASE_ARGS_INLINE;
46+
3647
const program = createProgram(options);
3748
// Obtain a list of source files to format.
3849
// We use program.getRootFileNames to get only the files that match the
@@ -45,9 +56,9 @@ export async function format(
4556
program.getRootFileNames().filter(f => !f.endsWith('.d.ts'));
4657

4758
if (fix) {
48-
return await fixFormat(srcFiles);
59+
return await fixFormat(srcFiles, baseClangFormatArgs);
4960
} else {
50-
const result = await checkFormat(srcFiles);
61+
const result = await checkFormat(srcFiles, baseClangFormatArgs);
5162
if (!result) {
5263
options.logger.log(
5364
'clang-format reported errors... run `gts fix` to address.');
@@ -61,7 +72,7 @@ export async function format(
6172
*
6273
* @param srcFiles list of source files
6374
*/
64-
function fixFormat(srcFiles: string[]): Promise<boolean> {
75+
function fixFormat(srcFiles: string[], baseArgs: string[]): Promise<boolean> {
6576
return new Promise<boolean>((resolve, reject) => {
6677
const args = baseArgs.concat(['-i'], srcFiles);
6778
clangFormat.spawnClangFormat(args, (err?: Error) => {
@@ -80,7 +91,7 @@ function fixFormat(srcFiles: string[]): Promise<boolean> {
8091
*
8192
* @param srcFiles list of source files
8293
*/
83-
function checkFormat(srcFiles: string[]): Promise<boolean> {
94+
function checkFormat(srcFiles: string[], baseArgs: string[]): Promise<boolean> {
8495
return new Promise<boolean>((resolve, reject) => {
8596
let output = '';
8697
const args = baseArgs.concat(['-output-replacements-xml'], srcFiles);

test/test-format.ts

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,8 @@ import {nop} from '../src/util';
2525
import {withFixtures} from './fixtures';
2626

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

3131
const OPTIONS: Options = {
3232
gtsRootDir: path.resolve(__dirname, '../..'),
@@ -164,3 +164,17 @@ test.serial('format should not auto fix on dry-run', t => {
164164
t.deepEqual(contents, BAD_CODE);
165165
});
166166
});
167+
168+
test.serial('format should use user provided config', t => {
169+
return withFixtures(
170+
{
171+
'tsconfig.json': JSON.stringify({files: ['a.ts']}),
172+
'.clang-format': 'Language: JavaScript',
173+
'a.ts':
174+
BAD_CODE // but actually good under the custom JS format config.
175+
},
176+
async () => {
177+
const result = await format.format(OPTIONS, [], false);
178+
t.true(result);
179+
});
180+
});

0 commit comments

Comments
 (0)