Skip to content

Commit

Permalink
Fix some codemod bugs (#12126)
Browse files Browse the repository at this point in the history
  • Loading branch information
jerelmiller authored Nov 14, 2024
1 parent 431fa33 commit d10d702
Show file tree
Hide file tree
Showing 5 changed files with 124 additions and 29 deletions.
5 changes: 5 additions & 0 deletions .changeset/brown-readers-smash.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@apollo/client": patch
---

Maintain the existing document if its unchanged by the codemod and move to more naive whitespace formatting
5 changes: 5 additions & 0 deletions .changeset/slimy-points-end.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@apollo/client": patch
---

Ensure documents unchanged by the codemod are left untouched.
4 changes: 2 additions & 2 deletions .size-limits.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{
"dist/apollo-client.min.cjs": 41576,
"import { ApolloClient, InMemoryCache, HttpLink } from \"dist/index.js\" (production)": 34364
"dist/apollo-client.min.cjs": 41573,
"import { ApolloClient, InMemoryCache, HttpLink } from \"dist/index.js\" (production)": 34361
}
81 changes: 81 additions & 0 deletions scripts/codemods/data-masking/examples/queries.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,3 +29,84 @@ export const GetCurrentUser = gql`
${CurrentUserFieldsFragment}
`;

export const SKIP_TO_NEXT_MUTATION = gql`
mutation SkipToNextMutation {
skipToNext {
playbackState {
progressMs
item {
__typename
... on Track {
id
name
album {
id
name
images {
url
}
}
artists {
id
name
}
}
... on Episode {
id
name
show {
id
name
images {
url
}
}
}
}
}
}
}
`;

export const SKIP_TO_NEXT_MUTATION_WITH_FRAGMENT = gql`
mutation SkipToNextMutation {
skipToNext {
playbackState {
progressMs
item {
__typename
... on Track {
id
name
album {
id
name
images {
url
}
}
artists {
id
name
}
...TrackItem_track
}
... on Episode {
id
name
show {
id
name
images {
url
}
}
}
}
}
}
}
`;
58 changes: 31 additions & 27 deletions scripts/codemods/data-masking/unmask.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,9 +67,15 @@ const transform: Transform = function transform(file, api, options) {
return templateElement;
}

const query = applyWhitepaceFromOriginalQuery(
const modifiedDocument = addUnmaskDirective(document, mode);

if (modifiedDocument === document) {
return templateElement;
}

const query = applyIndentationFromOriginalQuery(
queryString,
print(addUnmaskDirective(document, mode))
modifiedDocument
);

return j.templateElement(
Expand All @@ -91,31 +97,29 @@ function parseDocument(source: string) {
}
}

function applyWhitepaceFromOriginalQuery(source: string, printed: string) {
let firstNonWhitespaceLineNumber: number | null = null;
const printedLines = printed.split("\n");

return source
.split("\n")
.map((line, idx) => {
if (line.match(/^\s*$/)) {
return line;
}

if (firstNonWhitespaceLineNumber === null) {
firstNonWhitespaceLineNumber = idx;
}

const leading = getMatch(line, LEADING_WHITESPACE);
const trailing = getMatch(line, TRAILING_WHITESPACE);

const printedLine = printedLines[idx - firstNonWhitespaceLineNumber];
const printedLeading = getMatch(printedLine, LEADING_WHITESPACE);
const totalWhitespace = leading.length - printedLeading.length;

return leading.slice(0, totalWhitespace) + printedLine + trailing;
})
.join("\n");
function applyIndentationFromOriginalQuery(
source: string,
document: DocumentNode
) {
const lines = source.split("\n");
const locationOffset = document.loc!.source.locationOffset.line;

const leadingWhitespace = getMatch(source, /^[\s]*(?=\S)/);
const trailingWhitespace = getMatch(source, TRAILING_WHITESPACE);
const indentation = getMatch(lines[locationOffset], LEADING_WHITESPACE);

return (
leadingWhitespace +
print(document)
.split("\n")
.map((line, idx) => {
// `leadingWhitespace` will contain the whitespace needed for the
// first line so we can skip adding it
return idx === 0 ? line : indentation + line;
})
.join("\n") +
trailingWhitespace
);
}

function addUnmaskDirective(document: DocumentNode, mode: string | undefined) {
Expand Down

0 comments on commit d10d702

Please sign in to comment.