@atlaskit/codemod-utils
Advanced tools
Comparing version
# @atlaskit/codemod-utils | ||
## 4.1.1 | ||
### Patch Changes | ||
- [`edc6fef0c8f`](https://bitbucket.org/atlassian/atlassian-frontend/commits/edc6fef0c8f) - Fix printf format specifier matching within `matchesStringWithFormatSpecifier` for fuzzy matching interpolated placeholder values | ||
## 4.1.0 | ||
@@ -4,0 +10,0 @@ |
@@ -17,2 +17,4 @@ "use strict"; | ||
exports.isEmpty = exports.hasJSXAttributesByName = exports.hasImportDeclarationFromAnyPackageEntrypoint = exports.hasImportDeclaration = void 0; | ||
exports.matchesStringWithFormatSpecifier = matchesStringWithFormatSpecifier; | ||
exports.placeholderStringMatches = placeholderStringMatches; | ||
exports.removeImport = removeImport; | ||
@@ -194,14 +196,54 @@ exports.testMethodVariantEach = exports.shiftDefaultImport = void 0; | ||
var checkForStringWithFormatSpecifier = function checkForStringWithFormatSpecifier(argValue, str) { | ||
var value = String(argValue); | ||
var formatSpecifierRegex = /%[a-z]/g; | ||
function placeholderStringMatches(placeholderStr, resolvedStr) { | ||
if (placeholderStr === resolvedStr) { | ||
return true; | ||
} | ||
if (value && value.match(formatSpecifierRegex)) { | ||
var formatSpecifierReplacedStr = value.replace(formatSpecifierRegex, '.*'); | ||
var regex = new RegExp(formatSpecifierReplacedStr); | ||
return regex.test(str); | ||
var value = ''; | ||
var offset = 0; | ||
var partsPlaceholder = placeholderStr.split(' '); | ||
var partsResolved = resolvedStr.split(' '); | ||
partsPlaceholder.forEach(function (p, i) { | ||
// Placeholder | ||
if (p.startsWith('%')) { | ||
// Trim remaining words from current position to avoid premature matching from previous parts | ||
var remainingWords = partsResolved.slice(i + offset); | ||
var nextWordIndex = remainingWords.indexOf(partsPlaceholder[i + 1]); | ||
var hasNextWord = nextWordIndex !== -1; | ||
if (hasNextWord) { | ||
offset += nextWordIndex - 1; | ||
} | ||
var insert = remainingWords.slice(0, hasNextWord ? nextWordIndex : undefined).join(' '); | ||
value += "".concat(insert, " "); // Regular words | ||
} else { | ||
value += "".concat(p, " "); | ||
} | ||
}); | ||
return value.trimRight() === resolvedStr; | ||
} | ||
/** | ||
* Check whether a value contains a Format Specifier (printf placeholder) | ||
* | ||
* @see https://jestjs.io/docs/api#testeachtablename-fn-timeout | ||
* @see https://nodejs.org/api/util.html#utilformatformat-args | ||
* | ||
* @param argValue The string potentially containing a format specifier (e.g. 'Foo %s') | ||
* @param str The string that has replaced a format specifier with a tangible value (e.g. 'Foo Bar`) | ||
* | ||
* @returns Boolean: True if the strings matched (after considering the placeholder), or false if not. | ||
*/ | ||
function matchesStringWithFormatSpecifier(argValue, str) { | ||
var value = String(argValue); // Check whether value contains a printf format placeholder e.g. %s, %d etc | ||
if (value && value.match(/%(p|s|d|i|f|j|o|#)/g)) { | ||
return placeholderStringMatches(value, str); | ||
} else { | ||
// No format specifier placeholder | ||
return false; | ||
} | ||
}; | ||
} | ||
@@ -225,3 +267,3 @@ var checkForTemplateLiteralsWithPlaceholders = function checkForTemplateLiteralsWithPlaceholders(quasis, str) { | ||
// Eg: 'should contain %s' | ||
return checkForStringWithFormatSpecifier(arg.value, str); | ||
return matchesStringWithFormatSpecifier(arg.value, str); | ||
} | ||
@@ -228,0 +270,0 @@ } |
{ | ||
"name": "@atlaskit/codemod-utils", | ||
"version": "4.1.0", | ||
"version": "4.1.1", | ||
"sideEffects": false | ||
} |
@@ -138,14 +138,53 @@ function getNamedSpecifier(j, source, specifier, importName) { | ||
const checkForStringWithFormatSpecifier = (argValue, str) => { | ||
const value = String(argValue); | ||
const formatSpecifierRegex = /%[a-z]/g; | ||
export function placeholderStringMatches(placeholderStr, resolvedStr) { | ||
if (placeholderStr === resolvedStr) { | ||
return true; | ||
} | ||
if (value && value.match(formatSpecifierRegex)) { | ||
const formatSpecifierReplacedStr = value.replace(formatSpecifierRegex, '.*'); | ||
let regex = new RegExp(formatSpecifierReplacedStr); | ||
return regex.test(str); | ||
let value = ''; | ||
let offset = 0; | ||
const partsPlaceholder = placeholderStr.split(' '); | ||
const partsResolved = resolvedStr.split(' '); | ||
partsPlaceholder.forEach((p, i) => { | ||
// Placeholder | ||
if (p.startsWith('%')) { | ||
// Trim remaining words from current position to avoid premature matching from previous parts | ||
const remainingWords = partsResolved.slice(i + offset); | ||
const nextWordIndex = remainingWords.indexOf(partsPlaceholder[i + 1]); | ||
const hasNextWord = nextWordIndex !== -1; | ||
if (hasNextWord) { | ||
offset += nextWordIndex - 1; | ||
} | ||
const insert = remainingWords.slice(0, hasNextWord ? nextWordIndex : undefined).join(' '); | ||
value += `${insert} `; // Regular words | ||
} else { | ||
value += `${p} `; | ||
} | ||
}); | ||
return value.trimRight() === resolvedStr; | ||
} | ||
/** | ||
* Check whether a value contains a Format Specifier (printf placeholder) | ||
* | ||
* @see https://jestjs.io/docs/api#testeachtablename-fn-timeout | ||
* @see https://nodejs.org/api/util.html#utilformatformat-args | ||
* | ||
* @param argValue The string potentially containing a format specifier (e.g. 'Foo %s') | ||
* @param str The string that has replaced a format specifier with a tangible value (e.g. 'Foo Bar`) | ||
* | ||
* @returns Boolean: True if the strings matched (after considering the placeholder), or false if not. | ||
*/ | ||
export function matchesStringWithFormatSpecifier(argValue, str) { | ||
const value = String(argValue); // Check whether value contains a printf format placeholder e.g. %s, %d etc | ||
if (value && value.match(/%(p|s|d|i|f|j|o|#)/g)) { | ||
return placeholderStringMatches(value, str); | ||
} else { | ||
// No format specifier placeholder | ||
return false; | ||
} | ||
}; | ||
} | ||
@@ -167,3 +206,3 @@ const checkForTemplateLiteralsWithPlaceholders = (quasis, str) => { | ||
// Eg: 'should contain %s' | ||
return checkForStringWithFormatSpecifier(arg.value, str); | ||
return matchesStringWithFormatSpecifier(arg.value, str); | ||
} | ||
@@ -170,0 +209,0 @@ } |
{ | ||
"name": "@atlaskit/codemod-utils", | ||
"version": "4.1.0", | ||
"version": "4.1.1", | ||
"sideEffects": false | ||
} |
@@ -156,14 +156,53 @@ import _toConsumableArray from "@babel/runtime/helpers/toConsumableArray"; | ||
var checkForStringWithFormatSpecifier = function checkForStringWithFormatSpecifier(argValue, str) { | ||
var value = String(argValue); | ||
var formatSpecifierRegex = /%[a-z]/g; | ||
export function placeholderStringMatches(placeholderStr, resolvedStr) { | ||
if (placeholderStr === resolvedStr) { | ||
return true; | ||
} | ||
if (value && value.match(formatSpecifierRegex)) { | ||
var formatSpecifierReplacedStr = value.replace(formatSpecifierRegex, '.*'); | ||
var regex = new RegExp(formatSpecifierReplacedStr); | ||
return regex.test(str); | ||
var value = ''; | ||
var offset = 0; | ||
var partsPlaceholder = placeholderStr.split(' '); | ||
var partsResolved = resolvedStr.split(' '); | ||
partsPlaceholder.forEach(function (p, i) { | ||
// Placeholder | ||
if (p.startsWith('%')) { | ||
// Trim remaining words from current position to avoid premature matching from previous parts | ||
var remainingWords = partsResolved.slice(i + offset); | ||
var nextWordIndex = remainingWords.indexOf(partsPlaceholder[i + 1]); | ||
var hasNextWord = nextWordIndex !== -1; | ||
if (hasNextWord) { | ||
offset += nextWordIndex - 1; | ||
} | ||
var insert = remainingWords.slice(0, hasNextWord ? nextWordIndex : undefined).join(' '); | ||
value += "".concat(insert, " "); // Regular words | ||
} else { | ||
value += "".concat(p, " "); | ||
} | ||
}); | ||
return value.trimRight() === resolvedStr; | ||
} | ||
/** | ||
* Check whether a value contains a Format Specifier (printf placeholder) | ||
* | ||
* @see https://jestjs.io/docs/api#testeachtablename-fn-timeout | ||
* @see https://nodejs.org/api/util.html#utilformatformat-args | ||
* | ||
* @param argValue The string potentially containing a format specifier (e.g. 'Foo %s') | ||
* @param str The string that has replaced a format specifier with a tangible value (e.g. 'Foo Bar`) | ||
* | ||
* @returns Boolean: True if the strings matched (after considering the placeholder), or false if not. | ||
*/ | ||
export function matchesStringWithFormatSpecifier(argValue, str) { | ||
var value = String(argValue); // Check whether value contains a printf format placeholder e.g. %s, %d etc | ||
if (value && value.match(/%(p|s|d|i|f|j|o|#)/g)) { | ||
return placeholderStringMatches(value, str); | ||
} else { | ||
// No format specifier placeholder | ||
return false; | ||
} | ||
}; | ||
} | ||
@@ -187,3 +226,3 @@ var checkForTemplateLiteralsWithPlaceholders = function checkForTemplateLiteralsWithPlaceholders(quasis, str) { | ||
// Eg: 'should contain %s' | ||
return checkForStringWithFormatSpecifier(arg.value, str); | ||
return matchesStringWithFormatSpecifier(arg.value, str); | ||
} | ||
@@ -190,0 +229,0 @@ } |
{ | ||
"name": "@atlaskit/codemod-utils", | ||
"version": "4.1.0", | ||
"version": "4.1.1", | ||
"sideEffects": false | ||
} |
@@ -19,2 +19,15 @@ import core, { ASTPath, CallExpression, ImportDeclaration, ImportDefaultSpecifier, ImportSpecifier, JSXAttribute, JSXElement, Program, VariableDeclaration, VariableDeclarator } from 'jscodeshift'; | ||
declare const debug: (component: string) => (j: core.JSCodeshift, source: Collection<Node>) => void; | ||
export declare function placeholderStringMatches(placeholderStr: string, resolvedStr: string): boolean; | ||
/** | ||
* Check whether a value contains a Format Specifier (printf placeholder) | ||
* | ||
* @see https://jestjs.io/docs/api#testeachtablename-fn-timeout | ||
* @see https://nodejs.org/api/util.html#utilformatformat-args | ||
* | ||
* @param argValue The string potentially containing a format specifier (e.g. 'Foo %s') | ||
* @param str The string that has replaced a format specifier with a tangible value (e.g. 'Foo Bar`) | ||
* | ||
* @returns Boolean: True if the strings matched (after considering the placeholder), or false if not. | ||
*/ | ||
export declare function matchesStringWithFormatSpecifier(argValue: string | number | boolean | RegExp | null, str: string): boolean; | ||
declare const callExpressionArgMatchesString: (arg: CallExpression['arguments'][number], str: string) => boolean; | ||
@@ -21,0 +34,0 @@ declare const testMethodVariantEach: (path: ASTPath<CallExpression>, testMethods: Set<string>) => boolean; |
{ | ||
"name": "@atlaskit/codemod-utils", | ||
"version": "4.1.0", | ||
"version": "4.1.1", | ||
"author": "Atlassian Pty Ltd", | ||
@@ -5,0 +5,0 @@ "license": "Apache-2.0", |
107675
11.82%20
5.26%1896
6.76%