Skip to content

Commit

Permalink
This backports bfd1531 from react 15.2 (facebook#6882); it took some …
Browse files Browse the repository at this point in the history
…help from 1d0c1b1 to get the basic gulp setup (0.14 beta v2 code), and took a lot of dependency wrangling.

`grunt extract-errors` should run the script now.  I'm starting with a fresh codes.json because these will never match up with the official ones unfortunately.

Things skipped:
- travis integration.  I don't know anything about travis so don't know if / how to hook this up for it.
- inserting this as part of a full build process.  It looks like it's a oneoff in master too fwiw
  • Loading branch information
David Goldstein committed Jul 2, 2016
1 parent 65c4715 commit a298281
Show file tree
Hide file tree
Showing 10 changed files with 510 additions and 1 deletion.
21 changes: 21 additions & 0 deletions Gruntfile.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
'use strict';

var assign = require('object-assign');
var path = require('path');
var exec = require('child_process').exec;
var jsxTask = require('./grunt/tasks/jsx');
var browserifyTask = require('./grunt/tasks/browserify');
Expand Down Expand Up @@ -33,6 +35,21 @@ module.exports = function(grunt) {

grunt.config.set('compress', require('./grunt/config/compress'));

function spawnGulp(args, opts, done) {
grunt.util.spawn({
// This could be more flexible (require.resolve & lookup bin in package)
// but if it breaks we'll fix it then.
cmd: path.join('node_modules', '.bin', 'gulp'),
args: args,
opts: assign({stdio: 'inherit'}, opts),
}, function(err, result, code) {
if (err) {
grunt.fail.fatal('Something went wrong running gulp: ', result);
}
done(code === 0);
});
}

Object.keys(grunt.file.readJSON('package.json').devDependencies)
.filter(function(npmTaskName) { return npmTaskName.indexOf('grunt-') === 0; })
.filter(function(npmTaskName) { return npmTaskName != 'grunt-cli'; })
Expand Down Expand Up @@ -253,6 +270,10 @@ module.exports = function(grunt) {
'release:msg'
]);

grunt.registerTask('extract-errors', function() {
spawnGulp(['react:extract-errors'], null, this.async());
});

// The default task - build - to keep setup easy
grunt.registerTask('default', ['build']);
};
40 changes: 40 additions & 0 deletions gulpfile.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/**
* Copyright 2013-2015, Facebook, Inc.
* All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree. An additional grant
* of patent rights can be found in the PATENTS file in the same directory.
*/

'use strict';

var gulp = require('gulp');
var babel = require('gulp-babel');
var flatten = require('gulp-flatten');
var del = require('del');

var extractErrors = require('./scripts/error-codes/gulp-extract-errors');

var paths = {
react: {
src: [
'src/**/*.js',
'!src/**/__tests__/**/*.js',
'!test/all.js',
],
lib: 'build/modules',
},
};

var errorCodeOpts = {
errorMapFilePath: 'scripts/error-codes/codes.json',
};

gulp.task('react:extract-errors', function() {
return gulp
.src(paths.react.src)
.pipe(extractErrors(errorCodeOpts));
});

gulp.task('default', ['react:extract-errors']);
11 changes: 10 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,18 @@
"url": "https://github.com/facebook/react"
},
"dependencies": {
"babel-traverse": "^6.10.4",
"babylon": "^6.8.2",
"commoner": "^0.10.0",
"del": "^2.2.1",
"esprima-fb": "^6001.1.0-dev-harmony-fb",
"jstransform": "^6.3.2"
"gulp": "^3.9.0",
"gulp-babel": "^6.0.0",
"gulp-flatten": "^0.2.0",
"gulp-util": "^3.0.7",
"jstransform": "^6.3.2",
"object-assign": "^4.1.0",
"through2": "^2.0.1"
},
"devDependencies": {
"benchmark": "~1.0.0",
Expand Down
13 changes: 13 additions & 0 deletions scripts/error-codes/Types.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
/**
* Copyright (c) 2013-present, Facebook, Inc.
* All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree. An additional grant
* of patent rights can be found in the PATENTS file in the same directory.
*
* @flow
*/
'use strict';

/*:: export type ErrorMap = { [id: string]: string; }; */
36 changes: 36 additions & 0 deletions scripts/error-codes/__tests__/evalToString-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/**
* Copyright (c) 2013-present, Facebook, Inc.
* All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree. An additional grant
* of patent rights can be found in the PATENTS file in the same directory.
*/
'use strict';

var evalToString = require('../evalToString');
var babylon = require('babylon');

var parse = (source) => babylon.parse(
`(${source});`
).program.body[0].expression; // quick way to get an exp node

var parseAndEval = (source) => evalToString(parse(source));

describe('evalToString', () => {
it('should support StringLiteral', () => {
expect(parseAndEval(`'foobar'`)).toBe('foobar');
expect(parseAndEval(`'yowassup'`)).toBe('yowassup');
});

it('should support string concat (`+`)', () => {
expect(parseAndEval(`'foo ' + 'bar'`)).toBe('foo bar');
});

it('should throw when it finds other types', () => {
expect(() => parseAndEval(`'foo ' + true`)).toThrowError(/Unsupported type/);
expect(() => parseAndEval(`'foo ' + 3`)).toThrowError(/Unsupported type/);
expect(() => parseAndEval(`'foo ' + null`)).toThrowError(/Unsupported type/);
expect(() => parseAndEval(`'foo ' + undefined`)).toThrowError(/Unsupported type/);
});
});
54 changes: 54 additions & 0 deletions scripts/error-codes/__tests__/invertObject-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
/**
* Copyright (c) 2013-present, Facebook, Inc.
* All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree. An additional grant
* of patent rights can be found in the PATENTS file in the same directory.
*/
'use strict';

var invertObject = require('../invertObject');

var objectValues = (target) => Object.keys(target).map((key) => target[key]);

describe('invertObject', () => {
it('should return an empty object for an empty input', () => {
expect(invertObject({})).toEqual({});
});

it('should invert key-values', () => {
expect(invertObject({
a: '3',
b: '4',
})).toEqual({
3: 'a',
4: 'b',
});
});

it('should take the last value when there\'re duplications in vals', () => {
expect(invertObject({
a: '3',
b: '4',
c: '3',
})).toEqual({
4: 'b',
3: 'c',
});
});

it('should perserve the original order', () => {
expect(Object.keys(invertObject({
a: '3',
b: '4',
c: '3',
}))).toEqual(['3', '4']);

expect(objectValues(invertObject({
a: '3',
b: '4',
c: '3',
}))).toEqual(['c', 'b']);
});
});
Loading

0 comments on commit a298281

Please sign in to comment.