@babel/types#importSpecifier TypeScript Examples
The following examples show how to use
@babel/types#importSpecifier.
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: plugin.ts From vite-react-jsx with MIT License | 6 votes |
/**
* Replace this:
*
* import { jsx as _jsx } from "react/jsx-runtime"
*
* with this:
*
* var _jsx = require("react/jsx-runtime").jsx
*/
export function babelImportToRequire({
types: t,
}: typeof import('@babel/core')) {
return {
visitor: {
ImportDeclaration(path: NodePath) {
const decl = path.node as ImportDeclaration
const spec = decl.specifiers[0] as ImportSpecifier
path.replaceWith(
t.variableDeclaration('var', [
t.variableDeclarator(
spec.local,
t.memberExpression(
t.callExpression(t.identifier('require'), [decl.source]),
spec.imported
)
),
])
)
},
},
}
}
Example #2
Source File: babel-polyfill.ts From nota with MIT License | 6 votes |
importDeclaration = (
specifiers: Array<ImportSpecifier | ImportDefaultSpecifier | ImportNamespaceSpecifier>,
source: StringLiteral
): ImportDeclaration => ({
type: "ImportDeclaration",
specifiers,
source,
...baseNode,
})
Example #3
Source File: babel-polyfill.ts From nota with MIT License | 6 votes |
importSpecifier = (
local: Identifier,
imported: Identifier | StringLiteral
): ImportSpecifier => ({
type: "ImportSpecifier",
local,
imported,
...baseNode,
})
Example #4
Source File: get-sorted-nodes-modules-names.ts From prettier-plugin-sort-imports with Apache License 2.0 | 6 votes |
getSortedNodesModulesNames = (
modules: (
| ImportSpecifier
| ImportDefaultSpecifier
| ImportNamespaceSpecifier
)[],
) =>
modules
.filter((m) =>
[
'ImportSpecifier',
'ImportDefaultSpecifier',
'ImportNamespaceSpecifier',
].includes(m.type),
)
.map((m) => m.local.name)
Example #5
Source File: addPathImport.ts From engine with MIT License | 6 votes |
addPathImport: AddPathImport = (babel, state, ref) => {
const producerName = "@c11/engine.producer";
const pathImport = importDeclaration(
[importSpecifier(identifier("path"), identifier("path"))],
stringLiteral(producerName)
);
const program = ref.findParent((p) => p.isProgram());
if (!program) {
throw new Error("Internal error. Cannot find program node");
}
const macroImport = program.get("body").find((p) => {
const result =
p.isImportDeclaration() &&
p.node.source.value.indexOf("@c11/engine.macro") !== -1;
return result;
});
if (macroImport) {
// @ts-ignore
macroImport.insertAfter(pathImport);
}
}
Example #6
Source File: addWildcardImport.ts From engine with MIT License | 6 votes |
addWildcardImport: AddWildcardImport = (babel, state, ref) => {
const producerName = "@c11/engine.producer";
const pathImport = importDeclaration(
[importSpecifier(identifier("wildcard"), identifier("wildcard"))],
stringLiteral(producerName)
);
const program = ref.findParent((p) => p.isProgram());
if (!program) {
throw new Error("");
}
const macroImport = program.get("body").find((p) => {
const result =
p.isImportDeclaration() &&
p.node.source.value.indexOf("@c11/engine.macro") !== -1;
return result;
});
if (macroImport) {
// @ts-ignore
macroImport.insertAfter(pathImport);
}
}
Example #7
Source File: resolveJsxComponent.ts From react-optimized-image with MIT License | 5 votes |
resolveImport = (binding: Binding | undefined): { exportName?: string; moduleName?: string } | undefined => {
// handle import statements
if (
binding &&
binding.kind === 'module' &&
isImport(binding.path) &&
binding.path.parent.type === 'ImportDeclaration'
) {
return {
moduleName: binding.path.parent.source.value,
exportName: getExportName(binding.path.node as ImportSpecifier | ImportDefaultSpecifier),
};
}
// handle require statements and other libraries like styled-components
if (binding && binding.kind !== 'module' && binding.path.node.type === 'VariableDeclarator') {
const { node } = binding.path;
// check for require('react-optimized-image').default calls
if (node.init && node.init.type === 'MemberExpression' && node.init.object.type === 'CallExpression') {
return {
moduleName: resolveRequireModule(node.init.object),
exportName: resolveRequireExportName(node, binding),
};
}
// check for `const { Svg } = require('react-optimized-image')` or `styled(Img)({}) calls
if (node.init && node.init.type === 'CallExpression') {
// handle styled-components
if (node.init.callee.type === 'CallExpression' && node.init.callee.callee.type === 'Identifier') {
return resolveStyledComponentsImport(node.init.callee, binding);
}
// handle transpiled styled-components
if (
node.init.callee.type === 'CallExpression' &&
node.init.callee.callee.type === 'MemberExpression' &&
node.init.callee.callee.object.type === 'CallExpression'
) {
return resolveStyledComponentsImport(node.init.callee.callee.object, binding);
}
return {
moduleName: resolveRequireModule(node.init),
exportName: resolveRequireExportName(node, binding),
};
}
// handle styled-components (styled(Img)`...`)
if (
node.init &&
node.init.type === 'TaggedTemplateExpression' &&
node.init.tag.type === 'CallExpression' &&
node.init.tag.callee.type === 'Identifier'
) {
return resolveStyledComponentsImport(node.init.tag, binding);
}
// handle recursiveness
if (node.init && node.init.type === 'Identifier') {
return resolveImport(binding.scope.getBinding(node.init.name));
}
}
return undefined;
}
Example #8
Source File: traverse.ts From react-optimized-image with MIT License | 5 votes |
getExportName = (node: ImportSpecifier | ImportDefaultSpecifier): string => {
if (node.type === 'ImportDefaultSpecifier') {
return 'default';
}
return node.imported.name;
}
Example #9
Source File: prepareForEngine.ts From engine with MIT License | 5 votes |
prepareForEngine: PrepareForEngine = (babel, state, ref, type) => {
const validation = validateRef(ref);
if (validation.error) {
throw new Error(validation.errorMessage);
}
const config = getConfig(state);
const op = parseRef(babel, state, ref);
const props = structOperationCompiler(op);
const parent = ref.findParent((p) => p.isVariableDeclarator());
if (!parent) {
throw new Error(
"Misuse of the view/producer keyword. It needs to be a variable declaration e.g. let foo: view = ..."
);
}
const node = parent.node as VariableDeclarator;
const fn = node.init as ArrowFunctionExpression;
fn.params = paramsCompiler(op);
const result = objectExpression([
objectProperty(identifier("props"), props),
objectProperty(identifier("fn"), fn),
]);
if (type === TransformType.PRODUCER) {
node.init = result;
} else if (type === TransformType.VIEW) {
const viewCall = callExpression(identifier("view"), [result]);
node.init = viewCall;
const viewImport = config.view.importFrom;
const program = ref.findParent((p) => p.isProgram());
if (!program) {
throw new Error("Internal error. Cannot find program node");
}
const macroImport = program.get("body").find((p) => {
const result =
p.isImportDeclaration() &&
p.node.source.value.indexOf("@c11/engine.macro") !== -1;
return result;
});
const engineImport = program.get("body").find((p) => {
const result =
p.isImportDeclaration() &&
p.node.source.value.indexOf(viewImport) !== -1;
return result;
});
if (macroImport) {
if (!engineImport) {
const importView = importDeclaration(
[importSpecifier(identifier("view"), identifier("view"))],
stringLiteral(viewImport)
);
// @ts-ignore
macroImport.insertAfter(importView);
} else {
const node = engineImport.node as ImportDeclaration;
const viewNode = node.specifiers.find((node) => {
return (
isImportSpecifier(node) &&
isIdentifier(node.imported) &&
node.imported.name === "view"
);
});
if (!viewNode) {
node.specifiers.push(
importSpecifier(identifier("view"), identifier("view"))
);
}
}
} else {
throw new Error("Could not find macro import");
}
}
}