@babel/types#JSXIdentifier TypeScript Examples
The following examples show how to use
@babel/types#JSXIdentifier.
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: index.ts From plasmic with MIT License | 6 votes |
serializeNamedAttribute = (
attrName: JSXIdentifier | JSXNamespacedName,
rawValue: PlasmicASTNode | null | undefined,
codeVersions: CodeVersions
) => {
const attrValue = rawValue
? serializePlasmicASTNode(rawValue, codeVersions)
: undefined;
return babel.types.jsxAttribute(attrName, wrapAsJsxAttrValue(attrValue));
}
Example #2
Source File: visitor.ts From react-dev-inspector with MIT License | 6 votes |
doJSXPathName: NodeHandler<JSXOpeningElement['name']> = (name) => {
const visitors: { [key in ElementTypes]: NodeHandler } = {
JSXIdentifier: doJSXIdentifierName,
JSXMemberExpression: doJSXMemberExpressionName,
JSXNamespacedName: doJSXNamespacedNameName,
}
return visitors[name.type](name)
}
Example #3
Source File: index.ts From tailchat with GNU General Public License v3.0 | 5 votes |
export default function sourceRef(): Plugin {
return {
name: 'source-ref',
transform(code, id) {
const filepath = id;
const ast = parse(code, {
sourceType: 'module',
plugins: ['jsx', 'typescript'],
});
traverse(ast, {
JSXOpeningElement(path) {
const location = path.node.loc;
if (!location) {
return;
}
if (Array.isArray(location)) {
return;
}
const name = path.node.name;
if (isJSXIdentifier(name) && name.name === 'Fragment') {
return;
}
if (
isJSXMemberExpression(name) &&
name.property.name === 'Fragment'
) {
return;
}
const line = location.start.line;
const col = location.start.column;
const attrs = path.node.attributes;
for (let i = 0; i < attrs.length; i++) {
const attr = attrs[i];
if (attr.type === 'JSXAttribute' && attr.name.name === TRACE_ID) {
// existed
return;
}
}
const traceId = `${filepath}:${line}:${col}`;
attrs.push(
jsxAttribute(jsxIdentifier(TRACE_ID), stringLiteral(traceId))
);
},
});
const res = generate(ast);
return { code: res.code, map: res.map };
},
};
}
Example #4
Source File: index.ts From tailchat with GNU General Public License v3.0 | 5 votes |
async function loader(this: LoaderContext<any>, source: string): Promise<void> {
const done = this.async();
const { available } = this.getOptions();
if (!available) {
// skip if not
done(null, source);
return;
}
const ast = parse(source, {
sourceType: 'module',
plugins: ['jsx', 'typescript'],
});
const filepath = this.resourcePath;
if (filepath.includes('node_modules')) {
done(null, source);
return;
}
traverse(ast, {
JSXOpeningElement(path) {
const location = path.node.loc;
if (!location) {
return;
}
if (Array.isArray(location)) {
return;
}
const name = path.node.name;
if (isJSXIdentifier(name) && name.name === 'Fragment') {
return;
}
if (isJSXMemberExpression(name) && name.property.name === 'Fragment') {
return;
}
const line = location.start.line;
const col = location.start.column;
const attrs = path.node.attributes;
for (let i = 0; i < attrs.length; i++) {
const attr = attrs[i];
if (attr.type === 'JSXAttribute' && attr.name.name === TRACE_ID) {
// existed
return;
}
}
const traceId = `${filepath}:${line}:${col}`;
attrs.push(jsxAttribute(jsxIdentifier(TRACE_ID), stringLiteral(traceId)));
},
});
const code = generate(ast).code;
done(null, code);
}
Example #5
Source File: jsxConverter.ts From react-native-decompiler with GNU Affero General Public License v3.0 | 5 votes |
private parseJsx(node: Expression): JSXText | JSXExpressionContainer | JSXSpreadChild | JSXElement | JSXFragment {
if (isStringLiteral(node)) {
return jsxText(node.value);
}
if (isCallExpression(node)) {
const args = node.arguments;
let name: JSXIdentifier | JSXMemberExpression | undefined;
if (isIdentifier(args[0]) || isStringLiteral(args[0])) {
name = jsxIdentifier(isIdentifier(args[0]) ? args[0].name : args[0].value);
} else if (isMemberExpression(args[0]) && isIdentifier(args[0].object) && isIdentifier(args[0].property)) {
name = jsxMemberExpression(jsxIdentifier(args[0].object.name), jsxIdentifier(args[0].property.name));
} else {
this.debugLog(`fail to parse component ${args[0].type} inside callExpression`);
return jsxExpressionContainer(node);
}
let props: JSXAttribute[] = [];
if (isObjectExpression(args[1])) {
props = args[1].properties.map((prop) => {
if (!isObjectProperty(prop) || !isIdentifier(prop.key)) return null;
if (isStringLiteral(prop.value)) {
return jsxAttribute(jsxIdentifier(prop.key.name), prop.value);
}
if (isBooleanLiteral(prop.value) && prop.value.value) {
return jsxAttribute(jsxIdentifier(prop.key.name), null);
}
if (isExpression(prop.value)) {
return jsxAttribute(jsxIdentifier(prop.key.name), jsxExpressionContainer(prop.value));
}
return null;
}).filter((e): e is JSXAttribute => e != null);
}
const children = args.slice(2).map((e) => (isExpression(e) ? this.parseJsx(e) : null)).filter((e): e is JSXElement => e != null);
if (children.length) {
return jsxElement(jsxOpeningElement(name, props), jsxClosingElement(name), children);
}
return jsxElement(jsxOpeningElement(name, props, true), null, []);
}
this.debugLog(`fail to parse component ${node.type}`);
return jsxExpressionContainer(node);
}
Example #6
Source File: jsxConverter.ts From react-native-decompiler with GNU Affero General Public License v3.0 | 5 votes |
private parseJsx(node: Expression): JSXText | JSXExpressionContainer | JSXSpreadChild | JSXElement | JSXFragment {
if (isStringLiteral(node)) {
return jsxText(node.value);
}
if (isCallExpression(node)) {
const args = node.arguments;
let name: JSXIdentifier | JSXMemberExpression | undefined;
if (isIdentifier(args[0]) || isStringLiteral(args[0])) {
name = jsxIdentifier(isIdentifier(args[0]) ? args[0].name : args[0].value);
} else if (isMemberExpression(args[0]) && isIdentifier(args[0].object) && isIdentifier(args[0].property)) {
name = jsxMemberExpression(jsxIdentifier(args[0].object.name), jsxIdentifier(args[0].property.name));
} else {
this.debugLog(`fail to parse component ${args[0].type} inside callExpression`);
return jsxExpressionContainer(node);
}
let props: JSXAttribute[] = [];
if (isObjectExpression(args[1])) {
props = args[1].properties.map((prop) => {
if (!isObjectProperty(prop) || !isIdentifier(prop.key)) return null;
if (isStringLiteral(prop.value)) {
return jsxAttribute(jsxIdentifier(prop.key.name), prop.value);
}
if (isBooleanLiteral(prop.value) && prop.value.value) {
return jsxAttribute(jsxIdentifier(prop.key.name), null);
}
if (isExpression(prop.value)) {
return jsxAttribute(jsxIdentifier(prop.key.name), jsxExpressionContainer(prop.value));
}
return null;
}).filter((e): e is JSXAttribute => e != null);
}
const children = args.slice(2).map((e) => (isExpression(e) ? this.parseJsx(e) : null)).filter((e): e is JSXElement => e != null);
if (children.length) {
return jsxElement(jsxOpeningElement(name, props), jsxClosingElement(name), children);
}
return jsxElement(jsxOpeningElement(name, props, true), null, []);
}
this.debugLog(`fail to parse component ${node.type}`);
return jsxExpressionContainer(node);
}
Example #7
Source File: jsxConverter.ts From react-native-decompiler with GNU Affero General Public License v3.0 | 5 votes |
private parseJsx(node: Expression): JSXText | JSXExpressionContainer | JSXSpreadChild | JSXElement | JSXFragment {
if (isStringLiteral(node)) {
return jsxText(node.value);
}
if (isCallExpression(node)) {
const args = node.arguments;
let name: JSXIdentifier | JSXMemberExpression | undefined;
if (isIdentifier(args[0]) || isStringLiteral(args[0])) {
name = jsxIdentifier(isIdentifier(args[0]) ? args[0].name : args[0].value);
} else if (isMemberExpression(args[0]) && isIdentifier(args[0].object) && isIdentifier(args[0].property)) {
name = jsxMemberExpression(jsxIdentifier(args[0].object.name), jsxIdentifier(args[0].property.name));
} else {
this.debugLog(`fail to parse component ${args[0].type} inside callExpression`);
return jsxExpressionContainer(node);
}
let props: JSXAttribute[] = [];
if (isObjectExpression(args[1])) {
props = args[1].properties.map((prop) => {
if (!isObjectProperty(prop) || !isIdentifier(prop.key)) return null;
if (isStringLiteral(prop.value)) {
return jsxAttribute(jsxIdentifier(prop.key.name), prop.value);
}
if (isBooleanLiteral(prop.value) && prop.value.value) {
return jsxAttribute(jsxIdentifier(prop.key.name), null);
}
if (isExpression(prop.value)) {
return jsxAttribute(jsxIdentifier(prop.key.name), jsxExpressionContainer(prop.value));
}
return null;
}).filter((e): e is JSXAttribute => e != null);
}
const children = args.slice(2).map((e) => (isExpression(e) ? this.parseJsx(e) : null)).filter((e): e is JSXElement => e != null);
if (children.length) {
return jsxElement(jsxOpeningElement(name, props), jsxClosingElement(name), children);
}
return jsxElement(jsxOpeningElement(name, props, true), null, []);
}
this.debugLog(`fail to parse component ${node.type}`);
return jsxExpressionContainer(node);
}
Example #8
Source File: jsxConverter.ts From react-native-decompiler with GNU Affero General Public License v3.0 | 5 votes |
private parseJsx(node: Expression): JSXText | JSXExpressionContainer | JSXSpreadChild | JSXElement | JSXFragment {
if (isStringLiteral(node)) {
return jsxText(node.value);
}
if (isCallExpression(node)) {
const args = node.arguments;
let name: JSXIdentifier | JSXMemberExpression | undefined;
if (isIdentifier(args[0]) || isStringLiteral(args[0])) {
name = jsxIdentifier(isIdentifier(args[0]) ? args[0].name : args[0].value);
} else if (isMemberExpression(args[0]) && isIdentifier(args[0].object) && isIdentifier(args[0].property)) {
name = jsxMemberExpression(jsxIdentifier(args[0].object.name), jsxIdentifier(args[0].property.name));
} else {
this.debugLog(`fail to parse component ${args[0].type} inside callExpression`);
return jsxExpressionContainer(node);
}
let props: JSXAttribute[] = [];
if (isObjectExpression(args[1])) {
props = args[1].properties.map((prop) => {
if (!isObjectProperty(prop) || !isIdentifier(prop.key)) return null;
if (isStringLiteral(prop.value)) {
return jsxAttribute(jsxIdentifier(prop.key.name), prop.value);
}
if (isBooleanLiteral(prop.value) && prop.value.value) {
return jsxAttribute(jsxIdentifier(prop.key.name), null);
}
if (isExpression(prop.value)) {
return jsxAttribute(jsxIdentifier(prop.key.name), jsxExpressionContainer(prop.value));
}
return null;
}).filter((e): e is JSXAttribute => e != null);
}
const children = args.slice(2).map((e) => (isExpression(e) ? this.parseJsx(e) : null)).filter((e): e is JSXElement => e != null);
if (children.length) {
return jsxElement(jsxOpeningElement(name, props), jsxClosingElement(name), children);
}
return jsxElement(jsxOpeningElement(name, props, true), null, []);
}
this.debugLog(`fail to parse component ${node.type}`);
return jsxExpressionContainer(node);
}
Example #9
Source File: visitor.ts From react-dev-inspector with MIT License | 5 votes |
doJSXIdentifierName: NodeHandler<JSXIdentifier> = (name) => {
if (name.name.endsWith('Fragment')) {
return { stop: true }
}
return { stop: false }
}