@linaria/utils
Advanced tools
Comparing version 4.2.4 to 4.2.5
import path from 'path'; | ||
const safeResolve = (name, where) => { | ||
@@ -12,3 +11,2 @@ try { | ||
}; | ||
const suffixes = ['.js', '.ts', '.jsx', '.tsx'].reduce((acc, ext) => { | ||
@@ -22,11 +20,9 @@ acc.push(`/index${ext}`); | ||
const resolved = safeResolve(what, where); | ||
if (!(resolved instanceof Error)) { | ||
return resolved; | ||
} // eslint-disable-next-line no-restricted-syntax | ||
} | ||
// eslint-disable-next-line no-restricted-syntax | ||
for (const suffix of suffixes) { | ||
const resolvedWithSuffix = safeResolve(what + suffix, where); | ||
if (resolvedWithSuffix instanceof Error) { | ||
@@ -36,21 +32,16 @@ // eslint-disable-next-line no-continue | ||
} | ||
return resolvedWithSuffix; | ||
} | ||
throw resolved; | ||
}; | ||
const asyncResolve = (what, importer, stack) => { | ||
const where = [importer, ...stack].map(p => path.dirname(p)); | ||
const resolved = safeResolve(what, where); | ||
if (!(resolved instanceof Error)) { | ||
return Promise.resolve(resolved); | ||
} // eslint-disable-next-line no-restricted-syntax | ||
} | ||
// eslint-disable-next-line no-restricted-syntax | ||
for (const suffix of suffixes) { | ||
const resolvedWithSuffix = safeResolve(what + suffix, where); | ||
if (resolvedWithSuffix instanceof Error) { | ||
@@ -60,10 +51,7 @@ // eslint-disable-next-line no-continue | ||
} | ||
return Promise.resolve(resolvedWithSuffix); | ||
} | ||
throw resolved; | ||
}; | ||
export default asyncResolve; | ||
//# sourceMappingURL=asyncResolveFallback.js.map |
/* eslint @typescript-eslint/no-use-before-define: ["error", { "functions": false }] */ | ||
/* eslint-disable no-restricted-syntax,no-continue */ | ||
/* eslint-disable no-restricted-syntax,no-continue */ | ||
import { warn } from '@linaria/logger'; | ||
@@ -12,3 +12,2 @@ import { getScope } from './getScope'; | ||
export const explicitImport = item => item.imported !== null; | ||
function getValue({ | ||
@@ -18,8 +17,9 @@ node | ||
return node.type === 'Identifier' ? node.name : node.value; | ||
} // We ignore imports and exports of types | ||
} | ||
// We ignore imports and exports of types | ||
const isType = p => 'importKind' in p.node && p.node.importKind === 'type' || 'exportKind' in p.node && p.node.exportKind === 'type'; | ||
const isType = p => 'importKind' in p.node && p.node.importKind === 'type' || 'exportKind' in p.node && p.node.exportKind === 'type'; // Force TypeScript to check, that we have implementation for every possible specifier | ||
// Force TypeScript to check, that we have implementation for every possible specifier | ||
const collectors = { | ||
@@ -36,3 +36,2 @@ ImportSpecifier(path, source) { | ||
}, | ||
ImportDefaultSpecifier(path, source) { | ||
@@ -46,3 +45,2 @@ const local = path.get('local'); | ||
}, | ||
ImportNamespaceSpecifier(path, source) { | ||
@@ -56,5 +54,3 @@ const local = path.get('local'); | ||
} | ||
}; | ||
function collectFromImportDeclaration(path, state) { | ||
@@ -65,3 +61,2 @@ // If importKind is specified, and it's not a value, ignore that import | ||
const specifiers = path.get('specifiers'); | ||
if (specifiers.length === 0) { | ||
@@ -74,3 +69,2 @@ state.imports.push({ | ||
} | ||
specifiers.forEach(specifier => { | ||
@@ -82,7 +76,5 @@ if (specifier.isImportSpecifier() && isType(specifier)) return; | ||
} | ||
function getAncestorsWhile(path, cond) { | ||
const result = []; | ||
let current = path; | ||
while (current && cond(current)) { | ||
@@ -92,6 +84,4 @@ result.push(current); | ||
} | ||
return result; | ||
} | ||
function whatIsDestructed(objectPattern) { | ||
@@ -103,3 +93,2 @@ const destructedProps = []; | ||
const parent = identifier.parentPath; | ||
if (parent.isObjectProperty({ | ||
@@ -110,3 +99,2 @@ value: identifier.node | ||
const key = p.get('key'); | ||
if (!key.isIdentifier()) { | ||
@@ -116,7 +104,5 @@ // TODO: try to process other type of keys or at least warn about this | ||
} | ||
return key; | ||
}).filter(isNotNull); | ||
chain.reverse(); | ||
if (chain.length > 0) { | ||
@@ -128,6 +114,4 @@ destructedProps.push({ | ||
} | ||
return; | ||
} | ||
if (parent.isRestElement({ | ||
@@ -143,10 +127,7 @@ argument: identifier.node | ||
} | ||
}); | ||
return destructedProps; | ||
} | ||
function importFromVariableDeclarator(path, isSync) { | ||
const id = path.get('id'); | ||
if (id.isIdentifier()) { | ||
@@ -159,3 +140,2 @@ // It's the simplest case when the full namespace is imported | ||
} | ||
if (!isSync) { | ||
@@ -167,18 +147,16 @@ // Something went wrong | ||
} | ||
if (id.isObjectPattern()) { | ||
return whatIsDestructed(id); | ||
} // What else it can be? | ||
} | ||
// What else it can be? | ||
warn('evaluator:collectExportsAndImports:importFromVariableDeclarator', 'Unknown type of id', id.node.type); | ||
return []; | ||
} | ||
function exportFromVariableDeclarator(path) { | ||
const id = path.get('id'); | ||
const init = path.get('init'); // If there is no init expression, we can ignore this export | ||
const init = path.get('init'); | ||
// If there is no init expression, we can ignore this export | ||
if (!init || !init.isExpression()) return []; | ||
if (id.isIdentifier()) { | ||
@@ -191,3 +169,2 @@ // It is `export const a = 1;` | ||
} | ||
if (id.isObjectPattern()) { | ||
@@ -199,9 +176,8 @@ // It is `export const { a, ...rest } = obj;` | ||
})); | ||
} // What else it can be? | ||
} | ||
// What else it can be? | ||
warn('evaluator:collectExportsAndImports:exportFromVariableDeclarator', 'Unknown type of id', id.node.type); | ||
return []; | ||
} | ||
function collectFromDynamicImport(path, state) { | ||
@@ -211,3 +187,2 @@ const { | ||
} = path; | ||
if (!callExpression.isCallExpression()) { | ||
@@ -217,5 +192,3 @@ // It's wrong `import` | ||
} | ||
const [sourcePath] = callExpression.get('arguments'); | ||
if (!sourcePath || !sourcePath.isStringLiteral()) { | ||
@@ -225,3 +198,2 @@ // Import should have at least one argument, and it should be StringLiteral | ||
} | ||
const source = sourcePath.node.value; | ||
@@ -233,3 +205,2 @@ let { | ||
let isAwaited = false; | ||
if (container.isAwaitExpression()) { | ||
@@ -240,5 +211,5 @@ // If it's not awaited import, it imports the full namespace | ||
container = container.parentPath; | ||
} // Is it `const something = await import("something")`? | ||
} | ||
// Is it `const something = await import("something")`? | ||
if (key === 'init' && container.isVariableDeclarator()) { | ||
@@ -252,29 +223,21 @@ importFromVariableDeclarator(container, isAwaited).map(prop => state.imports.push({ | ||
} | ||
function getImportTypeByInteropFunction(path) { | ||
const callee = path.get('callee'); | ||
if (!callee.isIdentifier()) { | ||
return undefined; | ||
} | ||
const { | ||
name | ||
} = callee.node; | ||
if (name.startsWith('_interopRequireDefault') || name.startsWith('__importDefault')) { | ||
return 'default'; | ||
} | ||
if (name.startsWith('_interopRequireWildcard') || name.startsWith('__importStar')) { | ||
return '*'; | ||
} | ||
if (name.startsWith('__rest')) { | ||
if (name.startsWith('__rest') || name.startsWith('_objectDestructuringEmpty')) { | ||
return '*'; | ||
} | ||
return undefined; | ||
} | ||
function collectFromRequire(path, state) { | ||
@@ -285,3 +248,2 @@ if (!isRequire(path)) return; | ||
} = path; | ||
if (!callExpression.isCallExpression()) { | ||
@@ -291,5 +253,3 @@ // It's wrong `require` | ||
} | ||
const [sourcePath] = callExpression.get('arguments'); | ||
if (!sourcePath || !sourcePath.isStringLiteral()) { | ||
@@ -299,3 +259,2 @@ // Import should have at least one argument, and it should be StringLiteral | ||
} | ||
const source = sourcePath.node.value; | ||
@@ -306,3 +265,2 @@ const { | ||
} = callExpression; | ||
if (container.isCallExpression() && key === 0) { | ||
@@ -312,3 +270,2 @@ // It may be transpiled import such as | ||
const imported = getImportTypeByInteropFunction(container); | ||
if (!imported) { | ||
@@ -320,7 +277,12 @@ // It's not a transpiled import. | ||
} | ||
const { | ||
let { | ||
parentPath: variableDeclarator | ||
} = container; | ||
if (variableDeclarator.isCallExpression()) { | ||
if (variableDeclarator.get('callee').isIdentifier({ | ||
name: '_extends' | ||
})) { | ||
variableDeclarator = variableDeclarator.parentPath; | ||
} | ||
} | ||
if (!variableDeclarator.isVariableDeclarator()) { | ||
@@ -331,5 +293,3 @@ // TODO: Where else it can be? | ||
} | ||
const id = variableDeclarator.get('id'); | ||
if (!id.isIdentifier()) { | ||
@@ -339,3 +299,2 @@ warn('evaluator:collectExportsAndImports', 'Id should be Identifier', variableDeclarator.node.type); | ||
} | ||
if (imported === '*') { | ||
@@ -356,7 +315,5 @@ const unfolded = unfoldNamespaceImport({ | ||
} | ||
if (container.isMemberExpression()) { | ||
// It is `require('@linaria/shaker').dep` | ||
const property = container.get('property'); | ||
if (!property.isIdentifier() && !property.isStringLiteral()) { | ||
@@ -366,11 +323,8 @@ warn('evaluator:collectExportsAndImports', 'Property should be Identifier or StringLiteral', property.node.type); | ||
} | ||
const { | ||
parentPath: variableDeclarator | ||
} = container; | ||
if (variableDeclarator.isVariableDeclarator()) { | ||
// It is `const … = require('@linaria/shaker').dep`; | ||
const id = variableDeclarator.get('id'); | ||
if (id.isIdentifier()) { | ||
@@ -394,7 +348,6 @@ state.imports.push({ | ||
} | ||
return; | ||
} // Is it `const something = require("something")`? | ||
} | ||
// Is it `const something = require("something")`? | ||
if (key === 'init' && container.isVariableDeclarator()) { | ||
@@ -418,3 +371,2 @@ importFromVariableDeclarator(container, true).forEach(prop => { | ||
} | ||
if (container.isExpressionStatement()) { | ||
@@ -429,6 +381,4 @@ // Looks like standalone require | ||
} | ||
function isChainOfVoidAssignment(path) { | ||
const right = path.get('right'); | ||
if (right.isUnaryExpression({ | ||
@@ -439,10 +389,22 @@ operator: 'void' | ||
} | ||
if (right.isAssignmentExpression()) { | ||
return isChainOfVoidAssignment(right); | ||
} | ||
return false; | ||
} | ||
function getReturnValue(path) { | ||
if (path.node.params.length !== 0) return undefined; | ||
const body = path.get('body'); | ||
if (body.isExpression()) { | ||
return body; | ||
} | ||
if (body.node.body.length === 1) { | ||
const returnStatement = body.get('body')?.[0]; | ||
if (!returnStatement.isReturnStatement()) return undefined; | ||
const argument = returnStatement.get('argument'); | ||
if (!argument.isExpression()) return undefined; | ||
return argument; | ||
} | ||
return undefined; | ||
} | ||
function getGetterValueFromDescriptor(descriptor) { | ||
@@ -453,21 +415,9 @@ const getter = descriptor.get('properties').filter(isTypedNode('ObjectProperty')).find(p => p.get('key').isIdentifier({ | ||
const value = getter?.get('value'); | ||
if (value?.isFunctionExpression()) { | ||
const returnStatement = value.get('body').get('body')[0]; | ||
if (returnStatement?.isReturnStatement()) { | ||
const local = returnStatement.get('argument'); | ||
if (local.isExpression()) { | ||
return local; | ||
} | ||
} | ||
if (value?.isFunctionExpression() || value?.isArrowFunctionExpression()) { | ||
return getReturnValue(value); | ||
} | ||
return undefined; | ||
} | ||
function collectFromExports(path, state) { | ||
if (!isExports(path)) return; | ||
if (path.parentPath.isMemberExpression({ | ||
@@ -479,9 +429,6 @@ object: path.node | ||
const property = memberExpression.get('property'); | ||
if (!property.isIdentifier()) { | ||
return; | ||
} | ||
const exportName = property.node.name; | ||
const saveRef = () => { | ||
@@ -492,8 +439,5 @@ // Save all export.____ usages for later | ||
} | ||
state.exportRefs.get(exportName).push(memberExpression); | ||
}; | ||
const assignmentExpression = memberExpression.parentPath; | ||
if (!assignmentExpression.isAssignmentExpression({ | ||
@@ -506,5 +450,3 @@ left: memberExpression.node | ||
} | ||
const right = assignmentExpression.get('right'); | ||
if (isChainOfVoidAssignment(assignmentExpression)) { | ||
@@ -514,11 +456,8 @@ // It is `exports.foo = void 0` | ||
} | ||
const { | ||
name | ||
} = property.node; | ||
if (name === '__esModule') { | ||
return; | ||
} | ||
saveRef(); | ||
@@ -531,6 +470,4 @@ state.exports.push({ | ||
} | ||
if (path.parentPath.isCallExpression() && path.parentPath.get('callee').matchesPattern('Object.defineProperty')) { | ||
const [obj, prop, descriptor] = path.parentPath.get('arguments'); | ||
if (obj?.isIdentifier(path.node) && prop?.isStringLiteral() && prop.node.value !== '__esModule' && descriptor?.isObjectExpression()) { | ||
@@ -547,3 +484,2 @@ /** | ||
const local = getGetterValueFromDescriptor(descriptor); | ||
if (local) { | ||
@@ -565,3 +501,2 @@ state.exports.push({ | ||
const local = getGetterValueFromDescriptor(descriptor); | ||
if (local) { | ||
@@ -576,3 +511,2 @@ state.exports.push({ | ||
} | ||
function collectFromRequireOrExports(path, state) { | ||
@@ -585,3 +519,2 @@ if (isRequire(path)) { | ||
} | ||
function unfoldNamespaceImport(importItem) { | ||
@@ -592,3 +525,2 @@ const result = []; | ||
} = importItem; | ||
if (!local.isIdentifier()) { | ||
@@ -598,5 +530,3 @@ // TODO: handle it | ||
} | ||
const binding = getScope(local).getBinding(local.node.name); | ||
if (!binding?.referenced) { | ||
@@ -607,3 +537,2 @@ // Imported namespace is not referenced and probably not used, | ||
} | ||
for (const referencePath of binding?.referencePaths ?? []) { | ||
@@ -613,7 +542,5 @@ if (referencePath.find(ancestor => ancestor.isTSType() || ancestor.isFlowType())) { | ||
} | ||
const { | ||
parentPath | ||
} = referencePath; | ||
if (parentPath?.isMemberExpression() && referencePath.key === 'object') { | ||
@@ -623,3 +550,2 @@ const property = parentPath.get('property'); | ||
let imported; | ||
if (parentPath.node.computed && property.isStringLiteral()) { | ||
@@ -632,5 +558,5 @@ imported = property.node.value; | ||
} | ||
if (object.isIdentifier() && imported) { | ||
result.push({ ...importItem, | ||
result.push({ | ||
...importItem, | ||
imported, | ||
@@ -643,8 +569,7 @@ local: parentPath | ||
} | ||
continue; | ||
} | ||
if (parentPath?.isVariableDeclarator() && referencePath.key === 'init') { | ||
importFromVariableDeclarator(parentPath, true).map(prop => result.push({ ...importItem, | ||
importFromVariableDeclarator(parentPath, true).map(prop => result.push({ | ||
...importItem, | ||
imported: prop.what, | ||
@@ -655,3 +580,2 @@ local: prop.as | ||
} | ||
if (parentPath?.isExportSpecifier()) { | ||
@@ -661,6 +585,6 @@ // The whole namespace is re-exported | ||
break; | ||
} // Otherwise, we can't predict usage and import it as is | ||
} | ||
// Otherwise, we can't predict usage and import it as is | ||
// TODO: handle more cases | ||
warn('evaluator:collectExportsAndImports:unfoldNamespaceImports', 'Unknown reference', referencePath.node.type); | ||
@@ -670,11 +594,10 @@ result.push(importItem); | ||
} | ||
return result; | ||
} | ||
function collectFromExportAllDeclaration(path, state) { | ||
if (isType(path)) return; | ||
const source = path.get('source')?.node?.value; | ||
if (!source) return; // It is `export * from './css';` | ||
if (!source) return; | ||
// It is `export * from './css';` | ||
state.reexports.push({ | ||
@@ -687,7 +610,5 @@ exported: '*', | ||
} | ||
function collectFromExportSpecifier(path, source, state) { | ||
if (path.isExportSpecifier()) { | ||
const exported = getValue(path.get('exported')); | ||
if (source) { | ||
@@ -709,6 +630,4 @@ // It is `export { foo } from './css';` | ||
} | ||
return; | ||
} | ||
if (path.isExportDefaultSpecifier() && source) { | ||
@@ -723,6 +642,5 @@ // It is `export default from './css';` | ||
} | ||
if (path.isExportNamespaceSpecifier() && source) { | ||
const exported = path.get('exported').node.name; // It is `export * as foo from './css';` | ||
const exported = path.get('exported').node.name; | ||
// It is `export * as foo from './css';` | ||
state.reexports.push({ | ||
@@ -734,8 +652,7 @@ exported, | ||
}); | ||
} // TODO: handle other cases | ||
} | ||
// TODO: handle other cases | ||
warn('evaluator:collectExportsAndImports:collectFromExportSpecifier', 'Unprocessed ExportSpecifier', path.node.type); | ||
} | ||
function collectFromExportNamedDeclaration(path, state) { | ||
@@ -745,9 +662,6 @@ if (isType(path)) return; | ||
const specifiers = path.get('specifiers'); | ||
if (specifiers) { | ||
specifiers.forEach(specifier => collectFromExportSpecifier(specifier, source, state)); | ||
} | ||
const declaration = path.get('declaration'); | ||
if (declaration.isVariableDeclaration()) { | ||
@@ -761,6 +675,4 @@ declaration.get('declarations').forEach(declarator => { | ||
} | ||
if (declaration.isFunctionDeclaration()) { | ||
const id = declaration.get('id'); | ||
if (id.isIdentifier()) { | ||
@@ -774,3 +686,2 @@ state.exports.push({ | ||
} | ||
function collectFromExportDefaultDeclaration(path, state) { | ||
@@ -784,5 +695,3 @@ if (isType(path)) return; | ||
} | ||
const cache = new WeakMap(); | ||
function collectFromAssignmentExpression(path, state) { | ||
@@ -792,6 +701,4 @@ const left = path.get('left'); | ||
let exported; | ||
if (left.isMemberExpression() && isExports(left.get('object'))) { | ||
const property = left.get('property'); | ||
if (property.isIdentifier()) { | ||
@@ -808,4 +715,6 @@ exported = property.node.name; | ||
const source = sourcePath.isStringLiteral() ? sourcePath.node.value : undefined; | ||
if (!source) return; // It is `exports.foo = require('./css');` | ||
if (!source) return; | ||
// It is `exports.foo = require('./css');` | ||
state.reexports.push({ | ||
@@ -819,10 +728,3 @@ exported, | ||
} | ||
function collectFromCallExpression(path, state) { | ||
const maybeExportStart = path.get('callee'); | ||
if (!maybeExportStart.isIdentifier() || !maybeExportStart.node.name.startsWith('__exportStar')) { | ||
return; | ||
} | ||
function collectFromExportStarCall(path, state) { | ||
const [requireCall, exports] = path.get('arguments'); | ||
@@ -844,3 +746,49 @@ if (!isExports(exports)) return; | ||
} | ||
function collectFromExportCall(path, state) { | ||
const [exports, map] = path.get('arguments'); | ||
if (!isExports(exports)) return; | ||
if (!map.isObjectExpression()) return; | ||
const properties = map.get('properties'); | ||
properties.forEach(property => { | ||
if (!property.isObjectProperty()) return; | ||
const key = property.get('key'); | ||
const value = property.get('value'); | ||
if (!key.isIdentifier()) return; | ||
const exported = key.node.name; | ||
if (!value.isFunction()) return; | ||
if (value.node.params.length !== 0) return; | ||
const returnValue = getReturnValue(value); | ||
if (!returnValue) return; | ||
state.exports.push({ | ||
exported, | ||
local: returnValue | ||
}); | ||
}); | ||
path.skip(); | ||
} | ||
function collectFromCallExpression(path, state) { | ||
const maybeExportStart = path.get('callee'); | ||
if (!maybeExportStart.isIdentifier()) { | ||
return; | ||
} | ||
const { | ||
name | ||
} = maybeExportStart.node; | ||
// TypeScript | ||
if (name.startsWith('__exportStar')) { | ||
collectFromExportStarCall(path, state); | ||
return; | ||
} | ||
// swc | ||
if (name === '_exportStar') { | ||
collectFromExportStarCall(path, state); | ||
} | ||
// swc | ||
if (name === '_export') { | ||
collectFromExportCall(path, state); | ||
} | ||
} | ||
export default function collectExportsAndImports(path, force = false) { | ||
@@ -853,7 +801,5 @@ const state = { | ||
}; | ||
if (!force && cache.has(path)) { | ||
return cache.get(path) ?? state; | ||
} | ||
path.traverse({ | ||
@@ -860,0 +806,0 @@ AssignmentExpression: collectFromAssignmentExpression, |
import { getScope } from './getScope'; | ||
function isInVoid(path) { | ||
@@ -8,12 +7,10 @@ return path.parentPath?.isUnaryExpression({ | ||
} | ||
function isBindingIdentifier(path) { | ||
return path.isBindingIdentifier() && !isInVoid(path); | ||
} | ||
function isReferencedIdentifier(path) { | ||
return path.isReferencedIdentifier() || isInVoid(path); | ||
} // For some reasons, `isBindingIdentifier` returns true for identifiers inside `void` expressions. | ||
} | ||
// For some reasons, `isBindingIdentifier` returns true for identifiers inside `void` expressions. | ||
const checkers = { | ||
@@ -34,15 +31,13 @@ binding: ex => isBindingIdentifier(ex), | ||
} | ||
if (!nonType(path)) { | ||
// If skip in TSTypeAnnotation visitor doesn't work | ||
return; | ||
} // TODO: Is there a better way to check that it's a local variable? | ||
} | ||
// TODO: Is there a better way to check that it's a local variable? | ||
const binding = getScope(path).getBinding(path.node.name); | ||
if (!binding) { | ||
return; | ||
} | ||
if (type === 'referenced' && ex.isAncestor(binding.path)) { | ||
@@ -52,6 +47,4 @@ // This identifier is declared inside the expression. We don't need it. | ||
} | ||
identifiers.push(path); | ||
}; | ||
if (ex.isIdentifier() || ex.isJSXIdentifier()) { | ||
@@ -66,11 +59,8 @@ emit(ex); | ||
}, | ||
Identifier(path) { | ||
emit(path); | ||
}, | ||
JSXIdentifier(path) { | ||
emit(path); | ||
} | ||
}); | ||
@@ -77,0 +67,0 @@ } |
@@ -8,5 +8,4 @@ let nextIdx = 1; | ||
} | ||
return processed.get(name); | ||
} | ||
//# sourceMappingURL=getFileIdx.js.map |
import { getScope } from './getScope'; | ||
/** | ||
@@ -6,3 +7,2 @@ * Checks that specified Identifier is a global `exports` | ||
*/ | ||
export default function isExports(id) { | ||
@@ -12,3 +12,2 @@ if (!id?.isIdentifier() || id.node.name !== 'exports') { | ||
} | ||
const scope = getScope(id); | ||
@@ -15,0 +14,0 @@ return scope.getBinding('exports') === undefined && scope.hasGlobal('exports'); |
import { getScope } from './getScope'; | ||
/** | ||
@@ -6,3 +7,2 @@ * Checks that specified Identifier is a global `require` | ||
*/ | ||
export default function isRequire(id) { | ||
@@ -12,3 +12,2 @@ if (!id?.isIdentifier() || id.node.name !== 'require') { | ||
} | ||
const scope = getScope(id); | ||
@@ -15,0 +14,0 @@ return scope.getBinding('require') === undefined && scope.hasGlobal('require'); |
const isTypedNode = type => p => { | ||
return p.type === type; | ||
}; | ||
export default isTypedNode; | ||
//# sourceMappingURL=isTypedNode.js.map |
import collectExportsAndImports from './collectExportsAndImports'; | ||
import { getScope } from './getScope'; | ||
function getCallee(p) { | ||
const callee = p.get('callee'); | ||
if (callee.isSequenceExpression()) { | ||
const expressions = callee.get('expressions'); | ||
if (expressions.length === 2 && expressions[0].isNumericLiteral({ | ||
@@ -15,18 +12,12 @@ value: 0 | ||
} | ||
return callee; | ||
} | ||
return callee; | ||
} | ||
const JSXRuntimeSource = 'react/jsx-runtime'; | ||
function isJSXRuntime(p, imports) { | ||
const jsxRuntime = imports.find(i => i.source === JSXRuntimeSource); | ||
const jsxRuntimeName = jsxRuntime?.local?.isIdentifier() && jsxRuntime?.local?.node?.name; | ||
if (jsxRuntime) { | ||
const callee = getCallee(p); | ||
if (jsxRuntimeName && callee.isIdentifier({ | ||
@@ -37,3 +28,2 @@ name: jsxRuntimeName | ||
} | ||
if (callee.isMemberExpression() && imports.find(i => i.source === JSXRuntimeSource && i.local === callee)) { | ||
@@ -43,10 +33,7 @@ return true; | ||
} | ||
return false; | ||
} | ||
function isHookOrCreateElement(name) { | ||
return name === 'createElement' || /use[A-Z]/.test(name); | ||
} | ||
function isClassicReactRuntime(p, imports) { | ||
@@ -56,3 +43,2 @@ const reactImports = imports.filter(i => i.source === 'react' && (i.imported === 'default' || i.imported && isHookOrCreateElement(i.imported))); | ||
const callee = getCallee(p); | ||
if (callee.isIdentifier() && isHookOrCreateElement(callee.node.name)) { | ||
@@ -62,3 +48,2 @@ const bindingPath = getScope(callee).getBinding(callee.node.name)?.path; | ||
} | ||
if (callee.isMemberExpression()) { | ||
@@ -69,7 +54,5 @@ if (reactImports.some(i => i.local === callee)) { | ||
} | ||
const object = callee.get('object'); | ||
const property = callee.get('property'); | ||
const defaultImport = reactImports.find(i => i.imported === 'default'); | ||
if (!defaultImport || !defaultImport.local.isIdentifier() || !property.isIdentifier() || !isHookOrCreateElement(property.node.name) || !object.isIdentifier({ | ||
@@ -80,17 +63,12 @@ name: defaultImport.local.node.name | ||
} | ||
const bindingPath = getScope(object).getBinding(object.node.name)?.path; | ||
return bindingPath?.isAncestor(defaultImport.local) ?? false; | ||
} | ||
return false; | ||
} | ||
export default function isUnnecessaryReactCall(path) { | ||
const programPath = path.findParent(p => p.isProgram()); | ||
if (!programPath) { | ||
return false; | ||
} | ||
const { | ||
@@ -97,0 +75,0 @@ imports |
import babelMerge from 'babel-merge'; | ||
import isNotNull from '../isNotNull'; | ||
const cache = new WeakMap(); | ||
const merge = (a, b) => { | ||
@@ -9,9 +8,6 @@ if (!cache.has(a)) { | ||
} | ||
const cacheForA = cache.get(a); | ||
if (cacheForA.has(b)) { | ||
return cacheForA.get(b); | ||
} | ||
const result = babelMerge(a, b); | ||
@@ -21,2 +17,3 @@ cacheForA.set(b, result); | ||
}; | ||
/** | ||
@@ -26,4 +23,2 @@ * Merges babel configs together. If a pair of configs were merged before, | ||
*/ | ||
export default function buildOptions(...configs) { | ||
@@ -30,0 +25,0 @@ // Merge all configs together |
@@ -13,3 +13,2 @@ // @ts-expect-error | ||
} | ||
} | ||
@@ -16,0 +15,0 @@ }; |
@@ -5,8 +5,7 @@ const cache = new WeakMap(); | ||
const fileCache = cache.get(overrides) ?? new Map(); | ||
if (fileCache.has(filename)) { | ||
return fileCache.get(filename); | ||
} | ||
const babelOptions = babel.loadOptions({ ...overrides, | ||
const babelOptions = babel.loadOptions({ | ||
...overrides, | ||
filename, | ||
@@ -13,0 +12,0 @@ caller: { |
/* eslint-disable no-restricted-syntax */ | ||
/* eslint @typescript-eslint/no-use-before-define: ["error", { "functions": false }] */ | ||
/* eslint @typescript-eslint/no-use-before-define: ["error", { "functions": false }] */ | ||
import { NODE_FIELDS } from '@babel/types'; | ||
@@ -9,7 +9,5 @@ import findIdentifiers, { nonType } from './findIdentifiers'; | ||
import isRemoved from './isRemoved'; | ||
function validateField(node, key, val, field) { | ||
if (!(field != null && field.validate)) return true; | ||
if (field.optional && val == null) return true; | ||
try { | ||
@@ -22,13 +20,9 @@ field.validate(node, key, val); | ||
} | ||
function getBinding(path) { | ||
const binding = getScope(path).getBinding(path.node.name); | ||
if (!binding) { | ||
return undefined; | ||
} | ||
return binding; | ||
} | ||
export function reference(path, referencePath = path, force = false) { | ||
@@ -38,7 +32,5 @@ if (!force && !path.isReferencedIdentifier()) return; | ||
if (!binding) return; | ||
if (binding.referencePaths.includes(referencePath)) { | ||
return; | ||
} | ||
binding.referenced = true; | ||
@@ -48,26 +40,22 @@ binding.references += 1; | ||
} | ||
function isReferenced(binding) { | ||
if (!binding.referenced) { | ||
return false; | ||
} // If it's a param binding, we can't just remove it | ||
} | ||
// If it's a param binding, we can't just remove it | ||
// because it brakes the function signature. Keep it alive for now. | ||
if (binding.kind === 'param') { | ||
return true; | ||
} // If all remaining references are in TS/Flow types, binding is unreferenced | ||
} | ||
// If all remaining references are in TS/Flow types, binding is unreferenced | ||
return binding.referencePaths.some(i => !i.find(ancestor => ancestor.isTSType() || ancestor.isFlowType())); | ||
} | ||
export function dereference(path) { | ||
const binding = getBinding(path); | ||
if (!binding) return null; | ||
if (!binding.referencePaths.includes(path)) { | ||
return null; | ||
} | ||
binding.references -= 1; | ||
@@ -78,7 +66,5 @@ binding.referencePaths = binding.referencePaths.filter(i => i !== path); | ||
} | ||
function dereferenceAll(path) { | ||
return findIdentifiers([path]).map(identifierPath => dereference(identifierPath)).filter(isNotNull); | ||
} | ||
export function referenceAll(path) { | ||
@@ -88,5 +74,3 @@ findIdentifiers([path]).forEach(identifierPath => reference(identifierPath)); | ||
const deletingNodes = new WeakSet(); | ||
const isEmptyList = list => list.length === 0 || list.every(i => deletingNodes.has(i)); | ||
const getPathFromAction = action => { | ||
@@ -96,10 +80,7 @@ if (!Array.isArray(action)) { | ||
} | ||
if (action[0] === 'replace' || action[0] === 'remove') { | ||
return action[1]; | ||
} | ||
throw new Error(`Unknown action type: ${action[0]}`); | ||
}; | ||
export function findActionForNode(path) { | ||
@@ -110,3 +91,2 @@ if (isRemoved(path)) return null; | ||
if (!parent) return ['remove', path]; | ||
if (parent.isProgram()) { | ||
@@ -116,3 +96,2 @@ // Do not delete Program node | ||
} | ||
if (parent.isFunction() && path.listKey === 'params') { | ||
@@ -122,3 +101,2 @@ // Do not remove params of functions | ||
} | ||
if (parent.isLogicalExpression({ | ||
@@ -132,7 +110,5 @@ operator: '&&' | ||
} | ||
if (parent.isObjectProperty()) { | ||
// let's check if it is a special case with Object.defineProperty | ||
const key = parent.get('key'); | ||
if (key.isIdentifier({ | ||
@@ -142,3 +118,2 @@ name: 'get' | ||
const maybeDefineProperty = parent.parentPath.parentPath; | ||
if (maybeDefineProperty?.isCallExpression() && maybeDefineProperty.get('callee').matchesPattern('Object.defineProperty')) { | ||
@@ -148,6 +123,4 @@ return findActionForNode(maybeDefineProperty); | ||
} | ||
return findActionForNode(parent); | ||
} | ||
if (parent.isTemplateLiteral()) { | ||
@@ -159,11 +132,8 @@ return ['replace', path, { | ||
} | ||
if (parent.isAssignmentExpression()) { | ||
return findActionForNode(parent); | ||
} | ||
if (parent.isCallExpression()) { | ||
return findActionForNode(parent); | ||
} | ||
if (parent.isForInStatement({ | ||
@@ -174,3 +144,2 @@ left: path.node | ||
} | ||
if (parent.isFunctionExpression({ | ||
@@ -181,10 +150,7 @@ body: path.node | ||
} | ||
if (parent.isBlockStatement()) { | ||
const body = parent.get('body'); | ||
if (isEmptyList(body)) { | ||
return findActionForNode(parent); | ||
} | ||
if (path.listKey === 'body' && typeof path.key === 'number') { | ||
@@ -194,3 +160,2 @@ if (path.key > 0) { | ||
const prevStatement = body[path.key - 1]; | ||
if (prevStatement.isIfStatement() && prevStatement.get('consequent').isReturnStatement()) { | ||
@@ -207,15 +172,11 @@ // It's `if (…) return …`, we can remove it. | ||
} | ||
if (parent.isVariableDeclarator()) { | ||
return findActionForNode(parent); | ||
} | ||
if (parent.isExportNamedDeclaration() && (path.key === 'specifiers' && isEmptyList(parent.get('specifiers')) || path.key === 'declaration' && parent.node.declaration === path.node)) { | ||
return findActionForNode(parent); | ||
} | ||
for (const key of ['body', 'declarations', 'specifiers']) { | ||
if (path.listKey === key && typeof path.key === 'number') { | ||
const list = parent.get(key); | ||
if (isEmptyList(list)) { | ||
@@ -226,10 +187,7 @@ return findActionForNode(parent); | ||
} | ||
if (parent.isTryStatement()) { | ||
return findActionForNode(parent); | ||
} | ||
if (!path.listKey) { | ||
const field = NODE_FIELDS[parent.type][path.key]; | ||
if (!validateField(parent.node, path.key, null, field)) { | ||
@@ -240,3 +198,2 @@ // The parent node isn't valid without this field, so we should remove it also. | ||
} | ||
for (const key of ['argument', 'block', 'body', 'callee', 'discriminant', 'expression', 'id', 'left', 'object', 'property', 'right', 'test']) { | ||
@@ -247,6 +204,6 @@ if (path.key === key && parent.get(key) === path) { | ||
} | ||
return ['remove', path]; | ||
} // @babel/preset-typescript transpiles enums, but doesn't reference used identifiers. | ||
} | ||
// @babel/preset-typescript transpiles enums, but doesn't reference used identifiers. | ||
function referenceEnums(program) { | ||
@@ -267,3 +224,2 @@ /* | ||
const [arg] = args; | ||
if (arg.isLogicalExpression({ | ||
@@ -275,8 +231,5 @@ operator: '||' | ||
} | ||
}); | ||
} | ||
const fixed = new WeakSet(); | ||
function removeUnreferenced(items) { | ||
@@ -289,3 +242,2 @@ const referenced = new Set(); | ||
const hasReferences = binding.referencePaths.filter(i => !isRemoved(i)).length > 0; | ||
if (hasReferences) { | ||
@@ -295,3 +247,2 @@ referenced.add(item); | ||
} | ||
const forDeleting = [binding.path, ...binding.constantViolations].map(findActionForNode).filter(isNotNull).map(getPathFromAction); | ||
@@ -308,7 +259,5 @@ if (forDeleting.length === 0) return; | ||
} | ||
function removeWithRelated(paths) { | ||
if (paths.length === 0) return; | ||
const rootPath = getScope(paths[0]).getProgramParent().path; | ||
if (!fixed.has(rootPath)) { | ||
@@ -320,3 +269,2 @@ // Some libraries don't care about bindings, references, and other staff | ||
} | ||
const actions = paths.map(findActionForNode).filter(isNotNull); | ||
@@ -329,3 +277,2 @@ const affectedPaths = actions.map(getPathFromAction); | ||
if (isRemoved(p)) return; | ||
if (action[0] === 'remove') { | ||
@@ -341,3 +288,2 @@ p.remove(); | ||
let clean = false; | ||
while (!clean && referencedIdentifiers.length > 0) { | ||
@@ -349,3 +295,2 @@ const referenced = removeUnreferenced(referencedIdentifiers); | ||
} | ||
function mutate(path, fn) { | ||
@@ -365,3 +310,2 @@ const dereferenced = dereferenceAll(path); | ||
const declared = Object.values(assignment.getOuterBindingIdentifiers(false)); | ||
if (declared.length === 1 && 'name' in declared[0] && declared[0].name === binding.identifier.name) { | ||
@@ -372,3 +316,2 @@ // Only one identifier is declared, so we can remove the whole declaration | ||
} | ||
if (declared.every(identifier => identifier.type === 'Identifier' && !scope.getBinding(identifier.name)?.referenced)) { | ||
@@ -378,5 +321,5 @@ // No other identifier is referenced, so we can remove the whole declaration | ||
return; | ||
} // We can't remove the binding, but we can remove the part of it | ||
} | ||
// We can't remove the binding, but we can remove the part of it | ||
assignment.traverse({ | ||
@@ -386,3 +329,2 @@ Identifier(identifier) { | ||
const parent = identifier.parentPath; | ||
if (parent.isArrayPattern() && identifier.listKey === 'elements' && typeof identifier.key === 'number') { | ||
@@ -395,3 +337,2 @@ parent.node.elements[identifier.key] = null; | ||
} | ||
}); | ||
@@ -402,4 +343,3 @@ }); | ||
} | ||
export { mutate, removeWithRelated }; | ||
//# sourceMappingURL=scopeHelpers.js.map |
/* eslint-disable no-plusplus */ | ||
/** | ||
@@ -12,10 +11,9 @@ * This file contains a utility to generate hashes to be used as generated class names | ||
*/ | ||
function UInt32(str, pos) { | ||
return str.charCodeAt(pos++) + (str.charCodeAt(pos++) << 8) + (str.charCodeAt(pos++) << 16) + (str.charCodeAt(pos) << 24); | ||
} | ||
function UInt16(str, pos) { | ||
return str.charCodeAt(pos++) + (str.charCodeAt(pos++) << 8); | ||
} | ||
function Umul32(n, m) { | ||
@@ -28,3 +26,2 @@ n |= 0; | ||
} | ||
function doHash(str, seed = 0) { | ||
@@ -36,3 +33,2 @@ const m = 0x5bd1e995; | ||
let currentIndex = 0; | ||
while (length >= 4) { | ||
@@ -48,3 +44,2 @@ let k = UInt32(str, currentIndex); | ||
} | ||
switch (length) { | ||
@@ -56,3 +51,2 @@ case 3: | ||
break; | ||
case 2: | ||
@@ -62,3 +56,2 @@ h ^= UInt16(str, currentIndex); | ||
break; | ||
case 1: | ||
@@ -69,3 +62,2 @@ h ^= str.charCodeAt(currentIndex); | ||
} | ||
h ^= h >>> 13; | ||
@@ -76,8 +68,6 @@ h = Umul32(h, m); | ||
} | ||
function slugify(code) { | ||
return doHash(code).toString(36); | ||
} | ||
export default slugify; | ||
//# sourceMappingURL=slugify.js.map |
import { types as t } from '@babel/core'; | ||
import { getScope } from '../getScope'; | ||
import { mutate } from '../scopeHelpers'; | ||
function getFunctionName(path) { | ||
@@ -9,20 +8,20 @@ if (path.isClassMethod() && t.isIdentifier(path.node.key)) { | ||
} | ||
return null; | ||
} | ||
export default function JSXElementsRemover(path) { | ||
// JSX can be safely replaced with null because it is unnecessary for styles | ||
const nullLiteral = t.nullLiteral(); // We can do even more | ||
const nullLiteral = t.nullLiteral(); | ||
// We can do even more | ||
// If that JSX is a result of a function, we can replace the function body. | ||
const functionScope = getScope(path).getFunctionParent(); | ||
const scopePath = functionScope?.path; | ||
if (scopePath?.isFunction()) { | ||
const emptyBody = t.blockStatement([t.returnStatement(nullLiteral)]); // Is it not just a function, but a method `render`? | ||
const emptyBody = t.blockStatement([t.returnStatement(nullLiteral)]); | ||
// Is it not just a function, but a method `render`? | ||
if (getFunctionName(scopePath) === 'render') { | ||
const decl = scopePath.findParent(p => p.isClassDeclaration()); // Replace the whole component | ||
const decl = scopePath.findParent(p => p.isClassDeclaration()); | ||
// Replace the whole component | ||
if (decl?.isClassDeclaration()) { | ||
@@ -35,10 +34,8 @@ mutate(decl, p => { | ||
} | ||
const body = scopePath.get('body'); | ||
if (Array.isArray(body)) { | ||
throw new Error("A body of a function is expected to be a single element but an array was returned. It's possible if JS syntax has been changed since that code was written."); | ||
} | ||
const node = { ...scopePath.node, | ||
const node = { | ||
...scopePath.node, | ||
body: emptyBody, | ||
@@ -45,0 +42,0 @@ params: [] |
@@ -7,7 +7,4 @@ "use strict"; | ||
exports.syncResolve = exports.default = void 0; | ||
var _path = _interopRequireDefault(require("path")); | ||
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } | ||
const safeResolve = (name, where) => { | ||
@@ -22,3 +19,2 @@ try { | ||
}; | ||
const suffixes = ['.js', '.ts', '.jsx', '.tsx'].reduce((acc, ext) => { | ||
@@ -29,15 +25,12 @@ acc.push(`/index${ext}`); | ||
}, []); | ||
const syncResolve = (what, importer, stack) => { | ||
const where = [importer, ...stack].map(p => _path.default.dirname(p)); | ||
const resolved = safeResolve(what, where); | ||
if (!(resolved instanceof Error)) { | ||
return resolved; | ||
} // eslint-disable-next-line no-restricted-syntax | ||
} | ||
// eslint-disable-next-line no-restricted-syntax | ||
for (const suffix of suffixes) { | ||
const resolvedWithSuffix = safeResolve(what + suffix, where); | ||
if (resolvedWithSuffix instanceof Error) { | ||
@@ -47,23 +40,17 @@ // eslint-disable-next-line no-continue | ||
} | ||
return resolvedWithSuffix; | ||
} | ||
throw resolved; | ||
}; | ||
exports.syncResolve = syncResolve; | ||
const asyncResolve = (what, importer, stack) => { | ||
const where = [importer, ...stack].map(p => _path.default.dirname(p)); | ||
const resolved = safeResolve(what, where); | ||
if (!(resolved instanceof Error)) { | ||
return Promise.resolve(resolved); | ||
} // eslint-disable-next-line no-restricted-syntax | ||
} | ||
// eslint-disable-next-line no-restricted-syntax | ||
for (const suffix of suffixes) { | ||
const resolvedWithSuffix = safeResolve(what + suffix, where); | ||
if (resolvedWithSuffix instanceof Error) { | ||
@@ -73,11 +60,8 @@ // eslint-disable-next-line no-continue | ||
} | ||
return Promise.resolve(resolvedWithSuffix); | ||
} | ||
throw resolved; | ||
}; | ||
var _default = asyncResolve; | ||
exports.default = _default; | ||
//# sourceMappingURL=asyncResolveFallback.js.map |
@@ -8,28 +8,16 @@ "use strict"; | ||
exports.sideEffectImport = exports.explicitImport = void 0; | ||
var _logger = require("@linaria/logger"); | ||
var _getScope = require("./getScope"); | ||
var _isExports = _interopRequireDefault(require("./isExports")); | ||
var _isNotNull = _interopRequireDefault(require("./isNotNull")); | ||
var _isRequire = _interopRequireDefault(require("./isRequire")); | ||
var _isTypedNode = _interopRequireDefault(require("./isTypedNode")); | ||
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } | ||
/* eslint @typescript-eslint/no-use-before-define: ["error", { "functions": false }] */ | ||
/* eslint-disable no-restricted-syntax,no-continue */ | ||
/* eslint-disable no-restricted-syntax,no-continue */ | ||
const sideEffectImport = item => item.imported === null; | ||
exports.sideEffectImport = sideEffectImport; | ||
const explicitImport = item => item.imported !== null; | ||
exports.explicitImport = explicitImport; | ||
function getValue({ | ||
@@ -39,8 +27,9 @@ node | ||
return node.type === 'Identifier' ? node.name : node.value; | ||
} // We ignore imports and exports of types | ||
} | ||
// We ignore imports and exports of types | ||
const isType = p => 'importKind' in p.node && p.node.importKind === 'type' || 'exportKind' in p.node && p.node.exportKind === 'type'; | ||
const isType = p => 'importKind' in p.node && p.node.importKind === 'type' || 'exportKind' in p.node && p.node.exportKind === 'type'; // Force TypeScript to check, that we have implementation for every possible specifier | ||
// Force TypeScript to check, that we have implementation for every possible specifier | ||
const collectors = { | ||
@@ -57,3 +46,2 @@ ImportSpecifier(path, source) { | ||
}, | ||
ImportDefaultSpecifier(path, source) { | ||
@@ -67,3 +55,2 @@ const local = path.get('local'); | ||
}, | ||
ImportNamespaceSpecifier(path, source) { | ||
@@ -77,5 +64,3 @@ const local = path.get('local'); | ||
} | ||
}; | ||
function collectFromImportDeclaration(path, state) { | ||
@@ -86,3 +71,2 @@ // If importKind is specified, and it's not a value, ignore that import | ||
const specifiers = path.get('specifiers'); | ||
if (specifiers.length === 0) { | ||
@@ -95,3 +79,2 @@ state.imports.push({ | ||
} | ||
specifiers.forEach(specifier => { | ||
@@ -103,7 +86,5 @@ if (specifier.isImportSpecifier() && isType(specifier)) return; | ||
} | ||
function getAncestorsWhile(path, cond) { | ||
const result = []; | ||
let current = path; | ||
while (current && cond(current)) { | ||
@@ -113,6 +94,4 @@ result.push(current); | ||
} | ||
return result; | ||
} | ||
function whatIsDestructed(objectPattern) { | ||
@@ -124,3 +103,2 @@ const destructedProps = []; | ||
const parent = identifier.parentPath; | ||
if (parent.isObjectProperty({ | ||
@@ -131,3 +109,2 @@ value: identifier.node | ||
const key = p.get('key'); | ||
if (!key.isIdentifier()) { | ||
@@ -137,7 +114,5 @@ // TODO: try to process other type of keys or at least warn about this | ||
} | ||
return key; | ||
}).filter(_isNotNull.default); | ||
chain.reverse(); | ||
if (chain.length > 0) { | ||
@@ -149,6 +124,4 @@ destructedProps.push({ | ||
} | ||
return; | ||
} | ||
if (parent.isRestElement({ | ||
@@ -164,10 +137,7 @@ argument: identifier.node | ||
} | ||
}); | ||
return destructedProps; | ||
} | ||
function importFromVariableDeclarator(path, isSync) { | ||
const id = path.get('id'); | ||
if (id.isIdentifier()) { | ||
@@ -180,3 +150,2 @@ // It's the simplest case when the full namespace is imported | ||
} | ||
if (!isSync) { | ||
@@ -188,18 +157,16 @@ // Something went wrong | ||
} | ||
if (id.isObjectPattern()) { | ||
return whatIsDestructed(id); | ||
} // What else it can be? | ||
} | ||
// What else it can be? | ||
(0, _logger.warn)('evaluator:collectExportsAndImports:importFromVariableDeclarator', 'Unknown type of id', id.node.type); | ||
return []; | ||
} | ||
function exportFromVariableDeclarator(path) { | ||
const id = path.get('id'); | ||
const init = path.get('init'); // If there is no init expression, we can ignore this export | ||
const init = path.get('init'); | ||
// If there is no init expression, we can ignore this export | ||
if (!init || !init.isExpression()) return []; | ||
if (id.isIdentifier()) { | ||
@@ -212,3 +179,2 @@ // It is `export const a = 1;` | ||
} | ||
if (id.isObjectPattern()) { | ||
@@ -220,9 +186,8 @@ // It is `export const { a, ...rest } = obj;` | ||
})); | ||
} // What else it can be? | ||
} | ||
// What else it can be? | ||
(0, _logger.warn)('evaluator:collectExportsAndImports:exportFromVariableDeclarator', 'Unknown type of id', id.node.type); | ||
return []; | ||
} | ||
function collectFromDynamicImport(path, state) { | ||
@@ -232,3 +197,2 @@ const { | ||
} = path; | ||
if (!callExpression.isCallExpression()) { | ||
@@ -238,5 +202,3 @@ // It's wrong `import` | ||
} | ||
const [sourcePath] = callExpression.get('arguments'); | ||
if (!sourcePath || !sourcePath.isStringLiteral()) { | ||
@@ -246,3 +208,2 @@ // Import should have at least one argument, and it should be StringLiteral | ||
} | ||
const source = sourcePath.node.value; | ||
@@ -254,3 +215,2 @@ let { | ||
let isAwaited = false; | ||
if (container.isAwaitExpression()) { | ||
@@ -261,5 +221,5 @@ // If it's not awaited import, it imports the full namespace | ||
container = container.parentPath; | ||
} // Is it `const something = await import("something")`? | ||
} | ||
// Is it `const something = await import("something")`? | ||
if (key === 'init' && container.isVariableDeclarator()) { | ||
@@ -273,29 +233,21 @@ importFromVariableDeclarator(container, isAwaited).map(prop => state.imports.push({ | ||
} | ||
function getImportTypeByInteropFunction(path) { | ||
const callee = path.get('callee'); | ||
if (!callee.isIdentifier()) { | ||
return undefined; | ||
} | ||
const { | ||
name | ||
} = callee.node; | ||
if (name.startsWith('_interopRequireDefault') || name.startsWith('__importDefault')) { | ||
return 'default'; | ||
} | ||
if (name.startsWith('_interopRequireWildcard') || name.startsWith('__importStar')) { | ||
return '*'; | ||
} | ||
if (name.startsWith('__rest')) { | ||
if (name.startsWith('__rest') || name.startsWith('_objectDestructuringEmpty')) { | ||
return '*'; | ||
} | ||
return undefined; | ||
} | ||
function collectFromRequire(path, state) { | ||
@@ -306,3 +258,2 @@ if (!(0, _isRequire.default)(path)) return; | ||
} = path; | ||
if (!callExpression.isCallExpression()) { | ||
@@ -312,5 +263,3 @@ // It's wrong `require` | ||
} | ||
const [sourcePath] = callExpression.get('arguments'); | ||
if (!sourcePath || !sourcePath.isStringLiteral()) { | ||
@@ -320,3 +269,2 @@ // Import should have at least one argument, and it should be StringLiteral | ||
} | ||
const source = sourcePath.node.value; | ||
@@ -327,3 +275,2 @@ const { | ||
} = callExpression; | ||
if (container.isCallExpression() && key === 0) { | ||
@@ -333,3 +280,2 @@ // It may be transpiled import such as | ||
const imported = getImportTypeByInteropFunction(container); | ||
if (!imported) { | ||
@@ -341,7 +287,12 @@ // It's not a transpiled import. | ||
} | ||
const { | ||
let { | ||
parentPath: variableDeclarator | ||
} = container; | ||
if (variableDeclarator.isCallExpression()) { | ||
if (variableDeclarator.get('callee').isIdentifier({ | ||
name: '_extends' | ||
})) { | ||
variableDeclarator = variableDeclarator.parentPath; | ||
} | ||
} | ||
if (!variableDeclarator.isVariableDeclarator()) { | ||
@@ -352,5 +303,3 @@ // TODO: Where else it can be? | ||
} | ||
const id = variableDeclarator.get('id'); | ||
if (!id.isIdentifier()) { | ||
@@ -360,3 +309,2 @@ (0, _logger.warn)('evaluator:collectExportsAndImports', 'Id should be Identifier', variableDeclarator.node.type); | ||
} | ||
if (imported === '*') { | ||
@@ -377,7 +325,5 @@ const unfolded = unfoldNamespaceImport({ | ||
} | ||
if (container.isMemberExpression()) { | ||
// It is `require('@linaria/shaker').dep` | ||
const property = container.get('property'); | ||
if (!property.isIdentifier() && !property.isStringLiteral()) { | ||
@@ -387,11 +333,8 @@ (0, _logger.warn)('evaluator:collectExportsAndImports', 'Property should be Identifier or StringLiteral', property.node.type); | ||
} | ||
const { | ||
parentPath: variableDeclarator | ||
} = container; | ||
if (variableDeclarator.isVariableDeclarator()) { | ||
// It is `const … = require('@linaria/shaker').dep`; | ||
const id = variableDeclarator.get('id'); | ||
if (id.isIdentifier()) { | ||
@@ -415,7 +358,6 @@ state.imports.push({ | ||
} | ||
return; | ||
} // Is it `const something = require("something")`? | ||
} | ||
// Is it `const something = require("something")`? | ||
if (key === 'init' && container.isVariableDeclarator()) { | ||
@@ -439,3 +381,2 @@ importFromVariableDeclarator(container, true).forEach(prop => { | ||
} | ||
if (container.isExpressionStatement()) { | ||
@@ -450,6 +391,4 @@ // Looks like standalone require | ||
} | ||
function isChainOfVoidAssignment(path) { | ||
const right = path.get('right'); | ||
if (right.isUnaryExpression({ | ||
@@ -460,10 +399,23 @@ operator: 'void' | ||
} | ||
if (right.isAssignmentExpression()) { | ||
return isChainOfVoidAssignment(right); | ||
} | ||
return false; | ||
} | ||
function getReturnValue(path) { | ||
if (path.node.params.length !== 0) return undefined; | ||
const body = path.get('body'); | ||
if (body.isExpression()) { | ||
return body; | ||
} | ||
if (body.node.body.length === 1) { | ||
var _body$get; | ||
const returnStatement = (_body$get = body.get('body')) === null || _body$get === void 0 ? void 0 : _body$get[0]; | ||
if (!returnStatement.isReturnStatement()) return undefined; | ||
const argument = returnStatement.get('argument'); | ||
if (!argument.isExpression()) return undefined; | ||
return argument; | ||
} | ||
return undefined; | ||
} | ||
function getGetterValueFromDescriptor(descriptor) { | ||
@@ -474,21 +426,9 @@ const getter = descriptor.get('properties').filter((0, _isTypedNode.default)('ObjectProperty')).find(p => p.get('key').isIdentifier({ | ||
const value = getter === null || getter === void 0 ? void 0 : getter.get('value'); | ||
if (value !== null && value !== void 0 && value.isFunctionExpression()) { | ||
const returnStatement = value.get('body').get('body')[0]; | ||
if (returnStatement !== null && returnStatement !== void 0 && returnStatement.isReturnStatement()) { | ||
const local = returnStatement.get('argument'); | ||
if (local.isExpression()) { | ||
return local; | ||
} | ||
} | ||
if (value !== null && value !== void 0 && value.isFunctionExpression() || value !== null && value !== void 0 && value.isArrowFunctionExpression()) { | ||
return getReturnValue(value); | ||
} | ||
return undefined; | ||
} | ||
function collectFromExports(path, state) { | ||
if (!(0, _isExports.default)(path)) return; | ||
if (path.parentPath.isMemberExpression({ | ||
@@ -500,9 +440,6 @@ object: path.node | ||
const property = memberExpression.get('property'); | ||
if (!property.isIdentifier()) { | ||
return; | ||
} | ||
const exportName = property.node.name; | ||
const saveRef = () => { | ||
@@ -513,8 +450,5 @@ // Save all export.____ usages for later | ||
} | ||
state.exportRefs.get(exportName).push(memberExpression); | ||
}; | ||
const assignmentExpression = memberExpression.parentPath; | ||
if (!assignmentExpression.isAssignmentExpression({ | ||
@@ -527,5 +461,3 @@ left: memberExpression.node | ||
} | ||
const right = assignmentExpression.get('right'); | ||
if (isChainOfVoidAssignment(assignmentExpression)) { | ||
@@ -535,11 +467,8 @@ // It is `exports.foo = void 0` | ||
} | ||
const { | ||
name | ||
} = property.node; | ||
if (name === '__esModule') { | ||
return; | ||
} | ||
saveRef(); | ||
@@ -552,6 +481,4 @@ state.exports.push({ | ||
} | ||
if (path.parentPath.isCallExpression() && path.parentPath.get('callee').matchesPattern('Object.defineProperty')) { | ||
const [obj, prop, descriptor] = path.parentPath.get('arguments'); | ||
if (obj !== null && obj !== void 0 && obj.isIdentifier(path.node) && prop !== null && prop !== void 0 && prop.isStringLiteral() && prop.node.value !== '__esModule' && descriptor !== null && descriptor !== void 0 && descriptor.isObjectExpression()) { | ||
@@ -568,3 +495,2 @@ /** | ||
const local = getGetterValueFromDescriptor(descriptor); | ||
if (local) { | ||
@@ -586,3 +512,2 @@ state.exports.push({ | ||
const local = getGetterValueFromDescriptor(descriptor); | ||
if (local) { | ||
@@ -597,3 +522,2 @@ state.exports.push({ | ||
} | ||
function collectFromRequireOrExports(path, state) { | ||
@@ -606,3 +530,2 @@ if ((0, _isRequire.default)(path)) { | ||
} | ||
function unfoldNamespaceImport(importItem) { | ||
@@ -613,3 +536,2 @@ const result = []; | ||
} = importItem; | ||
if (!local.isIdentifier()) { | ||
@@ -619,5 +541,3 @@ // TODO: handle it | ||
} | ||
const binding = (0, _getScope.getScope)(local).getBinding(local.node.name); | ||
if (!(binding !== null && binding !== void 0 && binding.referenced)) { | ||
@@ -628,14 +548,10 @@ // Imported namespace is not referenced and probably not used, | ||
} | ||
for (const referencePath of (_binding$referencePat = binding === null || binding === void 0 ? void 0 : binding.referencePaths) !== null && _binding$referencePat !== void 0 ? _binding$referencePat : []) { | ||
var _binding$referencePat; | ||
if (referencePath.find(ancestor => ancestor.isTSType() || ancestor.isFlowType())) { | ||
continue; | ||
} | ||
const { | ||
parentPath | ||
} = referencePath; | ||
if (parentPath !== null && parentPath !== void 0 && parentPath.isMemberExpression() && referencePath.key === 'object') { | ||
@@ -645,3 +561,2 @@ const property = parentPath.get('property'); | ||
let imported; | ||
if (parentPath.node.computed && property.isStringLiteral()) { | ||
@@ -654,5 +569,5 @@ imported = property.node.value; | ||
} | ||
if (object.isIdentifier() && imported) { | ||
result.push({ ...importItem, | ||
result.push({ | ||
...importItem, | ||
imported, | ||
@@ -665,8 +580,7 @@ local: parentPath | ||
} | ||
continue; | ||
} | ||
if (parentPath !== null && parentPath !== void 0 && parentPath.isVariableDeclarator() && referencePath.key === 'init') { | ||
importFromVariableDeclarator(parentPath, true).map(prop => result.push({ ...importItem, | ||
importFromVariableDeclarator(parentPath, true).map(prop => result.push({ | ||
...importItem, | ||
imported: prop.what, | ||
@@ -677,3 +591,2 @@ local: prop.as | ||
} | ||
if (parentPath !== null && parentPath !== void 0 && parentPath.isExportSpecifier()) { | ||
@@ -683,6 +596,6 @@ // The whole namespace is re-exported | ||
break; | ||
} // Otherwise, we can't predict usage and import it as is | ||
} | ||
// Otherwise, we can't predict usage and import it as is | ||
// TODO: handle more cases | ||
(0, _logger.warn)('evaluator:collectExportsAndImports:unfoldNamespaceImports', 'Unknown reference', referencePath.node.type); | ||
@@ -692,13 +605,11 @@ result.push(importItem); | ||
} | ||
return result; | ||
} | ||
function collectFromExportAllDeclaration(path, state) { | ||
var _path$get, _path$get$node; | ||
if (isType(path)) return; | ||
const source = (_path$get = path.get('source')) === null || _path$get === void 0 ? void 0 : (_path$get$node = _path$get.node) === null || _path$get$node === void 0 ? void 0 : _path$get$node.value; | ||
if (!source) return; // It is `export * from './css';` | ||
if (!source) return; | ||
// It is `export * from './css';` | ||
state.reexports.push({ | ||
@@ -711,7 +622,5 @@ exported: '*', | ||
} | ||
function collectFromExportSpecifier(path, source, state) { | ||
if (path.isExportSpecifier()) { | ||
const exported = getValue(path.get('exported')); | ||
if (source) { | ||
@@ -733,6 +642,4 @@ // It is `export { foo } from './css';` | ||
} | ||
return; | ||
} | ||
if (path.isExportDefaultSpecifier() && source) { | ||
@@ -747,6 +654,5 @@ // It is `export default from './css';` | ||
} | ||
if (path.isExportNamespaceSpecifier() && source) { | ||
const exported = path.get('exported').node.name; // It is `export * as foo from './css';` | ||
const exported = path.get('exported').node.name; | ||
// It is `export * as foo from './css';` | ||
state.reexports.push({ | ||
@@ -758,21 +664,16 @@ exported, | ||
}); | ||
} // TODO: handle other cases | ||
} | ||
// TODO: handle other cases | ||
(0, _logger.warn)('evaluator:collectExportsAndImports:collectFromExportSpecifier', 'Unprocessed ExportSpecifier', path.node.type); | ||
} | ||
function collectFromExportNamedDeclaration(path, state) { | ||
var _path$get2, _path$get2$node; | ||
if (isType(path)) return; | ||
const source = (_path$get2 = path.get('source')) === null || _path$get2 === void 0 ? void 0 : (_path$get2$node = _path$get2.node) === null || _path$get2$node === void 0 ? void 0 : _path$get2$node.value; | ||
const specifiers = path.get('specifiers'); | ||
if (specifiers) { | ||
specifiers.forEach(specifier => collectFromExportSpecifier(specifier, source, state)); | ||
} | ||
const declaration = path.get('declaration'); | ||
if (declaration.isVariableDeclaration()) { | ||
@@ -786,6 +687,4 @@ declaration.get('declarations').forEach(declarator => { | ||
} | ||
if (declaration.isFunctionDeclaration()) { | ||
const id = declaration.get('id'); | ||
if (id.isIdentifier()) { | ||
@@ -799,3 +698,2 @@ state.exports.push({ | ||
} | ||
function collectFromExportDefaultDeclaration(path, state) { | ||
@@ -809,15 +707,10 @@ if (isType(path)) return; | ||
} | ||
const cache = new WeakMap(); | ||
function collectFromAssignmentExpression(path, state) { | ||
var _right$get; | ||
const left = path.get('left'); | ||
const right = path.get('right'); | ||
let exported; | ||
if (left.isMemberExpression() && (0, _isExports.default)(left.get('object'))) { | ||
const property = left.get('property'); | ||
if (property.isIdentifier()) { | ||
@@ -834,4 +727,6 @@ exported = property.node.name; | ||
const source = sourcePath.isStringLiteral() ? sourcePath.node.value : undefined; | ||
if (!source) return; // It is `exports.foo = require('./css');` | ||
if (!source) return; | ||
// It is `exports.foo = require('./css');` | ||
state.reexports.push({ | ||
@@ -845,12 +740,4 @@ exported, | ||
} | ||
function collectFromCallExpression(path, state) { | ||
function collectFromExportStarCall(path, state) { | ||
var _requireCall$get; | ||
const maybeExportStart = path.get('callee'); | ||
if (!maybeExportStart.isIdentifier() || !maybeExportStart.node.name.startsWith('__exportStar')) { | ||
return; | ||
} | ||
const [requireCall, exports] = path.get('arguments'); | ||
@@ -872,3 +759,49 @@ if (!(0, _isExports.default)(exports)) return; | ||
} | ||
function collectFromExportCall(path, state) { | ||
const [exports, map] = path.get('arguments'); | ||
if (!(0, _isExports.default)(exports)) return; | ||
if (!map.isObjectExpression()) return; | ||
const properties = map.get('properties'); | ||
properties.forEach(property => { | ||
if (!property.isObjectProperty()) return; | ||
const key = property.get('key'); | ||
const value = property.get('value'); | ||
if (!key.isIdentifier()) return; | ||
const exported = key.node.name; | ||
if (!value.isFunction()) return; | ||
if (value.node.params.length !== 0) return; | ||
const returnValue = getReturnValue(value); | ||
if (!returnValue) return; | ||
state.exports.push({ | ||
exported, | ||
local: returnValue | ||
}); | ||
}); | ||
path.skip(); | ||
} | ||
function collectFromCallExpression(path, state) { | ||
const maybeExportStart = path.get('callee'); | ||
if (!maybeExportStart.isIdentifier()) { | ||
return; | ||
} | ||
const { | ||
name | ||
} = maybeExportStart.node; | ||
// TypeScript | ||
if (name.startsWith('__exportStar')) { | ||
collectFromExportStarCall(path, state); | ||
return; | ||
} | ||
// swc | ||
if (name === '_exportStar') { | ||
collectFromExportStarCall(path, state); | ||
} | ||
// swc | ||
if (name === '_export') { | ||
collectFromExportCall(path, state); | ||
} | ||
} | ||
function collectExportsAndImports(path, force = false) { | ||
@@ -881,9 +814,6 @@ const state = { | ||
}; | ||
if (!force && cache.has(path)) { | ||
var _cache$get; | ||
return (_cache$get = cache.get(path)) !== null && _cache$get !== void 0 ? _cache$get : state; | ||
} | ||
path.traverse({ | ||
@@ -890,0 +820,0 @@ AssignmentExpression: collectFromAssignmentExpression, |
@@ -8,8 +8,5 @@ "use strict"; | ||
exports.nonType = nonType; | ||
var _getScope = require("./getScope"); | ||
function isInVoid(path) { | ||
var _path$parentPath$isUn, _path$parentPath; | ||
return (_path$parentPath$isUn = (_path$parentPath = path.parentPath) === null || _path$parentPath === void 0 ? void 0 : _path$parentPath.isUnaryExpression({ | ||
@@ -19,12 +16,10 @@ operator: 'void' | ||
} | ||
function isBindingIdentifier(path) { | ||
return path.isBindingIdentifier() && !isInVoid(path); | ||
} | ||
function isReferencedIdentifier(path) { | ||
return path.isReferencedIdentifier() || isInVoid(path); | ||
} // For some reasons, `isBindingIdentifier` returns true for identifiers inside `void` expressions. | ||
} | ||
// For some reasons, `isBindingIdentifier` returns true for identifiers inside `void` expressions. | ||
const checkers = { | ||
@@ -35,7 +30,5 @@ binding: ex => isBindingIdentifier(ex), | ||
}; | ||
function nonType(path) { | ||
return !path.find(p => p.isTSTypeReference() || p.isTSTypeQuery() || p.isFlowType() || p.isFlowDeclaration() || p.isTSInterfaceDeclaration()); | ||
} | ||
function findIdentifiers(expressions, type = 'referenced') { | ||
@@ -48,15 +41,13 @@ const identifiers = []; | ||
} | ||
if (!nonType(path)) { | ||
// If skip in TSTypeAnnotation visitor doesn't work | ||
return; | ||
} // TODO: Is there a better way to check that it's a local variable? | ||
} | ||
// TODO: Is there a better way to check that it's a local variable? | ||
const binding = (0, _getScope.getScope)(path).getBinding(path.node.name); | ||
if (!binding) { | ||
return; | ||
} | ||
if (type === 'referenced' && ex.isAncestor(binding.path)) { | ||
@@ -66,6 +57,4 @@ // This identifier is declared inside the expression. We don't need it. | ||
} | ||
identifiers.push(path); | ||
}; | ||
if (ex.isIdentifier() || ex.isJSXIdentifier()) { | ||
@@ -80,11 +69,8 @@ emit(ex); | ||
}, | ||
Identifier(path) { | ||
emit(path); | ||
}, | ||
JSXIdentifier(path) { | ||
emit(path); | ||
} | ||
}); | ||
@@ -91,0 +77,0 @@ } |
@@ -9,3 +9,2 @@ "use strict"; | ||
const processed = new Map(); | ||
function getFileIdx(name) { | ||
@@ -16,5 +15,4 @@ if (!processed.has(name)) { | ||
} | ||
return processed.get(name); | ||
} | ||
//# sourceMappingURL=getFileIdx.js.map |
@@ -7,3 +7,2 @@ "use strict"; | ||
exports.getScope = getScope; | ||
function getScope(path) { | ||
@@ -10,0 +9,0 @@ // In some nodes (like FunctionDeclaration) `scope` for `id` returns |
@@ -106,7 +106,4 @@ "use strict"; | ||
}); | ||
var _asyncResolveFallback = _interopRequireWildcard(require("./asyncResolveFallback")); | ||
var _collectExportsAndImports = _interopRequireWildcard(require("./collectExportsAndImports")); | ||
Object.keys(_collectExportsAndImports).forEach(function (key) { | ||
@@ -123,21 +120,11 @@ if (key === "default" || key === "__esModule") return; | ||
}); | ||
var _findIdentifiers = _interopRequireWildcard(require("./findIdentifiers")); | ||
var _getFileIdx = _interopRequireDefault(require("./getFileIdx")); | ||
var _isExports = _interopRequireDefault(require("./isExports")); | ||
var _isNotNull = _interopRequireDefault(require("./isNotNull")); | ||
var _isRemoved = _interopRequireDefault(require("./isRemoved")); | ||
var _isRequire = _interopRequireDefault(require("./isRequire")); | ||
var _isTypedNode = _interopRequireDefault(require("./isTypedNode")); | ||
var _isUnnecessaryReactCall = _interopRequireDefault(require("./isUnnecessaryReactCall")); | ||
var _options = require("./options"); | ||
Object.keys(_options).forEach(function (key) { | ||
@@ -154,5 +141,3 @@ if (key === "default" || key === "__esModule") return; | ||
}); | ||
var _scopeHelpers = require("./scopeHelpers"); | ||
Object.keys(_scopeHelpers).forEach(function (key) { | ||
@@ -169,12 +154,7 @@ if (key === "default" || key === "__esModule") return; | ||
}); | ||
var _slugify = _interopRequireDefault(require("./slugify")); | ||
var _JSXElementsRemover = _interopRequireDefault(require("./visitors/JSXElementsRemover")); | ||
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } | ||
function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function (nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); } | ||
function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; } | ||
//# sourceMappingURL=index.js.map |
@@ -7,5 +7,3 @@ "use strict"; | ||
exports.default = isExports; | ||
var _getScope = require("./getScope"); | ||
/** | ||
@@ -19,3 +17,2 @@ * Checks that specified Identifier is a global `exports` | ||
} | ||
const scope = (0, _getScope.getScope)(id); | ||
@@ -22,0 +19,0 @@ return scope.getBinding('exports') === undefined && scope.hasGlobal('exports'); |
@@ -7,3 +7,2 @@ "use strict"; | ||
exports.default = isNotNull; | ||
function isNotNull(x) { | ||
@@ -10,0 +9,0 @@ return x !== null; |
@@ -7,3 +7,2 @@ "use strict"; | ||
exports.default = isRemoved; | ||
function isRemoved(path) { | ||
@@ -10,0 +9,0 @@ return path.find(p => p.removed) !== null; |
@@ -7,5 +7,3 @@ "use strict"; | ||
exports.default = isRequire; | ||
var _getScope = require("./getScope"); | ||
/** | ||
@@ -19,3 +17,2 @@ * Checks that specified Identifier is a global `require` | ||
} | ||
const scope = (0, _getScope.getScope)(id); | ||
@@ -22,0 +19,0 @@ return scope.getBinding('require') === undefined && scope.hasGlobal('require'); |
@@ -7,9 +7,7 @@ "use strict"; | ||
exports.default = void 0; | ||
const isTypedNode = type => p => { | ||
return p.type === type; | ||
}; | ||
var _default = isTypedNode; | ||
exports.default = _default; | ||
//# sourceMappingURL=isTypedNode.js.map |
@@ -7,15 +7,9 @@ "use strict"; | ||
exports.default = isUnnecessaryReactCall; | ||
var _collectExportsAndImports = _interopRequireDefault(require("./collectExportsAndImports")); | ||
var _getScope = require("./getScope"); | ||
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } | ||
function getCallee(p) { | ||
const callee = p.get('callee'); | ||
if (callee.isSequenceExpression()) { | ||
const expressions = callee.get('expressions'); | ||
if (expressions.length === 2 && expressions[0].isNumericLiteral({ | ||
@@ -26,20 +20,13 @@ value: 0 | ||
} | ||
return callee; | ||
} | ||
return callee; | ||
} | ||
const JSXRuntimeSource = 'react/jsx-runtime'; | ||
function isJSXRuntime(p, imports) { | ||
var _jsxRuntime$local, _jsxRuntime$local2, _jsxRuntime$local2$no; | ||
const jsxRuntime = imports.find(i => i.source === JSXRuntimeSource); | ||
const jsxRuntimeName = (jsxRuntime === null || jsxRuntime === void 0 ? void 0 : (_jsxRuntime$local = jsxRuntime.local) === null || _jsxRuntime$local === void 0 ? void 0 : _jsxRuntime$local.isIdentifier()) && (jsxRuntime === null || jsxRuntime === void 0 ? void 0 : (_jsxRuntime$local2 = jsxRuntime.local) === null || _jsxRuntime$local2 === void 0 ? void 0 : (_jsxRuntime$local2$no = _jsxRuntime$local2.node) === null || _jsxRuntime$local2$no === void 0 ? void 0 : _jsxRuntime$local2$no.name); | ||
if (jsxRuntime) { | ||
const callee = getCallee(p); | ||
if (jsxRuntimeName && callee.isIdentifier({ | ||
@@ -50,3 +37,2 @@ name: jsxRuntimeName | ||
} | ||
if (callee.isMemberExpression() && imports.find(i => i.source === JSXRuntimeSource && i.local === callee)) { | ||
@@ -56,10 +42,7 @@ return true; | ||
} | ||
return false; | ||
} | ||
function isHookOrCreateElement(name) { | ||
return name === 'createElement' || /use[A-Z]/.test(name); | ||
} | ||
function isClassicReactRuntime(p, imports) { | ||
@@ -69,13 +52,9 @@ const reactImports = imports.filter(i => i.source === 'react' && (i.imported === 'default' || i.imported && isHookOrCreateElement(i.imported))); | ||
const callee = getCallee(p); | ||
if (callee.isIdentifier() && isHookOrCreateElement(callee.node.name)) { | ||
var _getScope$getBinding; | ||
const bindingPath = (_getScope$getBinding = (0, _getScope.getScope)(callee).getBinding(callee.node.name)) === null || _getScope$getBinding === void 0 ? void 0 : _getScope$getBinding.path; | ||
return reactImports.some(i => bindingPath === null || bindingPath === void 0 ? void 0 : bindingPath.isAncestor(i.local)); | ||
} | ||
if (callee.isMemberExpression()) { | ||
var _getScope$getBinding2, _bindingPath$isAncest; | ||
if (reactImports.some(i => i.local === callee)) { | ||
@@ -85,7 +64,5 @@ // It's React.createElement in CJS | ||
} | ||
const object = callee.get('object'); | ||
const property = callee.get('property'); | ||
const defaultImport = reactImports.find(i => i.imported === 'default'); | ||
if (!defaultImport || !defaultImport.local.isIdentifier() || !property.isIdentifier() || !isHookOrCreateElement(property.node.name) || !object.isIdentifier({ | ||
@@ -96,17 +73,12 @@ name: defaultImport.local.node.name | ||
} | ||
const bindingPath = (_getScope$getBinding2 = (0, _getScope.getScope)(object).getBinding(object.node.name)) === null || _getScope$getBinding2 === void 0 ? void 0 : _getScope$getBinding2.path; | ||
return (_bindingPath$isAncest = bindingPath === null || bindingPath === void 0 ? void 0 : bindingPath.isAncestor(defaultImport.local)) !== null && _bindingPath$isAncest !== void 0 ? _bindingPath$isAncest : false; | ||
} | ||
return false; | ||
} | ||
function isUnnecessaryReactCall(path) { | ||
const programPath = path.findParent(p => p.isProgram()); | ||
if (!programPath) { | ||
return false; | ||
} | ||
const { | ||
@@ -113,0 +85,0 @@ imports |
@@ -7,11 +7,6 @@ "use strict"; | ||
exports.default = buildOptions; | ||
var _babelMerge = _interopRequireDefault(require("babel-merge")); | ||
var _isNotNull = _interopRequireDefault(require("../isNotNull")); | ||
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } | ||
const cache = new WeakMap(); | ||
const merge = (a, b) => { | ||
@@ -21,9 +16,6 @@ if (!cache.has(a)) { | ||
} | ||
const cacheForA = cache.get(a); | ||
if (cacheForA.has(b)) { | ||
return cacheForA.get(b); | ||
} | ||
const result = (0, _babelMerge.default)(a, b); | ||
@@ -33,2 +25,3 @@ cacheForA.set(b, result); | ||
}; | ||
/** | ||
@@ -38,4 +31,2 @@ * Merges babel configs together. If a pair of configs were merged before, | ||
*/ | ||
function buildOptions(...configs) { | ||
@@ -42,0 +33,0 @@ // Merge all configs together |
@@ -7,8 +7,6 @@ "use strict"; | ||
exports.default = dynamic; | ||
var _pluginSyntaxDynamicImport = _interopRequireDefault(require("@babel/plugin-syntax-dynamic-import")); | ||
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } | ||
// @ts-expect-error | ||
// @ts-expect-error | ||
function dynamic({ | ||
@@ -24,3 +22,2 @@ types: t | ||
} | ||
} | ||
@@ -27,0 +24,0 @@ }; |
@@ -22,9 +22,5 @@ "use strict"; | ||
}); | ||
var _buildOptions = _interopRequireDefault(require("./buildOptions")); | ||
var _loadBabelOptions = _interopRequireDefault(require("./loadBabelOptions")); | ||
var _types = require("./types"); | ||
Object.keys(_types).forEach(function (key) { | ||
@@ -41,4 +37,3 @@ if (key === "default" || key === "__esModule") return; | ||
}); | ||
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } | ||
//# sourceMappingURL=index.js.map |
@@ -9,13 +9,10 @@ "use strict"; | ||
const empty = {}; | ||
function loadBabelOptions(babel, filename, overrides = empty) { | ||
var _cache$get, _babel$loadOptions; | ||
const fileCache = (_cache$get = cache.get(overrides)) !== null && _cache$get !== void 0 ? _cache$get : new Map(); | ||
if (fileCache.has(filename)) { | ||
return fileCache.get(filename); | ||
} | ||
const babelOptions = (_babel$loadOptions = babel.loadOptions({ ...overrides, | ||
const babelOptions = (_babel$loadOptions = babel.loadOptions({ | ||
...overrides, | ||
filename, | ||
@@ -22,0 +19,0 @@ caller: { |
@@ -12,26 +12,16 @@ "use strict"; | ||
exports.removeWithRelated = removeWithRelated; | ||
var _types = require("@babel/types"); | ||
var _findIdentifiers = _interopRequireWildcard(require("./findIdentifiers")); | ||
var _getScope = require("./getScope"); | ||
var _isNotNull = _interopRequireDefault(require("./isNotNull")); | ||
var _isRemoved = _interopRequireDefault(require("./isRemoved")); | ||
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } | ||
function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function (nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); } | ||
function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; } | ||
/* eslint-disable no-restricted-syntax */ | ||
/* eslint @typescript-eslint/no-use-before-define: ["error", { "functions": false }] */ | ||
/* eslint @typescript-eslint/no-use-before-define: ["error", { "functions": false }] */ | ||
function validateField(node, key, val, field) { | ||
if (!(field != null && field.validate)) return true; | ||
if (field.optional && val == null) return true; | ||
try { | ||
@@ -44,13 +34,9 @@ field.validate(node, key, val); | ||
} | ||
function getBinding(path) { | ||
const binding = (0, _getScope.getScope)(path).getBinding(path.node.name); | ||
if (!binding) { | ||
return undefined; | ||
} | ||
return binding; | ||
} | ||
function reference(path, referencePath = path, force = false) { | ||
@@ -60,7 +46,5 @@ if (!force && !path.isReferencedIdentifier()) return; | ||
if (!binding) return; | ||
if (binding.referencePaths.includes(referencePath)) { | ||
return; | ||
} | ||
binding.referenced = true; | ||
@@ -70,26 +54,22 @@ binding.references += 1; | ||
} | ||
function isReferenced(binding) { | ||
if (!binding.referenced) { | ||
return false; | ||
} // If it's a param binding, we can't just remove it | ||
} | ||
// If it's a param binding, we can't just remove it | ||
// because it brakes the function signature. Keep it alive for now. | ||
if (binding.kind === 'param') { | ||
return true; | ||
} // If all remaining references are in TS/Flow types, binding is unreferenced | ||
} | ||
// If all remaining references are in TS/Flow types, binding is unreferenced | ||
return binding.referencePaths.some(i => !i.find(ancestor => ancestor.isTSType() || ancestor.isFlowType())); | ||
} | ||
function dereference(path) { | ||
const binding = getBinding(path); | ||
if (!binding) return null; | ||
if (!binding.referencePaths.includes(path)) { | ||
return null; | ||
} | ||
binding.references -= 1; | ||
@@ -100,15 +80,10 @@ binding.referencePaths = binding.referencePaths.filter(i => i !== path); | ||
} | ||
function dereferenceAll(path) { | ||
return (0, _findIdentifiers.default)([path]).map(identifierPath => dereference(identifierPath)).filter(_isNotNull.default); | ||
} | ||
function referenceAll(path) { | ||
(0, _findIdentifiers.default)([path]).forEach(identifierPath => reference(identifierPath)); | ||
} | ||
const deletingNodes = new WeakSet(); | ||
const isEmptyList = list => list.length === 0 || list.every(i => deletingNodes.has(i)); | ||
const getPathFromAction = action => { | ||
@@ -118,10 +93,7 @@ if (!Array.isArray(action)) { | ||
} | ||
if (action[0] === 'replace' || action[0] === 'remove') { | ||
return action[1]; | ||
} | ||
throw new Error(`Unknown action type: ${action[0]}`); | ||
}; | ||
function findActionForNode(path) { | ||
@@ -132,3 +104,2 @@ if ((0, _isRemoved.default)(path)) return null; | ||
if (!parent) return ['remove', path]; | ||
if (parent.isProgram()) { | ||
@@ -138,3 +109,2 @@ // Do not delete Program node | ||
} | ||
if (parent.isFunction() && path.listKey === 'params') { | ||
@@ -144,3 +114,2 @@ // Do not remove params of functions | ||
} | ||
if (parent.isLogicalExpression({ | ||
@@ -154,7 +123,5 @@ operator: '&&' | ||
} | ||
if (parent.isObjectProperty()) { | ||
// let's check if it is a special case with Object.defineProperty | ||
const key = parent.get('key'); | ||
if (key.isIdentifier({ | ||
@@ -164,3 +131,2 @@ name: 'get' | ||
const maybeDefineProperty = parent.parentPath.parentPath; | ||
if (maybeDefineProperty !== null && maybeDefineProperty !== void 0 && maybeDefineProperty.isCallExpression() && maybeDefineProperty.get('callee').matchesPattern('Object.defineProperty')) { | ||
@@ -170,6 +136,4 @@ return findActionForNode(maybeDefineProperty); | ||
} | ||
return findActionForNode(parent); | ||
} | ||
if (parent.isTemplateLiteral()) { | ||
@@ -181,11 +145,8 @@ return ['replace', path, { | ||
} | ||
if (parent.isAssignmentExpression()) { | ||
return findActionForNode(parent); | ||
} | ||
if (parent.isCallExpression()) { | ||
return findActionForNode(parent); | ||
} | ||
if (parent.isForInStatement({ | ||
@@ -196,3 +157,2 @@ left: path.node | ||
} | ||
if (parent.isFunctionExpression({ | ||
@@ -203,10 +163,7 @@ body: path.node | ||
} | ||
if (parent.isBlockStatement()) { | ||
const body = parent.get('body'); | ||
if (isEmptyList(body)) { | ||
return findActionForNode(parent); | ||
} | ||
if (path.listKey === 'body' && typeof path.key === 'number') { | ||
@@ -216,3 +173,2 @@ if (path.key > 0) { | ||
const prevStatement = body[path.key - 1]; | ||
if (prevStatement.isIfStatement() && prevStatement.get('consequent').isReturnStatement()) { | ||
@@ -229,15 +185,11 @@ // It's `if (…) return …`, we can remove it. | ||
} | ||
if (parent.isVariableDeclarator()) { | ||
return findActionForNode(parent); | ||
} | ||
if (parent.isExportNamedDeclaration() && (path.key === 'specifiers' && isEmptyList(parent.get('specifiers')) || path.key === 'declaration' && parent.node.declaration === path.node)) { | ||
return findActionForNode(parent); | ||
} | ||
for (const key of ['body', 'declarations', 'specifiers']) { | ||
if (path.listKey === key && typeof path.key === 'number') { | ||
const list = parent.get(key); | ||
if (isEmptyList(list)) { | ||
@@ -248,10 +200,7 @@ return findActionForNode(parent); | ||
} | ||
if (parent.isTryStatement()) { | ||
return findActionForNode(parent); | ||
} | ||
if (!path.listKey) { | ||
const field = _types.NODE_FIELDS[parent.type][path.key]; | ||
if (!validateField(parent.node, path.key, null, field)) { | ||
@@ -262,3 +211,2 @@ // The parent node isn't valid without this field, so we should remove it also. | ||
} | ||
for (const key of ['argument', 'block', 'body', 'callee', 'discriminant', 'expression', 'id', 'left', 'object', 'property', 'right', 'test']) { | ||
@@ -269,7 +217,6 @@ if (path.key === key && parent.get(key) === path) { | ||
} | ||
return ['remove', path]; | ||
} // @babel/preset-typescript transpiles enums, but doesn't reference used identifiers. | ||
} | ||
// @babel/preset-typescript transpiles enums, but doesn't reference used identifiers. | ||
function referenceEnums(program) { | ||
@@ -290,3 +237,2 @@ /* | ||
const [arg] = args; | ||
if (arg.isLogicalExpression({ | ||
@@ -298,8 +244,5 @@ operator: '||' | ||
} | ||
}); | ||
} | ||
const fixed = new WeakSet(); | ||
function removeUnreferenced(items) { | ||
@@ -312,3 +255,2 @@ const referenced = new Set(); | ||
const hasReferences = binding.referencePaths.filter(i => !(0, _isRemoved.default)(i)).length > 0; | ||
if (hasReferences) { | ||
@@ -318,3 +260,2 @@ referenced.add(item); | ||
} | ||
const forDeleting = [binding.path, ...binding.constantViolations].map(findActionForNode).filter(_isNotNull.default).map(getPathFromAction); | ||
@@ -330,3 +271,2 @@ if (forDeleting.length === 0) return; | ||
var _a$node, _b$node; | ||
return (_a$node = a.node) === null || _a$node === void 0 ? void 0 : _a$node.name.localeCompare((_b$node = b.node) === null || _b$node === void 0 ? void 0 : _b$node.name); | ||
@@ -336,7 +276,5 @@ }); | ||
} | ||
function removeWithRelated(paths) { | ||
if (paths.length === 0) return; | ||
const rootPath = (0, _getScope.getScope)(paths[0]).getProgramParent().path; | ||
if (!fixed.has(rootPath)) { | ||
@@ -348,3 +286,2 @@ // Some libraries don't care about bindings, references, and other staff | ||
} | ||
const actions = paths.map(findActionForNode).filter(_isNotNull.default); | ||
@@ -355,3 +292,2 @@ const affectedPaths = actions.map(getPathFromAction); | ||
var _ref; | ||
return (_ref = i.node && (0, _getScope.getScope)(i).getBinding(i.node.name)) !== null && _ref !== void 0 ? _ref : null; | ||
@@ -362,3 +298,2 @@ }).filter(_isNotNull.default).reduce((acc, i) => [...acc, ...i.referencePaths.filter(_findIdentifiers.nonType)], []); | ||
if ((0, _isRemoved.default)(p)) return; | ||
if (action[0] === 'remove') { | ||
@@ -374,7 +309,5 @@ p.remove(); | ||
var _a$node2, _b$node2; | ||
return (_a$node2 = a.node) === null || _a$node2 === void 0 ? void 0 : _a$node2.name.localeCompare((_b$node2 = b.node) === null || _b$node2 === void 0 ? void 0 : _b$node2.name); | ||
}); | ||
let clean = false; | ||
while (!clean && referencedIdentifiers.length > 0) { | ||
@@ -384,7 +317,5 @@ const referenced = removeUnreferenced(referencedIdentifiers); | ||
var _i$node; | ||
return (_i$node = i.node) === null || _i$node === void 0 ? void 0 : _i$node.name; | ||
}).join('|') === referencedIdentifiers.map(i => { | ||
var _i$node2; | ||
return (_i$node2 = i.node) === null || _i$node2 === void 0 ? void 0 : _i$node2.name; | ||
@@ -395,3 +326,2 @@ }).join('|'); | ||
} | ||
function mutate(path, fn) { | ||
@@ -411,3 +341,2 @@ const dereferenced = dereferenceAll(path); | ||
const declared = Object.values(assignment.getOuterBindingIdentifiers(false)); | ||
if (declared.length === 1 && 'name' in declared[0] && declared[0].name === binding.identifier.name) { | ||
@@ -418,6 +347,4 @@ // Only one identifier is declared, so we can remove the whole declaration | ||
} | ||
if (declared.every(identifier => { | ||
var _scope$getBinding; | ||
return identifier.type === 'Identifier' && !((_scope$getBinding = scope.getBinding(identifier.name)) !== null && _scope$getBinding !== void 0 && _scope$getBinding.referenced); | ||
@@ -428,5 +355,5 @@ })) { | ||
return; | ||
} // We can't remove the binding, but we can remove the part of it | ||
} | ||
// We can't remove the binding, but we can remove the part of it | ||
assignment.traverse({ | ||
@@ -436,3 +363,2 @@ Identifier(identifier) { | ||
const parent = identifier.parentPath; | ||
if (parent.isArrayPattern() && identifier.listKey === 'elements' && typeof identifier.key === 'number') { | ||
@@ -445,3 +371,2 @@ parent.node.elements[identifier.key] = null; | ||
} | ||
}); | ||
@@ -448,0 +373,0 @@ }); |
@@ -7,5 +7,3 @@ "use strict"; | ||
exports.default = void 0; | ||
/* eslint-disable no-plusplus */ | ||
/** | ||
@@ -20,10 +18,9 @@ * This file contains a utility to generate hashes to be used as generated class names | ||
*/ | ||
function UInt32(str, pos) { | ||
return str.charCodeAt(pos++) + (str.charCodeAt(pos++) << 8) + (str.charCodeAt(pos++) << 16) + (str.charCodeAt(pos) << 24); | ||
} | ||
function UInt16(str, pos) { | ||
return str.charCodeAt(pos++) + (str.charCodeAt(pos++) << 8); | ||
} | ||
function Umul32(n, m) { | ||
@@ -36,3 +33,2 @@ n |= 0; | ||
} | ||
function doHash(str, seed = 0) { | ||
@@ -44,3 +40,2 @@ const m = 0x5bd1e995; | ||
let currentIndex = 0; | ||
while (length >= 4) { | ||
@@ -56,3 +51,2 @@ let k = UInt32(str, currentIndex); | ||
} | ||
switch (length) { | ||
@@ -64,3 +58,2 @@ case 3: | ||
break; | ||
case 2: | ||
@@ -70,3 +63,2 @@ h ^= UInt16(str, currentIndex); | ||
break; | ||
case 1: | ||
@@ -77,3 +69,2 @@ h ^= str.charCodeAt(currentIndex); | ||
} | ||
h ^= h >>> 13; | ||
@@ -84,9 +75,7 @@ h = Umul32(h, m); | ||
} | ||
function slugify(code) { | ||
return doHash(code).toString(36); | ||
} | ||
var _default = slugify; | ||
exports.default = _default; | ||
//# sourceMappingURL=slugify.js.map |
@@ -7,9 +7,5 @@ "use strict"; | ||
exports.default = JSXElementsRemover; | ||
var _core = require("@babel/core"); | ||
var _getScope = require("../getScope"); | ||
var _scopeHelpers = require("../scopeHelpers"); | ||
function getFunctionName(path) { | ||
@@ -19,22 +15,20 @@ if (path.isClassMethod() && _core.types.isIdentifier(path.node.key)) { | ||
} | ||
return null; | ||
} | ||
function JSXElementsRemover(path) { | ||
// JSX can be safely replaced with null because it is unnecessary for styles | ||
const nullLiteral = _core.types.nullLiteral(); // We can do even more | ||
const nullLiteral = _core.types.nullLiteral(); | ||
// We can do even more | ||
// If that JSX is a result of a function, we can replace the function body. | ||
const functionScope = (0, _getScope.getScope)(path).getFunctionParent(); | ||
const scopePath = functionScope === null || functionScope === void 0 ? void 0 : functionScope.path; | ||
if (scopePath !== null && scopePath !== void 0 && scopePath.isFunction()) { | ||
const emptyBody = _core.types.blockStatement([_core.types.returnStatement(nullLiteral)]); // Is it not just a function, but a method `render`? | ||
const emptyBody = _core.types.blockStatement([_core.types.returnStatement(nullLiteral)]); | ||
// Is it not just a function, but a method `render`? | ||
if (getFunctionName(scopePath) === 'render') { | ||
const decl = scopePath.findParent(p => p.isClassDeclaration()); // Replace the whole component | ||
const decl = scopePath.findParent(p => p.isClassDeclaration()); | ||
// Replace the whole component | ||
if (decl !== null && decl !== void 0 && decl.isClassDeclaration()) { | ||
@@ -47,10 +41,8 @@ (0, _scopeHelpers.mutate)(decl, p => { | ||
} | ||
const body = scopePath.get('body'); | ||
if (Array.isArray(body)) { | ||
throw new Error("A body of a function is expected to be a single element but an array was returned. It's possible if JS syntax has been changed since that code was written."); | ||
} | ||
const node = { ...scopePath.node, | ||
const node = { | ||
...scopePath.node, | ||
body: emptyBody, | ||
@@ -57,0 +49,0 @@ params: [] |
{ | ||
"name": "@linaria/utils", | ||
"description": "Blazing fast zero-runtime CSS in JS library", | ||
"version": "4.2.4", | ||
"version": "4.2.5", | ||
"bugs": "https://github.com/callstack/linaria/issues", | ||
"dependencies": { | ||
"@babel/core": "^7.18.9", | ||
"@babel/plugin-proposal-export-namespace-from": ">=7", | ||
"@babel/plugin-syntax-dynamic-import": ">=7", | ||
"@babel/plugin-transform-modules-commonjs": "^7.18.2", | ||
"@babel/traverse": "^7.18.9", | ||
"@babel/types": "^7.18.9", | ||
"@linaria/logger": "^4.0.0", | ||
"babel-merge": "^3.0.0" | ||
"@babel/core": "^7.20.2", | ||
"@babel/plugin-proposal-export-namespace-from": "^7.18.9", | ||
"@babel/plugin-syntax-dynamic-import": "^7.8.3", | ||
"@babel/plugin-transform-modules-commonjs": "^7.19.6", | ||
"@babel/traverse": "^7.20.1", | ||
"@babel/types": "^7.20.2", | ||
"babel-merge": "^3.0.0", | ||
"@linaria/logger": "^4.0.0" | ||
}, | ||
@@ -47,3 +47,3 @@ "devDependencies": { | ||
"scripts": { | ||
"build": "npm run build:lib && npm run build:esm && npm run build:declarations", | ||
"build": "pnpm build:lib && pnpm build:esm && pnpm build:declarations", | ||
"build:declarations": "tsc --emitDeclarationOnly --outDir types", | ||
@@ -53,4 +53,4 @@ "build:esm": "babel src --out-dir esm --extensions '.js,.jsx,.ts,.tsx' --source-maps --delete-dir-on-start", | ||
"typecheck": "tsc --noEmit --composite false", | ||
"watch": "npm run build --watch" | ||
"watch": "pnpm build:lib --watch & pnpm build:esm --watch & pnpm build:declarations --watch" | ||
} | ||
} |
import type { NodePath } from '@babel/traverse'; | ||
declare const isTypedNode: <T extends "AnyTypeAnnotation" | "ArgumentPlaceholder" | "ArrayExpression" | "ArrayPattern" | "ArrayTypeAnnotation" | "ArrowFunctionExpression" | "AssignmentExpression" | "AssignmentPattern" | "AwaitExpression" | "BigIntLiteral" | "BinaryExpression" | "BindExpression" | "BlockStatement" | "BooleanLiteral" | "BooleanLiteralTypeAnnotation" | "BooleanTypeAnnotation" | "BreakStatement" | "CallExpression" | "CatchClause" | "ClassAccessorProperty" | "ClassBody" | "ClassDeclaration" | "ClassExpression" | "ClassImplements" | "ClassMethod" | "ClassPrivateMethod" | "ClassPrivateProperty" | "ClassProperty" | "ConditionalExpression" | "ContinueStatement" | "DebuggerStatement" | "DecimalLiteral" | "DeclareClass" | "DeclareExportAllDeclaration" | "DeclareExportDeclaration" | "DeclareFunction" | "DeclareInterface" | "DeclareModule" | "DeclareModuleExports" | "DeclareOpaqueType" | "DeclareTypeAlias" | "DeclareVariable" | "DeclaredPredicate" | "Decorator" | "Directive" | "DirectiveLiteral" | "DoExpression" | "DoWhileStatement" | "EmptyStatement" | "EmptyTypeAnnotation" | "EnumBooleanBody" | "EnumBooleanMember" | "EnumDeclaration" | "EnumDefaultedMember" | "EnumNumberBody" | "EnumNumberMember" | "EnumStringBody" | "EnumStringMember" | "EnumSymbolBody" | "ExistsTypeAnnotation" | "ExportAllDeclaration" | "ExportDefaultDeclaration" | "ExportDefaultSpecifier" | "ExportNamedDeclaration" | "ExportNamespaceSpecifier" | "ExportSpecifier" | "ExpressionStatement" | "File" | "ForInStatement" | "ForOfStatement" | "ForStatement" | "FunctionDeclaration" | "FunctionExpression" | "FunctionTypeAnnotation" | "FunctionTypeParam" | "GenericTypeAnnotation" | "Identifier" | "IfStatement" | "Import" | "ImportAttribute" | "ImportDeclaration" | "ImportDefaultSpecifier" | "ImportNamespaceSpecifier" | "ImportSpecifier" | "IndexedAccessType" | "InferredPredicate" | "InterfaceDeclaration" | "InterfaceExtends" | "InterfaceTypeAnnotation" | "InterpreterDirective" | "IntersectionTypeAnnotation" | "JSXAttribute" | "JSXClosingElement" | "JSXClosingFragment" | "JSXElement" | "JSXEmptyExpression" | "JSXExpressionContainer" | "JSXFragment" | "JSXIdentifier" | "JSXMemberExpression" | "JSXNamespacedName" | "JSXOpeningElement" | "JSXOpeningFragment" | "JSXSpreadAttribute" | "JSXSpreadChild" | "JSXText" | "LabeledStatement" | "LogicalExpression" | "MemberExpression" | "MetaProperty" | "MixedTypeAnnotation" | "ModuleExpression" | "NewExpression" | "Noop" | "NullLiteral" | "NullLiteralTypeAnnotation" | "NullableTypeAnnotation" | "NumberLiteral" | "NumberLiteralTypeAnnotation" | "NumberTypeAnnotation" | "NumericLiteral" | "ObjectExpression" | "ObjectMethod" | "ObjectPattern" | "ObjectProperty" | "ObjectTypeAnnotation" | "ObjectTypeCallProperty" | "ObjectTypeIndexer" | "ObjectTypeInternalSlot" | "ObjectTypeProperty" | "ObjectTypeSpreadProperty" | "OpaqueType" | "OptionalCallExpression" | "OptionalIndexedAccessType" | "OptionalMemberExpression" | "ParenthesizedExpression" | "PipelineBareFunction" | "PipelinePrimaryTopicReference" | "PipelineTopicExpression" | "Placeholder" | "PrivateName" | "Program" | "QualifiedTypeIdentifier" | "RecordExpression" | "RegExpLiteral" | "RegexLiteral" | "RestElement" | "RestProperty" | "ReturnStatement" | "SequenceExpression" | "SpreadElement" | "SpreadProperty" | "StaticBlock" | "StringLiteral" | "StringLiteralTypeAnnotation" | "StringTypeAnnotation" | "Super" | "SwitchCase" | "SwitchStatement" | "SymbolTypeAnnotation" | "TSAnyKeyword" | "TSArrayType" | "TSAsExpression" | "TSBigIntKeyword" | "TSBooleanKeyword" | "TSCallSignatureDeclaration" | "TSConditionalType" | "TSConstructSignatureDeclaration" | "TSConstructorType" | "TSDeclareFunction" | "TSDeclareMethod" | "TSEnumDeclaration" | "TSEnumMember" | "TSExportAssignment" | "TSExpressionWithTypeArguments" | "TSExternalModuleReference" | "TSFunctionType" | "TSImportEqualsDeclaration" | "TSImportType" | "TSIndexSignature" | "TSIndexedAccessType" | "TSInferType" | "TSInstantiationExpression" | "TSInterfaceBody" | "TSInterfaceDeclaration" | "TSIntersectionType" | "TSIntrinsicKeyword" | "TSLiteralType" | "TSMappedType" | "TSMethodSignature" | "TSModuleBlock" | "TSModuleDeclaration" | "TSNamedTupleMember" | "TSNamespaceExportDeclaration" | "TSNeverKeyword" | "TSNonNullExpression" | "TSNullKeyword" | "TSNumberKeyword" | "TSObjectKeyword" | "TSOptionalType" | "TSParameterProperty" | "TSParenthesizedType" | "TSPropertySignature" | "TSQualifiedName" | "TSRestType" | "TSStringKeyword" | "TSSymbolKeyword" | "TSThisType" | "TSTupleType" | "TSTypeAliasDeclaration" | "TSTypeAnnotation" | "TSTypeAssertion" | "TSTypeLiteral" | "TSTypeOperator" | "TSTypeParameter" | "TSTypeParameterDeclaration" | "TSTypeParameterInstantiation" | "TSTypePredicate" | "TSTypeQuery" | "TSTypeReference" | "TSUndefinedKeyword" | "TSUnionType" | "TSUnknownKeyword" | "TSVoidKeyword" | "TaggedTemplateExpression" | "TemplateElement" | "TemplateLiteral" | "ThisExpression" | "ThisTypeAnnotation" | "ThrowStatement" | "TopicReference" | "TryStatement" | "TupleExpression" | "TupleTypeAnnotation" | "TypeAlias" | "TypeAnnotation" | "TypeCastExpression" | "TypeParameter" | "TypeParameterDeclaration" | "TypeParameterInstantiation" | "TypeofTypeAnnotation" | "UnaryExpression" | "UnionTypeAnnotation" | "UpdateExpression" | "V8IntrinsicIdentifier" | "VariableDeclaration" | "VariableDeclarator" | "Variance" | "VoidTypeAnnotation" | "WhileStatement" | "WithStatement" | "YieldExpression">(type: T) => (p: NodePath) => p is NodePath<Extract<import("@babel/types").AnyTypeAnnotation, { | ||
declare const isTypedNode: <T extends "AnyTypeAnnotation" | "ArgumentPlaceholder" | "ArrayExpression" | "ArrayPattern" | "ArrayTypeAnnotation" | "ArrowFunctionExpression" | "AssignmentExpression" | "AssignmentPattern" | "AwaitExpression" | "BigIntLiteral" | "BinaryExpression" | "BindExpression" | "BlockStatement" | "BooleanLiteral" | "BooleanLiteralTypeAnnotation" | "BooleanTypeAnnotation" | "BreakStatement" | "CallExpression" | "CatchClause" | "ClassAccessorProperty" | "ClassBody" | "ClassDeclaration" | "ClassExpression" | "ClassImplements" | "ClassMethod" | "ClassPrivateMethod" | "ClassPrivateProperty" | "ClassProperty" | "ConditionalExpression" | "ContinueStatement" | "DebuggerStatement" | "DecimalLiteral" | "DeclareClass" | "DeclareExportAllDeclaration" | "DeclareExportDeclaration" | "DeclareFunction" | "DeclareInterface" | "DeclareModule" | "DeclareModuleExports" | "DeclareOpaqueType" | "DeclareTypeAlias" | "DeclareVariable" | "DeclaredPredicate" | "Decorator" | "Directive" | "DirectiveLiteral" | "DoExpression" | "DoWhileStatement" | "EmptyStatement" | "EmptyTypeAnnotation" | "EnumBooleanBody" | "EnumBooleanMember" | "EnumDeclaration" | "EnumDefaultedMember" | "EnumNumberBody" | "EnumNumberMember" | "EnumStringBody" | "EnumStringMember" | "EnumSymbolBody" | "ExistsTypeAnnotation" | "ExportAllDeclaration" | "ExportDefaultDeclaration" | "ExportDefaultSpecifier" | "ExportNamedDeclaration" | "ExportNamespaceSpecifier" | "ExportSpecifier" | "ExpressionStatement" | "File" | "ForInStatement" | "ForOfStatement" | "ForStatement" | "FunctionDeclaration" | "FunctionExpression" | "FunctionTypeAnnotation" | "FunctionTypeParam" | "GenericTypeAnnotation" | "Identifier" | "IfStatement" | "Import" | "ImportAttribute" | "ImportDeclaration" | "ImportDefaultSpecifier" | "ImportNamespaceSpecifier" | "ImportSpecifier" | "IndexedAccessType" | "InferredPredicate" | "InterfaceDeclaration" | "InterfaceExtends" | "InterfaceTypeAnnotation" | "InterpreterDirective" | "IntersectionTypeAnnotation" | "JSXAttribute" | "JSXClosingElement" | "JSXClosingFragment" | "JSXElement" | "JSXEmptyExpression" | "JSXExpressionContainer" | "JSXFragment" | "JSXIdentifier" | "JSXMemberExpression" | "JSXNamespacedName" | "JSXOpeningElement" | "JSXOpeningFragment" | "JSXSpreadAttribute" | "JSXSpreadChild" | "JSXText" | "LabeledStatement" | "LogicalExpression" | "MemberExpression" | "MetaProperty" | "MixedTypeAnnotation" | "ModuleExpression" | "NewExpression" | "Noop" | "NullLiteral" | "NullLiteralTypeAnnotation" | "NullableTypeAnnotation" | "NumberLiteral" | "NumberLiteralTypeAnnotation" | "NumberTypeAnnotation" | "NumericLiteral" | "ObjectExpression" | "ObjectMethod" | "ObjectPattern" | "ObjectProperty" | "ObjectTypeAnnotation" | "ObjectTypeCallProperty" | "ObjectTypeIndexer" | "ObjectTypeInternalSlot" | "ObjectTypeProperty" | "ObjectTypeSpreadProperty" | "OpaqueType" | "OptionalCallExpression" | "OptionalIndexedAccessType" | "OptionalMemberExpression" | "ParenthesizedExpression" | "PipelineBareFunction" | "PipelinePrimaryTopicReference" | "PipelineTopicExpression" | "Placeholder" | "PrivateName" | "Program" | "QualifiedTypeIdentifier" | "RecordExpression" | "RegExpLiteral" | "RegexLiteral" | "RestElement" | "RestProperty" | "ReturnStatement" | "SequenceExpression" | "SpreadElement" | "SpreadProperty" | "StaticBlock" | "StringLiteral" | "StringLiteralTypeAnnotation" | "StringTypeAnnotation" | "Super" | "SwitchCase" | "SwitchStatement" | "SymbolTypeAnnotation" | "TSAnyKeyword" | "TSArrayType" | "TSAsExpression" | "TSBigIntKeyword" | "TSBooleanKeyword" | "TSCallSignatureDeclaration" | "TSConditionalType" | "TSConstructSignatureDeclaration" | "TSConstructorType" | "TSDeclareFunction" | "TSDeclareMethod" | "TSEnumDeclaration" | "TSEnumMember" | "TSExportAssignment" | "TSExpressionWithTypeArguments" | "TSExternalModuleReference" | "TSFunctionType" | "TSImportEqualsDeclaration" | "TSImportType" | "TSIndexSignature" | "TSIndexedAccessType" | "TSInferType" | "TSInstantiationExpression" | "TSInterfaceBody" | "TSInterfaceDeclaration" | "TSIntersectionType" | "TSIntrinsicKeyword" | "TSLiteralType" | "TSMappedType" | "TSMethodSignature" | "TSModuleBlock" | "TSModuleDeclaration" | "TSNamedTupleMember" | "TSNamespaceExportDeclaration" | "TSNeverKeyword" | "TSNonNullExpression" | "TSNullKeyword" | "TSNumberKeyword" | "TSObjectKeyword" | "TSOptionalType" | "TSParameterProperty" | "TSParenthesizedType" | "TSPropertySignature" | "TSQualifiedName" | "TSRestType" | "TSSatisfiesExpression" | "TSStringKeyword" | "TSSymbolKeyword" | "TSThisType" | "TSTupleType" | "TSTypeAliasDeclaration" | "TSTypeAnnotation" | "TSTypeAssertion" | "TSTypeLiteral" | "TSTypeOperator" | "TSTypeParameter" | "TSTypeParameterDeclaration" | "TSTypeParameterInstantiation" | "TSTypePredicate" | "TSTypeQuery" | "TSTypeReference" | "TSUndefinedKeyword" | "TSUnionType" | "TSUnknownKeyword" | "TSVoidKeyword" | "TaggedTemplateExpression" | "TemplateElement" | "TemplateLiteral" | "ThisExpression" | "ThisTypeAnnotation" | "ThrowStatement" | "TopicReference" | "TryStatement" | "TupleExpression" | "TupleTypeAnnotation" | "TypeAlias" | "TypeAnnotation" | "TypeCastExpression" | "TypeParameter" | "TypeParameterDeclaration" | "TypeParameterInstantiation" | "TypeofTypeAnnotation" | "UnaryExpression" | "UnionTypeAnnotation" | "UpdateExpression" | "V8IntrinsicIdentifier" | "VariableDeclaration" | "VariableDeclarator" | "Variance" | "VoidTypeAnnotation" | "WhileStatement" | "WithStatement" | "YieldExpression">(type: T) => (p: NodePath) => p is NodePath<Extract<import("@babel/types").AnyTypeAnnotation, { | ||
type: T; | ||
@@ -412,2 +412,4 @@ }> | Extract<import("@babel/types").ArgumentPlaceholder, { | ||
type: T; | ||
}> | Extract<import("@babel/types").TSSatisfiesExpression, { | ||
type: T; | ||
}> | Extract<import("@babel/types").TSStringKeyword, { | ||
@@ -414,0 +416,0 @@ type: T; |
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
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
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
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
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
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
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
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
373982
3894