Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

babel-plugin-normalize-requires

Package Overview
Dependencies
Maintainers
1
Versions
120
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

babel-plugin-normalize-requires - npm Package Compare versions

Comparing version 1.2.2 to 1.2.3

128

lib/__tests__/index.test.js

@@ -16,94 +16,94 @@ 'use strict';

describe('when requiring package default modules', function () {
it('keeps trailing ".js" from package names', function () {
var source = '\n\t\trequire(\'a-package.js\')\n\t\t';
it('keeps trailing ".js" from package names', function () {
var source = '\n\t\trequire(\'a-package.js\')\n\t\t';
var _babel$transform = babel.transform(source, {
plugins: [_index2.default]
}),
code = _babel$transform.code;
var _babel$transform = babel.transform(source, {
plugins: [_index2.default]
}),
code = _babel$transform.code;
expect(code).toMatchSnapshot();
});
expect(code).toMatchSnapshot();
});
it('keeps trailing ".js" from scoped package names', function () {
var source = '\n\t\trequire(\'@some-scope/a-package.js\')\n\t\t';
it('keeps trailing ".js" from scoped package names', function () {
var source = '\n\t\trequire(\'@some-scope/a-package.js\')\n\t\t';
var _babel$transform2 = babel.transform(source, {
plugins: [_index2.default]
}),
code = _babel$transform2.code;
var _babel$transform2 = babel.transform(source, {
plugins: [_index2.default]
}),
code = _babel$transform2.code;
expect(code).toMatchSnapshot();
});
expect(code).toMatchSnapshot();
});
});
describe('when requiring local modules', function () {
it('removes trailing ".js" from module names', function () {
var source = '\n\t require(\'./a-module.js\')\n\t ';
it('removes trailing ".js" from module names', function () {
var source = '\n\t require(\'./a-module.js\')\n\t ';
var _babel$transform3 = babel.transform(source, {
plugins: [_index2.default]
}),
code = _babel$transform3.code;
var _babel$transform3 = babel.transform(source, {
plugins: [_index2.default]
}),
code = _babel$transform3.code;
expect(code).toMatchSnapshot();
});
expect(code).toMatchSnapshot();
});
it('removes trailing "/" from module names', function () {
var source = '\n\t\trequire(\'./a-module/\')\n\t ';
it('removes trailing "/" from module names', function () {
var source = '\n\t\trequire(\'./a-module/\')\n\t ';
var _babel$transform4 = babel.transform(source, {
plugins: [_index2.default]
}),
code = _babel$transform4.code;
var _babel$transform4 = babel.transform(source, {
plugins: [_index2.default]
}),
code = _babel$transform4.code;
expect(code).toMatchSnapshot();
});
expect(code).toMatchSnapshot();
});
it('only removes trailing "/" from module names ending in ".js"', function () {
var source = '\n\t\trequire(\'./a-module.js/\')\n\t ';
it('only removes trailing "/" from module names ending in ".js"', function () {
var source = '\n\t\trequire(\'./a-module.js/\')\n\t ';
var _babel$transform5 = babel.transform(source, {
plugins: [_index2.default]
}),
code = _babel$transform5.code;
var _babel$transform5 = babel.transform(source, {
plugins: [_index2.default]
}),
code = _babel$transform5.code;
expect(code).toMatchSnapshot();
});
expect(code).toMatchSnapshot();
});
});
describe('when requiring external modules', function () {
it('removes trailing ".js" from module names', function () {
var source = '\n\t\trequire(\'a-package/a-module.js\')\n\t ';
it('removes trailing ".js" from module names', function () {
var source = '\n\t\trequire(\'a-package/a-module.js\')\n\t ';
var _babel$transform6 = babel.transform(source, {
plugins: [_index2.default]
}),
code = _babel$transform6.code;
var _babel$transform6 = babel.transform(source, {
plugins: [_index2.default]
}),
code = _babel$transform6.code;
expect(code).toMatchSnapshot();
});
expect(code).toMatchSnapshot();
});
it('removes trailing "/" from module names', function () {
var source = '\n\t\trequire(\'a-package/a-module/\')\n\t ';
it('removes trailing "/" from module names', function () {
var source = '\n\t\trequire(\'a-package/a-module/\')\n\t ';
var _babel$transform7 = babel.transform(source, {
plugins: [_index2.default]
}),
code = _babel$transform7.code;
var _babel$transform7 = babel.transform(source, {
plugins: [_index2.default]
}),
code = _babel$transform7.code;
expect(code).toMatchSnapshot();
});
expect(code).toMatchSnapshot();
});
it('only removes trailing "/" from module names ending in ".js"', function () {
var source = '\n\t\trequire(\'a-package/a-module.js/\')\n\t\t';
it('only removes trailing "/" from module names ending in ".js"', function () {
var source = '\n\t\trequire(\'a-package/a-module.js/\')\n\t\t';
var _babel$transform8 = babel.transform(source, {
plugins: [_index2.default]
}),
code = _babel$transform8.code;
var _babel$transform8 = babel.transform(source, {
plugins: [_index2.default]
}),
code = _babel$transform8.code;
expect(code).toMatchSnapshot();
});
expect(code).toMatchSnapshot();
});
});
//# sourceMappingURL=index.test.js.map
'use strict';
Object.defineProperty(exports, "__esModule", {
value: true
value: true
});
exports.default = function (_ref) {
var t = _ref.types;
var t = _ref.types;
return {
visitor: {
Identifier: {
exit: function exit(path, state) {
var node = path.node;
return {
visitor: {
Identifier: {
exit: function exit(path) {
var node = path.node;
if (node.name == 'require') {
var parent = path.parent;
if (node.name == 'require') {
var parent = path.parent;
if (t.isCallExpression(parent)) {
var argument = parent.arguments[0];
if (t.isCallExpression(parent)) {
var argument = parent.arguments[0];
if (t.isLiteral(argument) && argument.value) {
var moduleName = argument.value;
if (t.isLiteral(argument) && argument.value) {
var moduleName = argument.value;
if (typeof moduleName === 'string' && !isPackageName(moduleName)) {
if (moduleName.endsWith('.js')) {
moduleName = moduleName.substring(0, moduleName.length - 3);
}
if (typeof moduleName === 'string' && !isPackageName(moduleName)) {
if (moduleName.endsWith('.js')) {
moduleName = moduleName.substring(0, moduleName.length - 3);
}
if (moduleName.endsWith('/')) {
moduleName = moduleName.substring(0, moduleName.length - 1);
}
}
if (moduleName.endsWith('/')) {
moduleName = moduleName.substring(0, moduleName.length - 1);
}
}
argument.value = moduleName;
}
}
}
}
}
}
};
argument.value = moduleName;
}
}
}
}
}
}
};
};

@@ -51,15 +51,15 @@

function isPackageName(moduleName) {
var firstSlashIndex = moduleName.indexOf('/');
var firstSlashIndex = moduleName.indexOf('/');
if (firstSlashIndex == -1) {
return true;
}
if (firstSlashIndex == -1) {
return true;
}
var restOfModuleName = moduleName.substring(firstSlashIndex + 1);
var restOfModuleName = moduleName.substring(firstSlashIndex + 1);
if (moduleName.startsWith('@') && restOfModuleName.indexOf('/') == -1) {
return true;
}
if (moduleName.startsWith('@') && restOfModuleName.indexOf('/') == -1) {
return true;
}
return false;
return false;
} /**

@@ -66,0 +66,0 @@ * @return {object} a babel visitor

{
"name": "babel-plugin-normalize-requires",
"version": "1.2.2",
"version": "1.2.3",
"description": "A Babel plugin that rewrites require() calls to normalize them (removing extensions and trailing slashes, for example).",

@@ -5,0 +5,0 @@ "main": "lib/index.js",

@@ -5,101 +5,101 @@ import * as babel from 'babel-core';

describe('when requiring package default modules', () => {
it('keeps trailing ".js" from package names', () => {
const source = `
it('keeps trailing ".js" from package names', () => {
const source = `
require('a-package.js')
`;
const {code} = babel.transform(source, {
plugins: [plugin],
});
const {code} = babel.transform(source, {
plugins: [plugin],
});
expect(code).toMatchSnapshot();
});
expect(code).toMatchSnapshot();
});
it('keeps trailing ".js" from scoped package names', () => {
const source = `
it('keeps trailing ".js" from scoped package names', () => {
const source = `
require('@some-scope/a-package.js')
`;
const {code} = babel.transform(source, {
plugins: [plugin],
});
const {code} = babel.transform(source, {
plugins: [plugin],
});
expect(code).toMatchSnapshot();
});
expect(code).toMatchSnapshot();
});
});
describe('when requiring local modules', () => {
it('removes trailing ".js" from module names', () => {
const source = `
it('removes trailing ".js" from module names', () => {
const source = `
require('./a-module.js')
`;
const {code} = babel.transform(source, {
plugins: [plugin],
});
const {code} = babel.transform(source, {
plugins: [plugin],
});
expect(code).toMatchSnapshot();
});
expect(code).toMatchSnapshot();
});
it('removes trailing "/" from module names', () => {
const source = `
it('removes trailing "/" from module names', () => {
const source = `
require('./a-module/')
`;
const {code} = babel.transform(source, {
plugins: [plugin],
});
const {code} = babel.transform(source, {
plugins: [plugin],
});
expect(code).toMatchSnapshot();
});
expect(code).toMatchSnapshot();
});
it('only removes trailing "/" from module names ending in ".js"', () => {
const source = `
it('only removes trailing "/" from module names ending in ".js"', () => {
const source = `
require('./a-module.js/')
`;
const {code} = babel.transform(source, {
plugins: [plugin],
});
const {code} = babel.transform(source, {
plugins: [plugin],
});
expect(code).toMatchSnapshot();
});
expect(code).toMatchSnapshot();
});
});
describe('when requiring external modules', () => {
it('removes trailing ".js" from module names', () => {
const source = `
it('removes trailing ".js" from module names', () => {
const source = `
require('a-package/a-module.js')
`;
const {code} = babel.transform(source, {
plugins: [plugin],
});
const {code} = babel.transform(source, {
plugins: [plugin],
});
expect(code).toMatchSnapshot();
});
expect(code).toMatchSnapshot();
});
it('removes trailing "/" from module names', () => {
const source = `
it('removes trailing "/" from module names', () => {
const source = `
require('a-package/a-module/')
`;
const {code} = babel.transform(source, {
plugins: [plugin],
});
const {code} = babel.transform(source, {
plugins: [plugin],
});
expect(code).toMatchSnapshot();
});
expect(code).toMatchSnapshot();
});
it('only removes trailing "/" from module names ending in ".js"', () => {
const source = `
it('only removes trailing "/" from module names ending in ".js"', () => {
const source = `
require('a-package/a-module.js/')
`;
const {code} = babel.transform(source, {
plugins: [plugin],
});
const {code} = babel.transform(source, {
plugins: [plugin],
});
expect(code).toMatchSnapshot();
});
expect(code).toMatchSnapshot();
});
});

@@ -5,38 +5,44 @@ /**

export default function({types: t}) {
return {
visitor: {
Identifier: {
exit(path, state) {
const node = path.node;
return {
visitor: {
Identifier: {
exit(path) {
const node = path.node;
if (node.name == 'require') {
const parent = path.parent;
if (node.name == 'require') {
const parent = path.parent;
if (t.isCallExpression(parent)) {
const argument = parent.arguments[0];
if (t.isCallExpression(parent)) {
const argument = parent.arguments[0];
if (t.isLiteral(argument) && argument.value) {
let moduleName = argument.value;
if (t.isLiteral(argument) && argument.value) {
let moduleName = argument.value;
if (
typeof moduleName === 'string' &&
!isPackageName(moduleName)
) {
if (moduleName.endsWith('.js')) {
moduleName = moduleName.substring(0, moduleName.length - 3);
}
if (
typeof moduleName === 'string' &&
!isPackageName(moduleName)
) {
if (moduleName.endsWith('.js')) {
moduleName = moduleName.substring(
0,
moduleName.length - 3
);
}
if (moduleName.endsWith('/')) {
moduleName = moduleName.substring(0, moduleName.length - 1);
}
}
if (moduleName.endsWith('/')) {
moduleName = moduleName.substring(
0,
moduleName.length - 1
);
}
}
argument.value = moduleName;
}
}
}
},
},
},
};
argument.value = moduleName;
}
}
}
},
},
},
};
}

@@ -50,15 +56,15 @@

function isPackageName(moduleName) {
const firstSlashIndex = moduleName.indexOf('/');
const firstSlashIndex = moduleName.indexOf('/');
if (firstSlashIndex == -1) {
return true;
}
if (firstSlashIndex == -1) {
return true;
}
const restOfModuleName = moduleName.substring(firstSlashIndex + 1);
const restOfModuleName = moduleName.substring(firstSlashIndex + 1);
if (moduleName.startsWith('@') && restOfModuleName.indexOf('/') == -1) {
return true;
}
if (moduleName.startsWith('@') && restOfModuleName.indexOf('/') == -1) {
return true;
}
return false;
return false;
}

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc