react / wstein / node_modules / jest-cli / node_modules / node-haste / lib / loader / JSBenchLoader.js
80668 views/**1* Copyright 2013 Facebook, Inc.2*3* Licensed under the Apache License, Version 2.0 (the "License");4* you may not use this file except in compliance with the License.5* You may obtain a copy of the License at6*7* http://www.apache.org/licenses/LICENSE-2.08*9* Unless required by applicable law or agreed to in writing, software10* distributed under the License is distributed on an "AS IS" BASIS,11* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.12* See the License for the specific language governing permissions and13* limitations under the License.14*/15var inherits = require('util').inherits;1617var docblock = require('../parse/docblock');18var extract = require('../parse/extract');19var ResourceLoader = require('./ResourceLoader');20var JSBench = require('../resource/JSBench');212223/**24* @class Loads and parses __benchmarks__ / *.js files25*26* @extends {ResourceLoader}27*/28function JSBenchLoader(options) {29ResourceLoader.call(this, options);3031this.pathRe = this.options.matchSubDirs ?32/(?:[\\/]|^)__benchmarks__[\\/](.+)\.js$/ :33/(?:[\\/]|^)__benchmarks__[\\/]([^\/]+)\.js$/;34}35inherits(JSBenchLoader, ResourceLoader);36JSBenchLoader.prototype.path = __filename;3738JSBenchLoader.prototype.getResourceTypes = function() {39return [JSBench];40};4142JSBenchLoader.prototype.getExtensions = function() {43return ['.js'];44};454647/**48* Initialize a resource with the source code and configuration49* Loader can parse, gzip, minify the source code to build the resulting50* Resource value object51*52* @protected53* @param {String} path resource being built54* @param {ProjectConfiguration} configuration configuration for the path55* @param {String} sourceCode56* @param {Function} callback57*/58JSBenchLoader.prototype.loadFromSource =59function(path, configuration, sourceCode, messages, callback) {6061var bench = new JSBench(path);6263docblock.parse(docblock.extract(sourceCode)).forEach(function(pair) {64var name = pair[0];65var value = pair[1];6667switch (name) {68case 'emails':69bench.contacts = value.split(/\s/);70break;71default:72// do nothing73}74});7576bench.id = configuration && configuration.resolveID(path);77if (!bench.id) {78bench.id = path.match(this.pathRe)[1];79}80bench.requiredModules = extract.requireCalls(sourceCode);81callback(messages, bench);82};8384/**85* Only match __benchmarks__ / *.js files86* @static87* @param {String} filePath88* @return {Boolean}89*/90JSBenchLoader.prototype.matchPath = function(filePath) {91return this.pathRe.test(filePath);92};939495module.exports = JSBenchLoader;969798