@graphql-tools/graphql-tag-pluck
Advanced tools
Comparing version 8.0.2 to 8.1.0-rc-20231024125426-29a171f8
@@ -81,4 +81,5 @@ "use strict"; | ||
allowUndeclaredExports: true, | ||
sourceFilename: filePath, | ||
}; | ||
} | ||
exports.default = generateConfig; |
@@ -118,5 +118,30 @@ "use strict"; | ||
}; | ||
function defaultPluckStringFromFile(code, { start, end }, options = {}) { | ||
return (0, utils_js_1.freeText)(code | ||
// Slice quotes | ||
.slice(start + 1, end - 1) | ||
// Erase string interpolations as we gonna export everything as a single | ||
// string anyway | ||
.replace(/\$\{[^}]*\}/g, '') | ||
.split('\\`') | ||
.join('`'), options.skipIndent); | ||
} | ||
function defaultIsGqlTemplateLiteral(node, options) { | ||
const leadingComments = node.leadingComments; | ||
if (!leadingComments) { | ||
return; | ||
} | ||
if (!leadingComments.length) { | ||
return; | ||
} | ||
const leadingComment = leadingComments[leadingComments.length - 1]; | ||
const leadingCommentValue = leadingComment.value.trim().toLowerCase(); | ||
if (leadingCommentValue === options.gqlMagicComment) { | ||
return true; | ||
} | ||
return false; | ||
} | ||
exports.default = (code, out, options = {}) => { | ||
// Apply defaults to options | ||
let { modules = [], globalGqlIdentifierName, gqlMagicComment, } = { | ||
let { modules = [], globalGqlIdentifierName, gqlMagicComment, skipIndent, isGqlTemplateLiteral = defaultIsGqlTemplateLiteral, pluckStringFromFile = defaultPluckStringFromFile, } = { | ||
...defaults, | ||
@@ -135,2 +160,3 @@ ...options, | ||
globalGqlIdentifierName = (0, utils_1.asArray)(globalGqlIdentifierName).map(s => s.toLowerCase()); | ||
const hooksOptions = { skipIndent, gqlMagicComment, modules, globalGqlIdentifierName }; | ||
// Keep imported identifiers | ||
@@ -152,12 +178,2 @@ // import gql from 'graphql-tag' -> gql | ||
} | ||
const pluckStringFromFile = ({ start, end }) => { | ||
return (0, utils_js_1.freeText)(code | ||
// Slice quotes | ||
.slice(start + 1, end - 1) | ||
// Erase string interpolations as we gonna export everything as a single | ||
// string anyway | ||
.replace(/\$\{[^}]*\}/g, '') | ||
.split('\\`') | ||
.join('`'), options.skipIndent); | ||
}; | ||
const addTemplateLiteralToResult = (content) => { | ||
@@ -174,16 +190,7 @@ const cacheKey = `end/${content.end}/start/${content.start}/${content.content}`; | ||
const pluckMagicTemplateLiteral = (node, takeExpression = false) => { | ||
const leadingComments = node.leadingComments; | ||
if (!leadingComments) { | ||
if (!isGqlTemplateLiteral(node, hooksOptions)) { | ||
return; | ||
} | ||
if (!leadingComments.length) { | ||
return; | ||
} | ||
const leadingComment = leadingComments[leadingComments.length - 1]; | ||
const leadingCommentValue = leadingComment.value.trim().toLowerCase(); | ||
if (leadingCommentValue !== gqlMagicComment) { | ||
return; | ||
} | ||
const nodeToUse = takeExpression ? node.expression : node; | ||
const gqlTemplateLiteral = pluckStringFromFile(nodeToUse); | ||
const gqlTemplateLiteral = pluckStringFromFile(code, nodeToUse, hooksOptions); | ||
if (gqlTemplateLiteral) { | ||
@@ -235,3 +242,3 @@ addTemplateLiteralToResult({ | ||
if (start != null && end != null && start != null && loc != null) { | ||
const gqlTemplateLiteral = pluckStringFromFile({ start, end }); | ||
const gqlTemplateLiteral = pluckStringFromFile(code, unwrappedExpression, hooksOptions); | ||
// If the entire template was made out of interpolations it should be an empty | ||
@@ -303,3 +310,3 @@ // string by now and thus should be ignored | ||
} | ||
const gqlTemplateLiteral = pluckStringFromFile(path.node.quasi); | ||
const gqlTemplateLiteral = pluckStringFromFile(code, path.node.quasi, hooksOptions); | ||
if (gqlTemplateLiteral) { | ||
@@ -306,0 +313,0 @@ addTemplateLiteralToResult({ |
@@ -79,3 +79,4 @@ import { getExtNameFromFilePath } from './libs/extname.js'; | ||
allowUndeclaredExports: true, | ||
sourceFilename: filePath, | ||
}; | ||
} |
@@ -116,5 +116,30 @@ import { isIdentifier, isImportDefaultSpecifier, isImportSpecifier, isTemplateLiteral, isTSAsExpression, isTSTypeReference, isVariableDeclarator, } from '@babel/types'; | ||
}; | ||
function defaultPluckStringFromFile(code, { start, end }, options = {}) { | ||
return freeText(code | ||
// Slice quotes | ||
.slice(start + 1, end - 1) | ||
// Erase string interpolations as we gonna export everything as a single | ||
// string anyway | ||
.replace(/\$\{[^}]*\}/g, '') | ||
.split('\\`') | ||
.join('`'), options.skipIndent); | ||
} | ||
function defaultIsGqlTemplateLiteral(node, options) { | ||
const leadingComments = node.leadingComments; | ||
if (!leadingComments) { | ||
return; | ||
} | ||
if (!leadingComments.length) { | ||
return; | ||
} | ||
const leadingComment = leadingComments[leadingComments.length - 1]; | ||
const leadingCommentValue = leadingComment.value.trim().toLowerCase(); | ||
if (leadingCommentValue === options.gqlMagicComment) { | ||
return true; | ||
} | ||
return false; | ||
} | ||
export default (code, out, options = {}) => { | ||
// Apply defaults to options | ||
let { modules = [], globalGqlIdentifierName, gqlMagicComment, } = { | ||
let { modules = [], globalGqlIdentifierName, gqlMagicComment, skipIndent, isGqlTemplateLiteral = defaultIsGqlTemplateLiteral, pluckStringFromFile = defaultPluckStringFromFile, } = { | ||
...defaults, | ||
@@ -133,2 +158,3 @@ ...options, | ||
globalGqlIdentifierName = asArray(globalGqlIdentifierName).map(s => s.toLowerCase()); | ||
const hooksOptions = { skipIndent, gqlMagicComment, modules, globalGqlIdentifierName }; | ||
// Keep imported identifiers | ||
@@ -150,12 +176,2 @@ // import gql from 'graphql-tag' -> gql | ||
} | ||
const pluckStringFromFile = ({ start, end }) => { | ||
return freeText(code | ||
// Slice quotes | ||
.slice(start + 1, end - 1) | ||
// Erase string interpolations as we gonna export everything as a single | ||
// string anyway | ||
.replace(/\$\{[^}]*\}/g, '') | ||
.split('\\`') | ||
.join('`'), options.skipIndent); | ||
}; | ||
const addTemplateLiteralToResult = (content) => { | ||
@@ -172,16 +188,7 @@ const cacheKey = `end/${content.end}/start/${content.start}/${content.content}`; | ||
const pluckMagicTemplateLiteral = (node, takeExpression = false) => { | ||
const leadingComments = node.leadingComments; | ||
if (!leadingComments) { | ||
if (!isGqlTemplateLiteral(node, hooksOptions)) { | ||
return; | ||
} | ||
if (!leadingComments.length) { | ||
return; | ||
} | ||
const leadingComment = leadingComments[leadingComments.length - 1]; | ||
const leadingCommentValue = leadingComment.value.trim().toLowerCase(); | ||
if (leadingCommentValue !== gqlMagicComment) { | ||
return; | ||
} | ||
const nodeToUse = takeExpression ? node.expression : node; | ||
const gqlTemplateLiteral = pluckStringFromFile(nodeToUse); | ||
const gqlTemplateLiteral = pluckStringFromFile(code, nodeToUse, hooksOptions); | ||
if (gqlTemplateLiteral) { | ||
@@ -233,3 +240,3 @@ addTemplateLiteralToResult({ | ||
if (start != null && end != null && start != null && loc != null) { | ||
const gqlTemplateLiteral = pluckStringFromFile({ start, end }); | ||
const gqlTemplateLiteral = pluckStringFromFile(code, unwrappedExpression, hooksOptions); | ||
// If the entire template was made out of interpolations it should be an empty | ||
@@ -301,3 +308,3 @@ // string by now and thus should be ignored | ||
} | ||
const gqlTemplateLiteral = pluckStringFromFile(path.node.quasi); | ||
const gqlTemplateLiteral = pluckStringFromFile(code, path.node.quasi, hooksOptions); | ||
if (gqlTemplateLiteral) { | ||
@@ -304,0 +311,0 @@ addTemplateLiteralToResult({ |
{ | ||
"name": "@graphql-tools/graphql-tag-pluck", | ||
"version": "8.0.2", | ||
"version": "8.1.0-rc-20231024125426-29a171f8", | ||
"description": "Pluck graphql-tag template literals", | ||
@@ -5,0 +5,0 @@ "sideEffects": false, |
import { Source } from 'graphql'; | ||
import { ExpressionStatement, TemplateLiteral } from '@babel/types'; | ||
import { PluckedContent } from './visitor.js'; | ||
@@ -103,2 +104,11 @@ /** | ||
skipIndent?: boolean; | ||
/** | ||
* A function that allows custom extraction of GraphQL strings from a file. | ||
*/ | ||
pluckStringFromFile?: (code: string, node: TemplateLiteral, options: Omit<GraphQLTagPluckOptions, 'isGqlTemplateLiteral' | 'pluckStringFromFile'>) => string | undefined | null; | ||
/** | ||
* A custom way to determine if a template literal node contains a GraphQL query. | ||
* By default, it checks if the leading comment is equal to the `gqlMagicComment` option. | ||
*/ | ||
isGqlTemplateLiteral?: (node: TemplateLiteral | ExpressionStatement, options: Omit<GraphQLTagPluckOptions, 'isGqlTemplateLiteral' | 'pluckStringFromFile'>) => boolean | undefined; | ||
} | ||
@@ -105,0 +115,0 @@ /** |
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
No v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
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
59629
1428
2