Skip to content
This repository was archived by the owner on Dec 4, 2022. It is now read-only.

Commit d4807c1

Browse files
authored
support require statements with apostrophes (#103)
* fix teambit/bit#1708, support require statements with apostrophes * bump version
1 parent 2d7c990 commit d4807c1

File tree

8 files changed

+86
-46
lines changed

8 files changed

+86
-46
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
77

88
## [unreleased]
99

10+
## [2.0.11] - 2019-06-05
11+
12+
- [#1708](https://github.com/teambit/bit/issues/1708) support `require` with apostrophes
13+
1014
## [2.0.10] - 2019-05-31
1115

1216
- [#1690](https://github.com/teambit/bit/issues/1690) fix error "Cannot read property 'find' of undefined" with typescript files

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "bit-javascript",
3-
"version": "2.0.10",
3+
"version": "2.0.11",
44
"scripts": {
55
"flow": "flow; test $? -eq 0 -o $? -eq 2",
66
"lint": "eslint src && flow check || true",

src/cli/commands/get-dependencies.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@ import { getDependenciesAction } from '../../actions';
44
import { DEFAULT_BINDINGS_PREFIX } from '../../constants';
55

66
const report = (data) => {
7-
console.log('dependencies', JSON.stringify(data, null, ' '));
7+
if (!data) return 'No dependencies found!';
8+
return JSON.stringify(data, null, ' ');
89
};
910

1011
const resolveConfig = undefined; // @todo: figure out how to get the data from the command line, maybe as a file

src/dependency-builder/detectives/detective-es6/index.js

Lines changed: 7 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { getDependenciesFromMemberExpression, getDependenciesFromCallExpression } from '../parser-helper';
12
/**
23
* this file had been forked (and changed since then) from https://github.com/dependents/node-detective-es6
34
*/
@@ -83,32 +84,15 @@ module.exports = function (src) {
8384
addExportedToImportSpecifier(node.declaration.name);
8485
break;
8586
case 'CallExpression':
86-
if (node.callee.type === 'Import' && node.arguments.length && node.arguments[0].value) {
87-
addDependency(node.arguments[0].value);
88-
}
89-
if (
90-
node.callee.type === 'Identifier' && // taken from detective-cjs
91-
node.callee.name === 'require' &&
92-
node.arguments[0].value &&
93-
node.arguments &&
94-
node.arguments.length &&
95-
(node.arguments[0].type === 'Literal' || node.arguments[0].type === 'StringLiteral')
96-
) {
97-
addDependency(node.arguments[0].value);
87+
{
88+
const value = getDependenciesFromCallExpression(node);
89+
if (value) addDependency(value);
9890
}
9991
break;
10092
case 'MemberExpression':
101-
if (
102-
node.object.type === 'CallExpression' &&
103-
node.object.callee.type === 'Identifier' &&
104-
node.object.callee.name === 'require' &&
105-
node.object.arguments &&
106-
node.object.arguments.length &&
107-
node.object.arguments[0].value &&
108-
(node.object.arguments[0].type === 'Literal' || node.object.arguments[0].type === 'StringLiteral')
109-
) {
110-
const depValue = node.object.arguments[0].value;
111-
addDependency(depValue);
93+
{
94+
const value = getDependenciesFromMemberExpression(node);
95+
if (value) addDependency(value);
11296
}
11397
break;
11498
default:

src/dependency-builder/detectives/detective-es6/index.spec.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,18 @@ describe('detective-es6', () => {
116116
});
117117
});
118118

119+
describe('string in apostrophes', () => {
120+
it('should recognize when using require statement', () => {
121+
const deps = detective('const foo = require(`foo`);'); // eslint-disable-line
122+
const depsKeys = Object.keys(deps);
123+
assert.equal(depsKeys.length, 1);
124+
assert.equal(depsKeys[0], 'foo');
125+
});
126+
it('should throw when using import syntax', () => {
127+
expect(() => detective('import foo from `foo`;')).to.throw(); // eslint-disable-line
128+
});
129+
});
130+
119131
describe('import-specifiers detection (for tree shaking)', () => {
120132
it('should recognize default imports as default', () => {
121133
const deps = detective('import foo from "foo";');

src/dependency-builder/detectives/detective-typescript/index.js

Lines changed: 7 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
/**
22
* this file had been forked from https://github.com/pahen/detective-typescript
33
*/
4+
import { getDependenciesFromMemberExpression, getDependenciesFromCallExpression } from '../parser-helper';
45

56
const Parser = require('@typescript-eslint/typescript-estree');
67
const Walker = require('node-source-walk');
@@ -81,30 +82,15 @@ module.exports = function (src, options = {}) {
8182
}
8283
break;
8384
case 'CallExpression':
84-
if (node.callee.type === 'Import' && node.arguments.length) {
85-
addDependency(node.arguments[0].value);
86-
}
87-
if (
88-
node.callee.type === 'Identifier' && // taken from detective-cjs
89-
node.callee.name === 'require' &&
90-
node.arguments &&
91-
node.arguments.length &&
92-
(node.arguments[0].type === 'Literal' || node.arguments[0].type === 'StringLiteral')
93-
) {
94-
addDependency(node.arguments[0].value);
85+
{
86+
const value = getDependenciesFromCallExpression(node);
87+
if (value) addDependency(value);
9588
}
9689
break;
9790
case 'MemberExpression':
98-
if (
99-
node.object.type === 'CallExpression' &&
100-
node.object.callee.type === 'Identifier' &&
101-
node.object.callee.name === 'require' &&
102-
node.object.arguments &&
103-
node.object.arguments.length &&
104-
(node.object.arguments[0].type === 'Literal' || node.object.arguments[0].type === 'StringLiteral')
105-
) {
106-
const depValue = node.object.arguments[0].value;
107-
addDependency(depValue);
91+
{
92+
const value = getDependenciesFromMemberExpression(node);
93+
if (value) addDependency(value);
10894
}
10995
break;
11096
default:

src/dependency-builder/detectives/detective-typescript/index.spec.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,4 +119,13 @@ describe('detective-typescript', () => {
119119
detective("import './layout.scss'; export default something;");
120120
});
121121
});
122+
123+
describe('string in apostrophes', () => {
124+
it('should recognize when using require statement', () => {
125+
const deps = detective('const foo = require(`foo`);'); // eslint-disable-line
126+
const depsKeys = Object.keys(deps);
127+
assert.equal(depsKeys.length, 1);
128+
assert.equal(depsKeys[0], 'foo');
129+
});
130+
});
122131
});
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
export function getDependenciesFromMemberExpression(node) {
2+
if (
3+
node.object.type === 'CallExpression' &&
4+
node.object.callee.type === 'Identifier' &&
5+
node.object.callee.name === 'require' &&
6+
node.object.arguments &&
7+
node.object.arguments.length
8+
) {
9+
return getStringValue(node.object.arguments[0]);
10+
}
11+
return null;
12+
}
13+
14+
export function getDependenciesFromCallExpression(node) {
15+
if (node.callee.type === 'Import' && node.arguments.length && node.arguments[0].value) {
16+
return node.arguments[0].value;
17+
}
18+
if (
19+
node.callee.type === 'Identifier' && // taken from detective-cjs
20+
node.callee.name === 'require' &&
21+
node.arguments &&
22+
node.arguments.length
23+
) {
24+
return getStringValue(node.arguments[0]);
25+
}
26+
return null;
27+
}
28+
29+
function getStringValue(node) {
30+
// using single or double quotes (', ")
31+
if (node.type === 'Literal' || node.type === 'StringLiteral') {
32+
return node.value;
33+
}
34+
// using apostrophe (`)
35+
if (
36+
node.type === 'TemplateLiteral' &&
37+
node.quasis &&
38+
node.quasis.length &&
39+
node.quasis[0].type === 'TemplateElement'
40+
) {
41+
return node.quasis[0].value.raw;
42+
}
43+
return null;
44+
}

0 commit comments

Comments
 (0)