Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Download
80668 views
1
/**
2
* Copyright 2013 Facebook, Inc.
3
*
4
* Licensed under the Apache License, Version 2.0 (the "License");
5
* you may not use this file except in compliance with the License.
6
* You may obtain a copy of the License at
7
*
8
* http://www.apache.org/licenses/LICENSE-2.0
9
*
10
* Unless required by applicable law or agreed to in writing, software
11
* distributed under the License is distributed on an "AS IS" BASIS,
12
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
* See the License for the specific language governing permissions and
14
* limitations under the License.
15
*/
16
var inherits = require('util').inherits;
17
18
var extract = require('../parse/extract');
19
var ResourceLoader = require('./ResourceLoader');
20
var JSMock = require('../resource/JSMock');
21
22
/**
23
* @class Loads and parses __mocks__ / *.js files
24
*
25
* @extends {ResourceLoader}
26
*/
27
function JSMockLoader(options) {
28
ResourceLoader.call(this, options);
29
30
this.pathRe = this.options.matchSubDirs ?
31
/(?:[\\/]|^)__mocks__[\\/](.+)\.js$/ :
32
/(?:[\\/]|^)__mocks__[\\/]([^\/]+)\.js$/;
33
}
34
inherits(JSMockLoader, ResourceLoader);
35
JSMockLoader.prototype.path = __filename;
36
37
JSMockLoader.prototype.getResourceTypes = function() {
38
return [JSMock];
39
};
40
41
JSMockLoader.prototype.getExtensions = function() {
42
return ['.js'];
43
};
44
45
46
/**
47
* Initialize a resource with the source code and configuration
48
* Loader can parse, gzip, minify the source code to build the resulting
49
* Resource value object.
50
*
51
* @protected
52
* @param {String} path resource being built
53
* @param {ProjectConfiguration} configuration configuration for the path
54
* @param {String} sourceCode
55
* @param {Function} callback
56
*/
57
JSMockLoader.prototype.loadFromSource =
58
function(path, configuration, sourceCode, messages, callback) {
59
var mock = new JSMock(path);
60
mock.id = path.match(this.pathRe)[1];
61
mock.requiredModules = extract.requireCalls(sourceCode);
62
callback(messages, mock);
63
};
64
65
/**
66
* Only match __mocks__ / *.js files
67
* @static
68
* @param {String} filePath
69
* @return {Boolean}
70
*/
71
JSMockLoader.prototype.matchPath = function(filePath) {
72
return this.pathRe.test(filePath);
73
};
74
75
76
module.exports = JSMockLoader;
77
78