@babel/types#isJSXMemberExpression TypeScript Examples
The following examples show how to use
@babel/types#isJSXMemberExpression.
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 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 #2
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);
}