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

Commit

Permalink
support require statements with apostrophes (#103)
Browse files Browse the repository at this point in the history
* fix teambit/bit#1708, support require statements with apostrophes

* bump version
  • Loading branch information
davidfirst authored Jun 6, 2019
1 parent 2d7c990 commit d4807c1
Show file tree
Hide file tree
Showing 8 changed files with 86 additions and 46 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ and this project adheres to [Semantic Versioning](http://semver.org/).

## [unreleased]

## [2.0.11] - 2019-06-05

- [#1708](https://github.com/teambit/bit/issues/1708) support `require` with apostrophes

## [2.0.10] - 2019-05-31

- [#1690](https://github.com/teambit/bit/issues/1690) fix error "Cannot read property 'find' of undefined" with typescript files
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "bit-javascript",
"version": "2.0.10",
"version": "2.0.11",
"scripts": {
"flow": "flow; test $? -eq 0 -o $? -eq 2",
"lint": "eslint src && flow check || true",
Expand Down
3 changes: 2 additions & 1 deletion src/cli/commands/get-dependencies.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ import { getDependenciesAction } from '../../actions';
import { DEFAULT_BINDINGS_PREFIX } from '../../constants';

const report = (data) => {
console.log('dependencies', JSON.stringify(data, null, ' '));
if (!data) return 'No dependencies found!';
return JSON.stringify(data, null, ' ');
};

const resolveConfig = undefined; // @todo: figure out how to get the data from the command line, maybe as a file
Expand Down
30 changes: 7 additions & 23 deletions src/dependency-builder/detectives/detective-es6/index.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { getDependenciesFromMemberExpression, getDependenciesFromCallExpression } from '../parser-helper';
/**
* this file had been forked (and changed since then) from https://github.com/dependents/node-detective-es6
*/
Expand Down Expand Up @@ -83,32 +84,15 @@ module.exports = function (src) {
addExportedToImportSpecifier(node.declaration.name);
break;
case 'CallExpression':
if (node.callee.type === 'Import' && node.arguments.length && node.arguments[0].value) {
addDependency(node.arguments[0].value);
}
if (
node.callee.type === 'Identifier' && // taken from detective-cjs
node.callee.name === 'require' &&
node.arguments[0].value &&
node.arguments &&
node.arguments.length &&
(node.arguments[0].type === 'Literal' || node.arguments[0].type === 'StringLiteral')
) {
addDependency(node.arguments[0].value);
{
const value = getDependenciesFromCallExpression(node);
if (value) addDependency(value);
}
break;
case 'MemberExpression':
if (
node.object.type === 'CallExpression' &&
node.object.callee.type === 'Identifier' &&
node.object.callee.name === 'require' &&
node.object.arguments &&
node.object.arguments.length &&
node.object.arguments[0].value &&
(node.object.arguments[0].type === 'Literal' || node.object.arguments[0].type === 'StringLiteral')
) {
const depValue = node.object.arguments[0].value;
addDependency(depValue);
{
const value = getDependenciesFromMemberExpression(node);
if (value) addDependency(value);
}
break;
default:
Expand Down
12 changes: 12 additions & 0 deletions src/dependency-builder/detectives/detective-es6/index.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,18 @@ describe('detective-es6', () => {
});
});

describe('string in apostrophes', () => {
it('should recognize when using require statement', () => {
const deps = detective('const foo = require(`foo`);'); // eslint-disable-line
const depsKeys = Object.keys(deps);
assert.equal(depsKeys.length, 1);
assert.equal(depsKeys[0], 'foo');
});
it('should throw when using import syntax', () => {
expect(() => detective('import foo from `foo`;')).to.throw(); // eslint-disable-line
});
});

describe('import-specifiers detection (for tree shaking)', () => {
it('should recognize default imports as default', () => {
const deps = detective('import foo from "foo";');
Expand Down
28 changes: 7 additions & 21 deletions src/dependency-builder/detectives/detective-typescript/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
/**
* this file had been forked from https://github.com/pahen/detective-typescript
*/
import { getDependenciesFromMemberExpression, getDependenciesFromCallExpression } from '../parser-helper';

const Parser = require('@typescript-eslint/typescript-estree');
const Walker = require('node-source-walk');
Expand Down Expand Up @@ -81,30 +82,15 @@ module.exports = function (src, options = {}) {
}
break;
case 'CallExpression':
if (node.callee.type === 'Import' && node.arguments.length) {
addDependency(node.arguments[0].value);
}
if (
node.callee.type === 'Identifier' && // taken from detective-cjs
node.callee.name === 'require' &&
node.arguments &&
node.arguments.length &&
(node.arguments[0].type === 'Literal' || node.arguments[0].type === 'StringLiteral')
) {
addDependency(node.arguments[0].value);
{
const value = getDependenciesFromCallExpression(node);
if (value) addDependency(value);
}
break;
case 'MemberExpression':
if (
node.object.type === 'CallExpression' &&
node.object.callee.type === 'Identifier' &&
node.object.callee.name === 'require' &&
node.object.arguments &&
node.object.arguments.length &&
(node.object.arguments[0].type === 'Literal' || node.object.arguments[0].type === 'StringLiteral')
) {
const depValue = node.object.arguments[0].value;
addDependency(depValue);
{
const value = getDependenciesFromMemberExpression(node);
if (value) addDependency(value);
}
break;
default:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -119,4 +119,13 @@ describe('detective-typescript', () => {
detective("import './layout.scss'; export default something;");
});
});

describe('string in apostrophes', () => {
it('should recognize when using require statement', () => {
const deps = detective('const foo = require(`foo`);'); // eslint-disable-line
const depsKeys = Object.keys(deps);
assert.equal(depsKeys.length, 1);
assert.equal(depsKeys[0], 'foo');
});
});
});
44 changes: 44 additions & 0 deletions src/dependency-builder/detectives/parser-helper.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
export function getDependenciesFromMemberExpression(node) {
if (
node.object.type === 'CallExpression' &&
node.object.callee.type === 'Identifier' &&
node.object.callee.name === 'require' &&
node.object.arguments &&
node.object.arguments.length
) {
return getStringValue(node.object.arguments[0]);
}
return null;
}

export function getDependenciesFromCallExpression(node) {
if (node.callee.type === 'Import' && node.arguments.length && node.arguments[0].value) {
return node.arguments[0].value;
}
if (
node.callee.type === 'Identifier' && // taken from detective-cjs
node.callee.name === 'require' &&
node.arguments &&
node.arguments.length
) {
return getStringValue(node.arguments[0]);
}
return null;
}

function getStringValue(node) {
// using single or double quotes (', ")
if (node.type === 'Literal' || node.type === 'StringLiteral') {
return node.value;
}
// using apostrophe (`)
if (
node.type === 'TemplateLiteral' &&
node.quasis &&
node.quasis.length &&
node.quasis[0].type === 'TemplateElement'
) {
return node.quasis[0].value.raw;
}
return null;
}

0 comments on commit d4807c1

Please sign in to comment.