@formatjs/ts-transformer
Advanced tools
Comparing version 2.0.4 to 2.0.5
@@ -6,2 +6,13 @@ # Change Log | ||
## [2.0.5](https://github.com/formatjs/formatjs/compare/@formatjs/ts-transformer@2.0.4...@formatjs/ts-transformer@2.0.5) (2020-05-08) | ||
### Bug Fixes | ||
* **@formatjs/ts-transformer:** fix ID mangling ([7d3c701](https://github.com/formatjs/formatjs/commit/7d3c70179b7297c607963a33148fd63a619d66ed)) | ||
## [2.0.4](https://github.com/formatjs/formatjs/compare/@formatjs/ts-transformer@2.0.3...@formatjs/ts-transformer@2.0.4) (2020-05-08) | ||
@@ -8,0 +19,0 @@ |
@@ -5,3 +5,7 @@ "use strict"; | ||
const loader_utils_1 = require("loader-utils"); | ||
const MESSAGE_DESC_KEYS = new Set(['id', 'defaultMessage', 'description']); | ||
const MESSAGE_DESC_KEYS = [ | ||
'id', | ||
'defaultMessage', | ||
'description', | ||
]; | ||
const DEFAULT_OPTS = { | ||
@@ -122,3 +126,3 @@ onMsgExtracted: () => undefined, | ||
const clonedEl = ts.getMutableClone(node); | ||
clonedEl.attributes = setAttributesInObject(clonedEl.attributes, { | ||
clonedEl.attributes = setAttributesInJsxAttributes(clonedEl.attributes, { | ||
defaultMessage: opts.removeDefaultMessage ? undefined : msg.defaultMessage, | ||
@@ -132,20 +136,13 @@ id: msg.id, | ||
const newProps = []; | ||
for (const k of MESSAGE_DESC_KEYS) { | ||
const val = msg[k]; | ||
if (val) { | ||
newProps.push(ts.createPropertyAssignment(k, ts.createStringLiteral(String(val)))); | ||
} | ||
} | ||
for (const prop of node.properties) { | ||
if ((ts.isJsxAttribute(prop) || ts.isPropertyAssignment(prop)) && | ||
ts.isIdentifier(prop.name)) { | ||
const k = prop.name.text; | ||
if (MESSAGE_DESC_KEYS.has(k)) { | ||
const val = msg[k]; | ||
if (val) { | ||
const keyNode = ts.createIdentifier(k); | ||
const valNode = ts.createStringLiteral(val + ''); | ||
if (ts.isJsxAttributes(node)) { | ||
newProps.push(ts.createJsxAttribute(keyNode, valNode)); | ||
} | ||
else if (ts.isObjectLiteralExpression(node)) { | ||
newProps.push(ts.createPropertyAssignment(k, valNode)); | ||
} | ||
} | ||
continue; | ||
} | ||
if (ts.isPropertyAssignment(prop) && | ||
ts.isIdentifier(prop.name) && | ||
MESSAGE_DESC_KEYS.includes(prop.name.text)) { | ||
continue; | ||
} | ||
@@ -157,2 +154,22 @@ newProps.push(prop); | ||
} | ||
function setAttributesInJsxAttributes(node, msg) { | ||
const newNode = ts.getMutableClone(node); | ||
const newProps = []; | ||
for (const k of MESSAGE_DESC_KEYS) { | ||
const val = msg[k]; | ||
if (val) { | ||
newProps.push(ts.createJsxAttribute(ts.createIdentifier(k), ts.createStringLiteral(String(val)))); | ||
} | ||
} | ||
for (const prop of node.properties) { | ||
if (ts.isJsxAttribute(prop) && | ||
ts.isIdentifier(prop.name) && | ||
MESSAGE_DESC_KEYS.includes(prop.name.text)) { | ||
continue; | ||
} | ||
newProps.push(prop); | ||
} | ||
newNode.properties = ts.createNodeArray(newProps); | ||
return newNode; | ||
} | ||
function extractMessagesFromCallExpression(node, opts, sf) { | ||
@@ -165,3 +182,5 @@ const { onMsgExtracted } = opts; | ||
const msgs = properties | ||
.map(prop => extractMessageDescriptor(prop.initializer, opts, sf)) | ||
.filter((prop) => ts.isPropertyAssignment(prop)) | ||
.map(prop => ts.isObjectLiteralExpression(prop.initializer) && | ||
extractMessageDescriptor(prop.initializer, opts, sf)) | ||
.filter((msg) => !!msg); | ||
@@ -177,3 +196,4 @@ if (!msgs.length) { | ||
const clonedProperties = ts.createNodeArray(properties.map((prop, i) => { | ||
if (!ts.isObjectLiteralExpression(prop.initializer)) { | ||
if (!ts.isPropertyAssignment(prop) || | ||
!ts.isObjectLiteralExpression(prop.initializer)) { | ||
return prop; | ||
@@ -180,0 +200,0 @@ } |
{ | ||
"name": "@formatjs/ts-transformer", | ||
"version": "2.0.4", | ||
"version": "2.0.5", | ||
"description": "TS Compiler transformer for formatjs", | ||
@@ -38,3 +38,3 @@ "main": "dist/index.js", | ||
}, | ||
"gitHead": "88eaaa8a7871cbd4c7f0bfb849be8f3e07de80a7" | ||
"gitHead": "766a898093316bca36b6a04892f0873b433d30c8" | ||
} |
@@ -13,3 +13,7 @@ import * as ts from 'typescript'; | ||
const MESSAGE_DESC_KEYS = new Set(['id', 'defaultMessage', 'description']); | ||
const MESSAGE_DESC_KEYS: Array<keyof MessageDescriptor> = [ | ||
'id', | ||
'defaultMessage', | ||
'description', | ||
]; | ||
@@ -209,6 +213,6 @@ export interface Opts { | ||
const clonedEl = ts.getMutableClone(node); | ||
clonedEl.attributes = setAttributesInObject(clonedEl.attributes, { | ||
clonedEl.attributes = setAttributesInJsxAttributes(clonedEl.attributes, { | ||
defaultMessage: opts.removeDefaultMessage ? undefined : msg.defaultMessage, | ||
id: msg.id, | ||
}) as ts.JsxAttributes; | ||
}); | ||
return clonedEl; | ||
@@ -218,3 +222,3 @@ } | ||
function setAttributesInObject( | ||
node: ts.ObjectLiteralExpression | ts.JsxAttributes, | ||
node: ts.ObjectLiteralExpression, | ||
msg: MessageDescriptor | ||
@@ -224,28 +228,55 @@ ) { | ||
const newProps = []; | ||
for (const k of MESSAGE_DESC_KEYS) { | ||
const val = msg[k]; | ||
if (val) { | ||
newProps.push( | ||
ts.createPropertyAssignment(k, ts.createStringLiteral(String(val))) | ||
); | ||
} | ||
} | ||
for (const prop of node.properties) { | ||
if ( | ||
(ts.isJsxAttribute(prop) || ts.isPropertyAssignment(prop)) && | ||
ts.isIdentifier(prop.name) | ||
ts.isPropertyAssignment(prop) && | ||
ts.isIdentifier(prop.name) && | ||
MESSAGE_DESC_KEYS.includes(prop.name.text as keyof MessageDescriptor) | ||
) { | ||
const k = prop.name.text as keyof MessageDescriptor; | ||
if (MESSAGE_DESC_KEYS.has(k)) { | ||
const val = msg[k]; | ||
if (val) { | ||
const keyNode = ts.createIdentifier(k); | ||
const valNode = ts.createStringLiteral(val + ''); | ||
if (ts.isJsxAttributes(node)) { | ||
newProps.push(ts.createJsxAttribute(keyNode, valNode)); | ||
} else if (ts.isObjectLiteralExpression(node)) { | ||
newProps.push(ts.createPropertyAssignment(k, valNode)); | ||
} | ||
} | ||
continue; | ||
} | ||
continue; | ||
} | ||
newProps.push(prop); | ||
} | ||
newNode.properties = ts.createNodeArray(newProps) as any; | ||
newNode.properties = ts.createNodeArray(newProps); | ||
return newNode; | ||
} | ||
function setAttributesInJsxAttributes( | ||
node: ts.JsxAttributes, | ||
msg: MessageDescriptor | ||
) { | ||
const newNode = ts.getMutableClone(node); | ||
const newProps = []; | ||
for (const k of MESSAGE_DESC_KEYS) { | ||
const val = msg[k]; | ||
if (val) { | ||
newProps.push( | ||
ts.createJsxAttribute( | ||
ts.createIdentifier(k), | ||
ts.createStringLiteral(String(val)) | ||
) | ||
); | ||
} | ||
} | ||
for (const prop of node.properties) { | ||
if ( | ||
ts.isJsxAttribute(prop) && | ||
ts.isIdentifier(prop.name) && | ||
MESSAGE_DESC_KEYS.includes(prop.name.text as keyof MessageDescriptor) | ||
) { | ||
continue; | ||
} | ||
newProps.push(prop); | ||
} | ||
newNode.properties = ts.createNodeArray(newProps); | ||
return newNode; | ||
} | ||
function extractMessagesFromCallExpression( | ||
@@ -260,13 +291,12 @@ node: ts.CallExpression, | ||
if (ts.isObjectLiteralExpression(descriptorsObj)) { | ||
const properties = descriptorsObj.properties as ts.NodeArray< | ||
ts.PropertyAssignment | ||
>; | ||
const properties = descriptorsObj.properties; | ||
const msgs = properties | ||
.map(prop => | ||
extractMessageDescriptor( | ||
prop.initializer as ts.ObjectLiteralExpression, | ||
opts, | ||
sf | ||
) | ||
.filter<ts.PropertyAssignment>((prop): prop is ts.PropertyAssignment => | ||
ts.isPropertyAssignment(prop) | ||
) | ||
.map( | ||
prop => | ||
ts.isObjectLiteralExpression(prop.initializer) && | ||
extractMessageDescriptor(prop.initializer, opts, sf) | ||
) | ||
.filter((msg): msg is MessageDescriptor => !!msg); | ||
@@ -284,3 +314,6 @@ if (!msgs.length) { | ||
properties.map((prop, i) => { | ||
if (!ts.isObjectLiteralExpression(prop.initializer)) { | ||
if ( | ||
!ts.isPropertyAssignment(prop) || | ||
!ts.isObjectLiteralExpression(prop.initializer) | ||
) { | ||
return prop; | ||
@@ -287,0 +320,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
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
173890
1265