Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
// import generate from "@babel/generator";
// console.log(template,t)
// const buildRequire = template(`
// var %%importName%% = require(%%source%%);
// `);
// const ast = buildRequire({
// importName: t.identifier("myModule"),
// source: t.stringLiteral("my-module"),
// });
// https://astexplorer.net/
const source = "my-module";
const ast = template.ast(`
// JS
var a = 'abcd'
`);
ast.kind = 'let'
console.log(ast);
console.log(generate(ast).code);
case PropTypes.Enum:
return t.identifier(String(value));
case PropTypes.Date:
return t.newExpression(
t.identifier('Date'),
value ? [t.stringLiteral(String(value))] : []
);
case PropTypes.Ref:
return null;
case PropTypes.Object:
return template.ast(`${value}`, {plugins: ['jsx']}) as any;
case PropTypes.Array:
case PropTypes.Number:
case PropTypes.Function:
case PropTypes.ReactNode:
const output = (template.ast(String(value), {plugins: ['jsx']}) as any)
.expression;
// we never expect that user would input a variable as the value
// treat it as a string instead
if (output.type === 'Identifier') {
return t.stringLiteral(output.name);
}
return output;
case PropTypes.Custom:
if (!customProps[name] || !customProps[name].generate) {
console.error(`Missing customProps.${name}.generate definition.`);
}
return customProps[name].generate(value);
}
};
function buildExport({exportName, namespace, propName, localName}) {
const exportDefault = exportName === 'default';
const head = exportDefault ? 'module.exports' : `exports.${exportName}`;
let opt = {};
// FIXME
// Does `PRIORITY`, `LOCATION_PARAMS` recognised as babel-template placeholder?
// We have to do this for workaround temporarily.
if (/^[A-Z0-9_]+$/.test(localName)) {
opt[localName] = localName;
}
return babelTemplate.statement(
localName
? `${head} = ${localName};`
: `${head} = ${namespace}.${propName};`
)(opt);
}
// @flow
import semver from 'semver';
import path from 'path';
import {Transformer} from '@parcel/plugin';
import {relativeUrl} from '@parcel/utils';
import SourceMap from '@parcel/source-map';
import generate from '@babel/generator';
import {parse} from '@babel/parser';
import template from '@babel/template';
import * as t from '@babel/types';
const WRAPPER = path.join(__dirname, 'helpers', 'helpers.js');
const wrapper = template(`
var helpers = require(%%helper%%);
var prevRefreshReg = window.$RefreshReg$;
var prevRefreshSig = window.$RefreshSig$;
helpers.prelude(module);
try {
%%module%%
helpers.postlude(module);
} finally {
window.$RefreshReg$ = prevRefreshReg;
window.$RefreshSig$ = prevRefreshSig;
}
`);
function shouldExclude(asset, options) {
return (
const {relative} = require('path');
const template = require('@babel/template').default;
const t = require('@babel/types');
const traverse = require('@babel/traverse').default;
const generate = require('@babel/generator').default;
const treeShake = require('./shake');
const mangleScope = require('./mangler');
const {getName, getIdentifier} = require('./utils');
const EXPORTS_RE = /^\$([^$]+)\$exports$/;
const DEFAULT_INTEROP_TEMPLATE = template(
'var NAME = $parcel$interopDefault(MODULE)'
);
const THROW_TEMPLATE = template('$parcel$missingModule(MODULE)');
const REQUIRE_TEMPLATE = template('require(ID)');
module.exports = (packager, ast) => {
let {assets} = packager;
let replacements = new Map();
let imports = new Map();
let referenced = new Set();
// Build a mapping of all imported identifiers to replace.
for (let asset of assets.values()) {
for (let name in asset.cacheData.imports) {
let imp = asset.cacheData.imports[name];
imports.set(name, [packager.resolveModule(asset.id, imp[0]), imp[1]]);
const {relative} = require('path');
const template = require('@babel/template').default;
const t = require('@babel/types');
const traverse = require('@babel/traverse').default;
const generate = require('@babel/generator').default;
const treeShake = require('./shake');
const mangleScope = require('./mangler');
const {getName, getIdentifier} = require('./utils');
const EXPORTS_RE = /^\$([^$]+)\$exports$/;
const DEFAULT_INTEROP_TEMPLATE = template(
'var NAME = $parcel$interopDefault(MODULE)'
);
const THROW_TEMPLATE = template('$parcel$missingModule(MODULE)');
const REQUIRE_TEMPLATE = template('require(ID)');
module.exports = (packager, ast) => {
let {assets} = packager;
let replacements = new Map();
let imports = new Map();
let referenced = new Set();
// Build a mapping of all imported identifiers to replace.
for (let asset of assets.values()) {
for (let name in asset.cacheData.imports) {
let imp = asset.cacheData.imports[name];
imports.set(name, [packager.resolveModule(asset.id, imp[0]), imp[1]]);
}
}
function replaceExportNode(module, originalName, path) {
sourceType: 'module',
}
);
const fileChunkIdTemplate = template(`const CHUNKIDS_ID = LOCAL_ID(FILENAME);`);
// TODO(#4): There might be some issues related to npm module tree resolution here.
// How can we ensure that the fusion-experimentation instance imported here and elsewhere are the same?
const singletonImport = template(
`
import { nodeSingleton as LOCAL_SINGLETON } from 'fusion-experimentation';
`,
{sourceType: 'module'}
);
const builder = template(
`
LOCAL_SINGLETON.add(FILENAME, CHUNKIDS_ID, EXPERIMENTS);
if (module.hot) {
module.hot.dispose(() => {
LOCAL_SINGLETON.dispose(FILENAME, CHUNKIDS_ID, EXPERIMENTS);
});
}
`,
{sourceType: 'module'}
);
/**
* Appends a node to the program body and returns the path of the inserted node
*/
export const validateAndFormatMsgid = (msgid, exprNames) => {
const msgidAST = tpl.ast(strToQuasi(msgid));
const msgidExprs = new Set(msgidAST.expression.expressions.map(ast2Str));
exprNames.forEach((exprName) => {
if (!msgidExprs.has(exprName)) {
throw new NoExpressionError(`Expression '${exprName}' is not found in the localized string '${msgid}'.`);
}
});
// need to regenerate template to fix spaces between in ${}
// because translator can accidentally add extra space or remove
return generate(msgidAST).code.replace(/;$/, '');
};
import * as t from '@babel/types';
// forked prettier on a diet
//@ts-ignore
import prettier from '@miksu/prettier/lib/standalone';
//@ts-ignore
import parsers from '@miksu/prettier/lib/language-js/parser-babylon';
type TJsxChild =
| t.JSXText
| t.JSXExpressionContainer
| t.JSXSpreadChild
| t.JSXElement
| t.JSXFragment;
const reactImport = template.ast(`import * as React from 'react';`);
export const getAstPropsArray = (props: {[key: string]: TProp}) => {
return Object.entries(props).map(([name, prop]) => {
const {value, stateful, defaultValue} = prop;
if (stateful)
return t.jsxAttribute(
t.jsxIdentifier(name),
t.jsxExpressionContainer(t.identifier(name)),
);
// When the `defaultValue` is set and `value` is the same as the `defaultValue`
// we don't add it to the list of props.
// It handles boolean props where `defaultValue` set to true,
// and enum props that have a `defaultValue` set to be displayed
// in the yard correctly (checked checkboxes and selected default value in radio groups)
// and not rendered in the component's props.
if (
// @flow
import gqlTag from 'graphql-tag';
import serialize from 'babel-literal-to-ast';
import template from '@babel/template';
/**
* TODO: Reduce runtime to improve performance
* ref: https://github.com/gajus/babel-plugin-graphql-tag/blob/35edbae44990bf20be2de7139dc0ce5843f70bff/src/index.js#L25
*/
const uniqueFn = template.ast(`
(acc, definition) =>
definition.kind === 'FragmentDefinition' &&
acc.find(
curDef =>
curDef.kind === 'FragmentDefinition' &&
curDef.name.value === definition.name.value,
)
? acc
: acc.concat(definition)
`);
/**
* ref: https://github.com/leoasis/graphql-tag.macro
*/
export default function compileWithFragment(
referencePath: Object,