Socket
Socket
Sign inDemoInstall

typescript-transform-paths

Package Overview
Dependencies
Maintainers
2
Versions
59
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

typescript-transform-paths - npm Package Compare versions

Comparing version 2.2.4 to 3.0.0

dist/utils/resolve-module-name.js

7

CHANGELOG.md

@@ -5,2 +5,9 @@ # Changelog

## [3.0.0](https://github.com/LeDDGroup/typescript-transform-paths/compare/v2.2.4...v3.0.0) (2021-06-16)
### Features
* Rewrote resolution strategy + various improvements (see notes) ([ed1df79](https://github.com/LeDDGroup/typescript-transform-paths/commit/ed1df795063c4d08b2a29b8b229d6ac7d134b816)), closes [#109](https://github.com/LeDDGroup/typescript-transform-paths/issues/109) [#110](https://github.com/LeDDGroup/typescript-transform-paths/issues/110) [#106](https://github.com/LeDDGroup/typescript-transform-paths/issues/106) [#108](https://github.com/LeDDGroup/typescript-transform-paths/issues/108) [#107](https://github.com/LeDDGroup/typescript-transform-paths/issues/107) [#103](https://github.com/LeDDGroup/typescript-transform-paths/issues/103)
### [2.2.4](https://github.com/LeDDGroup/typescript-transform-paths/compare/v2.2.3...v2.2.4) (2021-05-20)

@@ -7,0 +14,0 @@

16

dist/transformer.js

@@ -12,2 +12,3 @@ "use strict";

const minimatch_1 = require("minimatch");
const ts_helpers_1 = require("./utils/ts-helpers");
/* ****************************************************************************************************************** *

@@ -21,6 +22,8 @@ * Transformer

const compilerOptions = program.getCompilerOptions();
const implicitExtensions = utils_1.getImplicitExtensions(compilerOptions);
const rootDirs = (_a = compilerOptions.rootDirs) === null || _a === void 0 ? void 0 : _a.filter(path_1.default.isAbsolute);
return (transformationContext) => {
var _a;
var _a, _b;
const pathsBasePath = (_a = compilerOptions.pathsBasePath) !== null && _a !== void 0 ? _a : compilerOptions.baseUrl;
if (!pathsBasePath || !compilerOptions.paths)
return (sourceFile) => sourceFile;
const tsTransformPathsContext = {

@@ -31,3 +34,2 @@ compilerOptions,

tsFactory: transformationContext.factory,
implicitExtensions,
program,

@@ -37,8 +39,10 @@ rootDirs,

tsInstance,
pathsBasePath,
getCanonicalFileName: tsInstance.createGetCanonicalFileName(tsInstance.sys.useCaseSensitiveFileNames),
tsThreeInstance: utils_1.cast(tsInstance),
excludeMatchers: (_a = config.exclude) === null || _a === void 0 ? void 0 : _a.map((globPattern) => new minimatch_1.Minimatch(globPattern, { matchBase: true })),
excludeMatchers: (_b = config.exclude) === null || _b === void 0 ? void 0 : _b.map((globPattern) => new minimatch_1.Minimatch(globPattern, { matchBase: true })),
parsedCommandLine: ts_helpers_1.createParsedCommandLineForProgram(tsInstance, program),
outputFileNamesCache: new Map(),
};
return (sourceFile) => {
if (!compilerOptions.baseUrl || !compilerOptions.paths)
return sourceFile;
const visitorContext = Object.assign(Object.assign({}, tsTransformPathsContext), { sourceFile, isDeclarationFile: sourceFile.isDeclarationFile, originalSourceFile: tsInstance.getOriginalSourceFile(sourceFile), getVisitor() {

@@ -45,0 +49,0 @@ return visitor_1.nodeVisitor.bind(this);

@@ -6,4 +6,3 @@ "use strict";

Object.defineProperty(exports, "__esModule", { value: true });
exports.getImplicitExtensions = exports.cast = exports.isBaseDir = exports.isURL = void 0;
const typescript_1 = __importDefault(require("typescript"));
exports.maybeAddRelativeLocalPrefix = exports.isBaseDir = exports.cast = exports.isURL = void 0;
const url_1 = __importDefault(require("url"));

@@ -16,18 +15,10 @@ const path_1 = __importDefault(require("path"));

exports.isURL = isURL;
const isBaseDir = (base, dir) => { var _a; return ((_a = path_1.default.relative(base, dir)) === null || _a === void 0 ? void 0 : _a[0]) !== "."; };
exports.isBaseDir = isBaseDir;
const cast = (v) => v;
exports.cast = cast;
/**
* @returns Array of implicit extensions, given CompilerOptions
*/
function getImplicitExtensions(options) {
let res = [".ts", ".d.ts"];
let { allowJs, jsx } = options;
const allowJsx = !!jsx && (jsx !== typescript_1.default.JsxEmit.None);
allowJs && res.push(".js", ".cjs", ".mjs");
allowJsx && res.push(".tsx");
allowJs && allowJsx && res.push(".jsx");
return res;
}
exports.getImplicitExtensions = getImplicitExtensions;
const isBaseDir = (baseDir, testDir) => {
const relative = path_1.default.relative(baseDir, testDir);
return relative ? !relative.startsWith("..") && !path_1.default.isAbsolute(relative) : true;
};
exports.isBaseDir = isBaseDir;
const maybeAddRelativeLocalPrefix = (p) => (p[0] === "." ? p : `./${p}`);
exports.maybeAddRelativeLocalPrefix = maybeAddRelativeLocalPrefix;
"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.resolvePathAndUpdateNode = void 0;
const path_1 = __importDefault(require("path"));
const general_utils_1 = require("./general-utils");
const ts_helpers_1 = require("./ts-helpers");
const resolve_module_name_1 = require("./resolve-module-name");
/* ****************************************************************************************************************** */
// region: Config
/* ****************************************************************************************************************** */
const explicitExtensions = [".js", ".jsx", ".cjs", ".mjs"];
// endregion
/* ****************************************************************************************************************** */
// region: Node Updater Utility

@@ -21,16 +14,28 @@ /* ****************************************************************************************************************** */

function resolvePathAndUpdateNode(context, node, moduleName, updaterFn) {
const { sourceFile, compilerOptions, tsInstance, config, implicitExtensions, factory } = context;
const { sourceFile, tsInstance, factory } = context;
const { normalizePath } = tsInstance;
/* Handle JSDoc statement tags */
const tags = getStatementTags();
// Skip if @no-transform-path specified
if (tags === null || tags === void 0 ? void 0 : tags.shouldSkip)
if (tags.shouldSkip)
return node;
const resolutionResult = resolvePath(tags === null || tags === void 0 ? void 0 : tags.overridePath);
// Skip if can't be resolved
if (!resolutionResult || !resolutionResult.outputPath)
// Accommodate direct override via @transform-path tag
if (tags.overridePath) {
const transformedPath = !general_utils_1.isURL(tags.overridePath)
? general_utils_1.maybeAddRelativeLocalPrefix(normalizePath(tags.overridePath))
: tags.overridePath;
return updaterFn(factory.createStringLiteral(transformedPath));
}
/* Resolve Module */
// Skip if no paths match found
if (!ts_helpers_1.isModulePathsMatch(context, moduleName))
return node;
const { outputPath, filePath } = resolutionResult;
// Check if matches exclusion
if (filePath && context.excludeMatchers)
const res = resolve_module_name_1.resolveModuleName(context, moduleName);
if (!res)
return void 0;
const { outputPath, resolvedPath } = res;
/* Skip if matches exclusion */
if (context.excludeMatchers)
for (const matcher of context.excludeMatchers)
if (matcher.match(filePath))
if (matcher.match(outputPath) || (resolvedPath && matcher.match(resolvedPath)))
return node;

@@ -41,73 +46,2 @@ return updaterFn(factory.createStringLiteral(outputPath));

* ********************************************************* */
function resolvePath(overridePath) {
/* Handle overridden path -- ie. @transform-path ../my/path) */
if (overridePath) {
return {
outputPath: filePathToOutputPath(overridePath, path_1.default.extname(overridePath)),
filePath: overridePath,
};
}
/* Have Compiler API attempt to resolve */
const { resolvedModule, failedLookupLocations } = tsInstance.resolveModuleName(moduleName, sourceFile.fileName, compilerOptions, tsInstance.sys);
// No transform for node-modules
if (resolvedModule === null || resolvedModule === void 0 ? void 0 : resolvedModule.isExternalLibraryImport)
return void 0;
/* Handle non-resolvable module */
if (!resolvedModule) {
const maybeURL = failedLookupLocations[0];
if (!general_utils_1.isURL(maybeURL))
return void 0;
return { outputPath: maybeURL };
}
/* Handle resolved module */
const { extension, resolvedFileName } = resolvedModule;
return {
outputPath: filePathToOutputPath(resolvedFileName, extension),
filePath: resolvedFileName,
};
}
function filePathToOutputPath(filePath, extension) {
if (path_1.default.isAbsolute(filePath)) {
let sourceFileDir = tsInstance.normalizePath(path_1.default.dirname(sourceFile.fileName));
let moduleDir = path_1.default.dirname(filePath);
/* Handle rootDirs mapping */
if (config.useRootDirs && context.rootDirs) {
let fileRootDir = "";
let moduleRootDir = "";
for (const rootDir of context.rootDirs) {
if (general_utils_1.isBaseDir(rootDir, filePath) && rootDir.length > moduleRootDir.length)
moduleRootDir = rootDir;
if (general_utils_1.isBaseDir(rootDir, sourceFile.fileName) && rootDir.length > fileRootDir.length)
fileRootDir = rootDir;
}
/* Remove base dirs to make relative to root */
if (fileRootDir && moduleRootDir) {
sourceFileDir = path_1.default.relative(fileRootDir, sourceFileDir);
moduleDir = path_1.default.relative(moduleRootDir, moduleDir);
}
}
/* Make path relative */
filePath = tsInstance.normalizePath(path_1.default.join(path_1.default.relative(sourceFileDir, moduleDir), path_1.default.basename(filePath)));
}
/* Fixup filename */
if (extension) {
const isImplicitIndex = path_1.default.basename(filePath, extension) === 'index' &&
path_1.default.basename(moduleName, path_1.default.extname(moduleName)) !== 'index';
// Remove implicit index
if (isImplicitIndex)
filePath = path_1.default.dirname(filePath);
// Remove implicit extension
else if (implicitExtensions.includes(extension))
filePath = filePath.slice(0, -extension.length) + maybeGetExplicitExtension(filePath, extension);
}
return filePath[0] === "." || general_utils_1.isURL(filePath) ? filePath : `./${filePath}`;
}
function maybeGetExplicitExtension(filePath, resolvedExtension) {
const moduleExtension = path_1.default.extname(moduleName);
if (moduleExtension && !explicitExtensions.includes(moduleExtension))
return "";
return path_1.default.basename(moduleName, moduleExtension) === path_1.default.basename(filePath, resolvedExtension)
? moduleExtension
: "";
}
function getStatementTags() {

@@ -114,0 +48,0 @@ var _a, _b, _c;

{
"name": "typescript-transform-paths",
"version": "2.2.4",
"version": "3.0.0",
"description": "Transforms module resolution paths using TypeScript path mapping",

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

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