You're Invited:Meet the Socket Team at BlackHat and DEF CON in Las Vegas, Aug 4-6.RSVP
Socket
Book a DemoInstallSign in
Socket

@ts-bridge/cli

Package Overview
Dependencies
Maintainers
0
Versions
18
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@ts-bridge/cli - npm Package Compare versions

Comparing version

to
0.5.0

15

CHANGELOG.md

@@ -10,2 +10,14 @@ # Changelog

## [0.5.0]
### Added
- Add shim for `require.resolve` ([#51](https://github.com/ts-bridge/ts-bridge/pull/51))
- This will replace `require.resolve` calls with an ESM-compatible version
when targeting ESM.
### Fixed
- Fix detection of global symbols ([#50](https://github.com/ts-bridge/ts-bridge/pull/50))
## [0.4.4]

@@ -127,3 +139,4 @@

[Unreleased]: https://github.com/ts-bridge/ts-bridge/compare/@ts-bridge/cli@0.4.4...HEAD
[Unreleased]: https://github.com/ts-bridge/ts-bridge/compare/@ts-bridge/cli@0.5.0...HEAD
[0.5.0]: https://github.com/ts-bridge/ts-bridge/compare/@ts-bridge/cli@0.4.4...@ts-bridge/cli@0.5.0
[0.4.4]: https://github.com/ts-bridge/ts-bridge/compare/@ts-bridge/cli@0.4.3...@ts-bridge/cli@0.4.4

@@ -130,0 +143,0 @@ [0.4.3]: https://github.com/ts-bridge/ts-bridge/compare/@ts-bridge/cli@0.4.2...@ts-bridge/cli@0.4.3

10

dist/generator.js
import typescript from 'typescript';
import { getCommonJsExports, isCommonJs } from './module-resolver.js';
import { getIdentifierName } from './utils.js';
import { getDefinedArray, getIdentifierName } from './utils.js';
const { factory, isNamedExports, isNamedImports, isNamespaceImport, isStringLiteral, NodeFlags, SymbolFlags, SyntaxKind, } = typescript;

@@ -30,3 +30,9 @@ /**

symbol.escapedName === `_${symbolName}`);
return foundSymbol?.parent?.escapedName === '__global';
const declarations = getDefinedArray(foundSymbol?.getDeclarations());
for (const declaration of declarations) {
if (declaration.getSourceFile().isDeclarationFile) {
return true;
}
}
return false;
}

@@ -33,0 +39,0 @@ /**

@@ -100,8 +100,5 @@ import type { Statement } from 'typescript';

* ```ts
* import { createRequire } from 'module';
* import { createRequire as $createRequire } from 'module';
*
* function require(identifier: string, url: string): any {
* const fn = createRequire(url);
* return fn(identifier);
* }
* const $require = $createRequire(import.meta.url);
* ```

@@ -113,6 +110,8 @@ *

*
* @param functionName - The name of the function to create.
* @param functionName - The name of the require function to create.
* @param createRequireFunctionName - The name of the `createRequire` function
* to import.
* @returns The AST for the `require` function.
*/
export declare function getRequireHelperFunction(functionName: string): [Statement, Statement];
export declare function getRequireHelperFunction(functionName: string, createRequireFunctionName: string): [Statement, Statement];
//# sourceMappingURL=shims.d.ts.map

@@ -156,8 +156,5 @@ import typescript from 'typescript';

* ```ts
* import { createRequire } from 'module';
* import { createRequire as $createRequire } from 'module';
*
* function require(identifier: string, url: string): any {
* const fn = createRequire(url);
* return fn(identifier);
* }
* const $require = $createRequire(import.meta.url);
* ```

@@ -169,20 +166,18 @@ *

*
* @param functionName - The name of the function to create.
* @param functionName - The name of the require function to create.
* @param createRequireFunctionName - The name of the `createRequire` function
* to import.
* @returns The AST for the `require` function.
*/
export function getRequireHelperFunction(functionName) {
export function getRequireHelperFunction(functionName, createRequireFunctionName) {
return [
factory.createImportDeclaration(undefined, factory.createImportClause(false, undefined, factory.createNamedImports([
factory.createImportSpecifier(false, factory.createIdentifier('createRequire'), factory.createIdentifier(functionName)),
factory.createImportSpecifier(false, factory.createIdentifier('createRequire'), factory.createIdentifier(createRequireFunctionName)),
])), factory.createStringLiteral('module'), undefined),
factory.createFunctionDeclaration(undefined, undefined, factory.createIdentifier('require'), undefined, [
factory.createParameterDeclaration(undefined, undefined, factory.createIdentifier('identifier'), undefined, factory.createKeywordTypeNode(SyntaxKind.StringKeyword), undefined),
factory.createParameterDeclaration(undefined, undefined, factory.createIdentifier('url'), undefined, factory.createKeywordTypeNode(SyntaxKind.StringKeyword), undefined),
], factory.createKeywordTypeNode(SyntaxKind.AnyKeyword), factory.createBlock([
factory.createVariableStatement(undefined, factory.createVariableDeclarationList([
factory.createVariableDeclaration(factory.createIdentifier('fn'), undefined, undefined, factory.createCallExpression(factory.createIdentifier(functionName), undefined, [factory.createIdentifier('url')])),
], NodeFlags.Const)),
factory.createReturnStatement(factory.createCallExpression(factory.createIdentifier('fn'), undefined, [factory.createIdentifier('identifier')])),
], true)),
factory.createVariableStatement(undefined, factory.createVariableDeclarationList([
factory.createVariableDeclaration(factory.createIdentifier(functionName), undefined, undefined, factory.createCallExpression(factory.createIdentifier(createRequireFunctionName), undefined, [
factory.createPropertyAccessExpression(factory.createMetaProperty(SyntaxKind.ImportKeyword, factory.createIdentifier('meta')), factory.createIdentifier('url')),
])),
], NodeFlags.Const)),
];
}

@@ -187,10 +187,7 @@ import { evaluateModule, getVirtualEnvironment } from '@ts-bridge/test-utils';

it('returns the `require` helper function', () => {
const ast = getRequireHelperFunction('createRequire');
const ast = getRequireHelperFunction('require', 'createRequire');
expect(compile(ast)).toMatchInlineSnapshot(`
""use strict";
import { createRequire as createRequire } from "module";
function require(identifier, url) {
const fn = createRequire(url);
return fn(identifier);
}
const require = createRequire(import.meta.url);
"

@@ -203,3 +200,3 @@ `);

const code = `
${compile(getRequireHelperFunction('createRequire'))}
${compile(getRequireHelperFunction('require', 'createRequire'))}
export { require };

@@ -206,0 +203,0 @@ `;

@@ -253,2 +253,3 @@ import typescript from 'typescript';

let insertShim = false;
const requireFunctionName = getUniqueIdentifier(typeChecker, sourceFile, 'require');
const createRequireFunctionName = getUniqueIdentifier(typeChecker, sourceFile, 'createRequire');

@@ -262,4 +263,14 @@ const visitor = (node) => {

insertShim = true;
return factory.createCallExpression(factory.createIdentifier('require'), undefined, [node.arguments[0], getImportMetaUrl()]);
return factory.createCallExpression(factory.createIdentifier(requireFunctionName), undefined, [node.arguments[0]]);
}
if (isCallExpression(node) &&
isPropertyAccessExpression(node.expression) &&
isIdentifier(node.expression.expression) &&
node.expression.expression.text === 'require' &&
node.expression.name.text === 'resolve' &&
isGlobal(typeChecker, node, 'require') &&
node.arguments[0]) {
insertShim = true;
return factory.createCallExpression(factory.createPropertyAccessExpression(factory.createIdentifier(requireFunctionName), 'resolve'), undefined, [node.arguments[0]]);
}
return visitEachChild(node, visitor, context);

@@ -270,3 +281,3 @@ };

return factory.updateSourceFile(modifiedSourceFile, [
...getRequireHelperFunction(createRequireFunctionName),
...getRequireHelperFunction(requireFunctionName, createRequireFunctionName),
...modifiedSourceFile.statements,

@@ -273,0 +284,0 @@ ]);

@@ -508,7 +508,4 @@ import { getFixture } from '@ts-bridge/test-utils';

"import { createRequire as $createRequire } from "module";
function require(identifier, url) {
const fn = $createRequire(url);
return fn(identifier);
}
const { builtinModules } = require('module', import.meta.url);
const $require = $createRequire(import.meta.url);
const { builtinModules } = $require('module');
console.log(builtinModules);

@@ -518,2 +515,10 @@ "

});
it('adds a shim when using `require.resolve`', async () => {
expect(files['require-resolve.js']).toMatchInlineSnapshot(`
"import { createRequire as $createRequire } from "module";
const $require = $createRequire(import.meta.url);
console.log($require.resolve('path/to/file.js'));
"
`);
});
it('does not add a shim when `require` refers to a variable in scope', async () => {

@@ -520,0 +525,0 @@ expect(files['require-in-scope.js']).toMatchInlineSnapshot(`

{
"name": "@ts-bridge/cli",
"version": "0.4.4",
"version": "0.5.0",
"description": "Bridge the gap between ES modules and CommonJS modules with an easy-to-use alternative to `tsc`.",

@@ -5,0 +5,0 @@ "keywords": [

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet