forked from facebookarchive/draft-js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhasteImpl.js
71 lines (58 loc) · 1.63 KB
/
hasteImpl.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
/**
* Copyright (c) Facebook, Inc. and its affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @flow
*/
'use strict';
const path = require('path');
const ROOT = path.join(__dirname, '..', '..');
const BLACKLISTED_PATTERNS/*: Array<RegExp>*/ = [
/.*\/__(mocks|tests)__\/.*/,
];
const WHITELISTED_PREFIXES/*: Array<string>*/ = [
'src'
];
const NAME_REDUCERS/*: Array<[RegExp, string]>*/ = [
// extract basename
[/^(?:.*\/)?([a-zA-Z0-9$_.-]+)$/, '$1'],
// strip .js/.js.flow suffix
[/^(.*)\.js(\.flow)?$/, '$1'],
// strip .android/.ios/.native/.web suffix
[/^(.*)\.(android|ios|native|web)$/, '$1'],
];
const haste = {
/*
* @return {string|void} hasteName for module at filePath; or undefined if
* filePath is not a haste module
*/
getHasteName(
filePath/*: string*/,
sourceCode/* : ?string*/
)/*: (string | void)*/ {
if (!isHastePath(filePath)) {
return undefined;
}
const hasteName = NAME_REDUCERS.reduce(
(name, [pattern, replacement]) => name.replace(pattern, replacement),
filePath
);
return hasteName;
},
};
function isHastePath(filePath/*: string*/)/*: bool*/ {
if (!filePath.endsWith('.js') && !filePath.endsWith('.js.flow')) {
return false;
}
if (!filePath.startsWith(ROOT)) {
return false;
}
filePath = filePath.substr(ROOT.length + 1);
if (BLACKLISTED_PATTERNS.some(pattern => pattern.test(filePath))) {
return false;
}
return WHITELISTED_PREFIXES.some(prefix => filePath.startsWith(prefix));
}
module.exports = haste;