Giter Club home page Giter Club logo

gulp-clang-format's Introduction

gulp-clang-format Build Status

Gulp plugin to check that code is properly formatted, according to clang-format.

If the code differs from how clang-format would format it, this prints a warning to the build output, as well as a command to run that formats the file in-place.

Usage

Sample gulpfile.js:

var format = require('gulp-clang-format');

var srcsToFmt = ['path/**/*.js'];

gulp.task('check-format', function() {
  return gulp.src(srcsToFmt)
     .pipe(format.checkFormat());
});

gulp.task('format', function() {
  // The base option ensures the glob doesn't strip prefixes
  return gulp.src(srcsToFmt, {base: '.'})
      .pipe(format.format())
      .pipe(gulp.dest('.'));
});

Promoting warnings to errors

If you want to enforce the formatting, so that other team members don't introduce code that gives you a warning, you can turn them into build errors by acting on the 'warning' event. For example, this task exits the build immediately:

var format = require('gulp-clang-format');

gulp.task('check-format', function() {
  return gulp.src('*.js')
      .pipe(format.checkFormat('file'))
      .on('warning', function(e) {
        process.stdout.write(e.message);
        process.exit(1);
      });
});

Options

The format() and checkFormat() both accept two options: opt_clangStyle and opt_clangFormat. checkFormat() also accepts a third option, opt_gulpOptions.

opt_clangStyle

An optional parameter indicating the clang-format style to use. By default, it applies the "Google" style.

The parameter is passed to the -style argument of clang-format. See the docs here: http://clang.llvm.org/docs/ClangFormatStyleOptions.html

The recommended value is the string 'file', this means that clang-format will look for a .clang-format file in your repository. This allows you to keep the formatting consistent with other developers.

opt_clangFormat

The resolved clang-format module to use. Useful to pass a specific clang-format version to the task.

var format = require('gulp-clang-format');
var clangFormat = require('clang-format');

gulp.task('check-format', function() {
  return gulp.src('*.js')
      .pipe(format.checkFormat('file', clangFormat));
});

opt_gulpOptions

Options for the gulp operation. Supported options are

  • verbose, which causes a diff of all changed files to be printed.
  • fail, which causes the task to emit an error instead of a warning.
var format = require('gulp-clang-format');

gulp.task('check-format', function() {
  return gulp.src('*.js')
      .pipe(format.checkFormat(undefined, undefined, {verbose: true, fail: true}));
});

gulp-clang-format's People

Contributors

alan-agius4 avatar alexeagle avatar dependabot[bot] avatar devversion avatar igorminar avatar mprobst avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

gulp-clang-format's Issues

Sporadic false positives due to stream-equal 0.1.7

When upgrading transitive dependencies of gulp-clang-format in angular/angular I started seeing frequent but random false positives when running gulp check-format.

I was able to narrow the issue down to gulp-clang-format's stream-equal dependency. Version 0.1.6 works just fine but 0.1.7 exhibits this problem.

I haven't investigated if this is happening due to a bug in stream-equal or due to how we use it.

For now I'm going to limit the stream-equal transitive dependency of gulp-clang-format to <=0.1.6.

format() does not change the file modification time

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

gulp.task('format', () =>
{
	let src = 'src/**/*.cpp';

	return gulp.src(src, { base: '.' })
		.pipe(clangFormat.format('file'))
		.pipe(gulp.dest('.'));
});

Runnin the gulp format task achieves its purpose and modifies source files according to rules in .clang-format. But, the modification time of those modified files do not change.

This is annoying when you have such a file into a text editor, run gulp format in a terminal, the file is modified, but the text editor has no way to realize of it so the display is not refreshed.

As a suggestion, format() may invoke touch in modified files.

P.S.: This may be an issue of clang-format itself.

version skew when running suggested command

When the formatting is not correct, we print a clang-format command line for how to fix it.
That relies on the version of clang-format installed globally by npm, which may differ from the version used in the project's node_modules.
As a result, the formatting produced by running the command may not fix the warnings from the gulp build. We should print a command line which uses the same version as gulp task will use.

Overwrite code

Instead of providing a formatted directory, is it possible to overwrite the files instead?

Thanks

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.