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

@formatjs/ts-transformer

Package Overview
Dependencies
Maintainers
2
Versions
172
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@formatjs/ts-transformer - npm Package Compare versions

Comparing version 1.0.1 to 1.0.2

tests/fixtures/noImport.tsx

11

CHANGELOG.md

@@ -6,4 +6,15 @@ # Change Log

## [1.0.2](https://github.com/formatjs/formatjs/compare/@formatjs/ts-transformer@1.0.1...@formatjs/ts-transformer@1.0.2) (2019-11-06)
### Bug Fixes
* **@formatjs/ts-transformer:** handle case where we dont import hooks ([6afb5af](https://github.com/formatjs/formatjs/commit/6afb5af6ca1246327d36d06c4930dec21f8b0421))
## 1.0.1 (2019-11-05)
**Note:** Version bump only for package @formatjs/ts-transformer

38

dist/transform.js

@@ -57,3 +57,3 @@ "use strict";

}
function extractMessageDescriptor(node, sf, opts) {
function extractMessageDescriptor(node, sf, { overrideIdFn, extractSourceLocation }) {
let properties = undefined;

@@ -91,7 +91,6 @@ if (ts.isObjectLiteralExpression(node)) {

// We extracted nothing
if (!msg.id && !msg.defaultMessage && !msg.description) {
if (!Object.keys(msg).length) {
return;
}
if (!msg.id && opts.overrideIdFn) {
const { overrideIdFn } = opts;
if (!msg.id && typeof overrideIdFn === 'function') {
switch (typeof overrideIdFn) {

@@ -106,3 +105,3 @@ case 'string':

}
if (opts.extractSourceLocation) {
if (extractSourceLocation) {
return Object.assign(Object.assign({}, msg), { file: sf.fileName, start: node.getStart(sf), end: node.getEnd() });

@@ -211,10 +210,2 @@ }

}
function getImportSpecifierHooks(node, sf, opts) {
const moduleSpecifier = trimSingleQuote(node.moduleSpecifier.getText(sf));
if (moduleSpecifier !== (opts.moduleSourceName || 'react-intl') ||
!node.importClause) {
return;
}
return node.importClause.namedBindings;
}
function transform(opts) {

@@ -224,20 +215,9 @@ opts = Object.assign(Object.assign({}, DEFAULT_OPTS), opts);

return (ctx) => {
let importedSpecifierHooks;
function getVisitor(sf) {
const visitor = (node) => {
if (ts.isImportDeclaration(node)) {
importedSpecifierHooks =
getImportSpecifierHooks(node, sf, opts) || importedSpecifierHooks;
}
if (!importedSpecifierHooks ||
!ts.isNamedImports(importedSpecifierHooks)) {
return ts.visitEachChild(node, visitor, ctx);
}
let newNode = undefined;
if (ts.isCallExpression(node)) {
newNode = extractMessagesFromCallExpression(node, program, sf, opts);
}
if (ts.isJsxOpeningElement(node) || ts.isJsxSelfClosingElement(node)) {
newNode = extractMessageFromJsxComponent(node, program, sf, opts);
}
const newNode = ts.isCallExpression(node)
? extractMessagesFromCallExpression(node, program, sf, opts)
: ts.isJsxOpeningElement(node) || ts.isJsxSelfClosingElement(node)
? extractMessageFromJsxComponent(node, program, sf, opts)
: undefined;
return newNode || ts.visitEachChild(node, visitor, ctx);

@@ -244,0 +224,0 @@ };

{
"name": "@formatjs/ts-transformer",
"version": "1.0.1",
"version": "1.0.2",
"description": "TS Compiler transformer for formatjs",

@@ -33,3 +33,3 @@ "main": "dist/index.js",

"homepage": "https://github.com/formatjs/formatjs#readme",
"gitHead": "cab515dc05b3c6f1a819ee3a0ad9a8d09278cc78"
"gitHead": "38ad5608c24f9a888efac3f09d33e48912ad91b9"
}

@@ -158,3 +158,3 @@ import * as ts from 'typescript';

sf: ts.SourceFile,
opts: Opts
{overrideIdFn, extractSourceLocation}: Opts
): MessageDescriptor | undefined {

@@ -193,7 +193,6 @@ let properties: ts.NodeArray<ts.ObjectLiteralElement> | undefined = undefined;

// We extracted nothing
if (!msg.id && !msg.defaultMessage && !msg.description) {
if (!Object.keys(msg).length) {
return;
}
if (!msg.id && opts.overrideIdFn) {
const {overrideIdFn} = opts;
if (!msg.id && typeof overrideIdFn === 'function') {
switch (typeof overrideIdFn) {

@@ -212,3 +211,3 @@ case 'string':

}
if (opts.extractSourceLocation) {
if (extractSourceLocation) {
return {

@@ -375,17 +374,2 @@ ...msg,

function getImportSpecifierHooks(
node: ts.ImportDeclaration,
sf: ts.SourceFile,
opts: Opts
) {
const moduleSpecifier = trimSingleQuote(node.moduleSpecifier.getText(sf));
if (
moduleSpecifier !== (opts.moduleSourceName || 'react-intl') ||
!node.importClause
) {
return;
}
return node.importClause.namedBindings as ts.NamedImports;
}
export function transform(opts: Opts) {

@@ -395,23 +379,10 @@ opts = {...DEFAULT_OPTS, ...opts};

return (ctx: ts.TransformationContext): ts.Transformer<ts.SourceFile> => {
let importedSpecifierHooks: ts.NamedImports | undefined;
function getVisitor(sf: ts.SourceFile) {
const visitor: ts.Visitor = (node: ts.Node): ts.Node => {
if (ts.isImportDeclaration(node)) {
importedSpecifierHooks =
getImportSpecifierHooks(node, sf, opts) || importedSpecifierHooks;
}
if (
!importedSpecifierHooks ||
!ts.isNamedImports(importedSpecifierHooks)
) {
return ts.visitEachChild(node, visitor, ctx);
}
const newNode = ts.isCallExpression(node)
? extractMessagesFromCallExpression(node, program, sf, opts)
: ts.isJsxOpeningElement(node) || ts.isJsxSelfClosingElement(node)
? extractMessageFromJsxComponent(node, program, sf, opts)
: undefined;
let newNode: ts.Node | undefined = undefined;
if (ts.isCallExpression(node)) {
newNode = extractMessagesFromCallExpression(node, program, sf, opts);
}
if (ts.isJsxOpeningElement(node) || ts.isJsxSelfClosingElement(node)) {
newNode = extractMessageFromJsxComponent(node, program, sf, opts);
}
return newNode || ts.visitEachChild(node, visitor, ctx);

@@ -418,0 +389,0 @@ };

@@ -29,2 +29,3 @@ import {join} from 'path';

},
extractFromFormatMessageCall: true,
},

@@ -34,2 +35,5 @@ removeDefaultMessage: {

},
noImport: {
extractFromFormatMessageCall: true,
},
removeDescription: {},

@@ -36,0 +40,0 @@ };

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

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