Skip to content

Commit 06fec17

Browse files
authored
fix: skip location when deep cloning AST nodes (#3280)
1 parent 2d08997 commit 06fec17

6 files changed

Lines changed: 75 additions & 31 deletions

File tree

packages/amplify-data-construct/.jsii

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1375,6 +1375,19 @@
13751375
}
13761376
}
13771377
},
1378+
"aws-cdk-lib.aws_dsql": {
1379+
"targets": {
1380+
"dotnet": {
1381+
"package": "Amazon.CDK.AWS.DSQL"
1382+
},
1383+
"java": {
1384+
"package": "software.amazon.awscdk.services.dsql"
1385+
},
1386+
"python": {
1387+
"module": "aws_cdk.aws_dsql"
1388+
}
1389+
}
1390+
},
13781391
"aws-cdk-lib.aws_dynamodb": {
13791392
"targets": {
13801393
"dotnet": {
@@ -4109,5 +4122,5 @@
41094122
},
41104123
"types": {},
41114124
"version": "1.16.1",
4112-
"fingerprint": "RmejNooIcfMZHBiK6PXB+e2XH6v5bdARzogf0gvsW9Q="
4125+
"fingerprint": "/ibLSIPW39LJ28MtSyskiOwCoijSx8eBTWO+BhuVXwE="
41134126
}

packages/amplify-graphql-api-construct/.jsii

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1367,6 +1367,19 @@
13671367
}
13681368
}
13691369
},
1370+
"aws-cdk-lib.aws_dsql": {
1371+
"targets": {
1372+
"dotnet": {
1373+
"package": "Amazon.CDK.AWS.DSQL"
1374+
},
1375+
"java": {
1376+
"package": "software.amazon.awscdk.services.dsql"
1377+
},
1378+
"python": {
1379+
"module": "aws_cdk.aws_dsql"
1380+
}
1381+
}
1382+
},
13701383
"aws-cdk-lib.aws_dynamodb": {
13711384
"targets": {
13721385
"dotnet": {
@@ -9513,5 +9526,5 @@
95139526
}
95149527
},
95159528
"version": "1.20.1",
9516-
"fingerprint": "+StR3FqEAlc1IFZPMqvjS5CUe5YnkTLZS2rMNf1BHPA="
9529+
"fingerprint": "oNtoWovWTY04XL/toJV/tRugU0fwH6LEnKJEbslPXlM="
95179530
}

packages/amplify-graphql-transformer-core/src/__tests__/utils/directive-wrapper-test.ts renamed to packages/amplify-graphql-transformer-core/src/__tests__/utils/directive-wrapper.test.ts

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -186,5 +186,31 @@ describe('Transformer Core Util Tests', () => {
186186
expect(newArgs.timestamps.createdAt).toEqual('createdOn');
187187
expect(newArgs.timestamps.updatedAt).toEqual('updatedOn');
188188
});
189+
190+
it(': Should skip location when deep cloning', () => {
191+
// Cloning token locations is expensive and not useful (they're read only).
192+
// Some transformers are passing AST nodes to 'getArguments'
193+
// Assert that we don't clone them.
194+
const parsedDoc = parse(schema);
195+
const objNode = parsedDoc?.definitions?.[0] as ObjectTypeDefinitionNode;
196+
const modelDir = objNode?.directives?.[0] as DirectiveNode;
197+
const wrappedDir = new DirectiveWrapper(modelDir);
198+
199+
const argsWithASTNodes = {
200+
// add some args that are AST nodes.
201+
objNode,
202+
modelDir,
203+
// include common args to trigger deep merging.
204+
...cloneDeep(defaultArgs),
205+
};
206+
207+
const newArgs = wrappedDir.getArguments(argsWithASTNodes, { deepMergeArguments: true });
208+
// Assert that args were cloned.
209+
expect(newArgs.objNode === objNode).toBeFalsy();
210+
expect(newArgs.modelDir === modelDir).toBeFalsy();
211+
// Assert that locations were not cloned.
212+
expect(newArgs.objNode.loc === objNode.loc).toBeTruthy();
213+
expect(newArgs.modelDir.loc === modelDir.loc).toBeTruthy();
214+
});
189215
});
190216
});

packages/amplify-graphql-transformer-core/src/__tests__/utils/function-runtime-test.ts renamed to packages/amplify-graphql-transformer-core/src/__tests__/utils/function-runtime.test.ts

File renamed without changes.

packages/amplify-graphql-transformer-core/src/utils/directive-wrapper.ts

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,24 @@ export class DirectiveWrapper {
5555
{},
5656
);
5757
if (options?.deepMergeArguments && needsDeepMerge(defaultValue, argValues)) {
58-
return _.merge(_.cloneDeep(defaultValue), argValues);
58+
return _.merge(
59+
_.cloneDeepWith(defaultValue, (value) => {
60+
if (value instanceof Location) {
61+
// Skip cloning for 'Locations'
62+
// Some transformers are using AST nodes as arguments.
63+
// These AST nodes contain 'loc: Location' property which contains information
64+
// about where tokens were found in the schema during parsing.
65+
// This is a deeply nested structure for large schemas and cloning it may
66+
// hit recursive call limits.
67+
// Location is typed as read-only and doesn't change in post processing after parsing.
68+
// Therefore, is safe to keep original values.
69+
return value;
70+
}
71+
// Returning undefined let's Lodash know to use it's algorithm to clone.
72+
return undefined;
73+
}),
74+
argValues,
75+
);
5976
}
6077
return Object.assign(defaultValue, argValues);
6178
};

yarn.lock

Lines changed: 3 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -17022,16 +17022,7 @@ string-length@^4.0.1:
1702217022
char-regex "^1.0.2"
1702317023
strip-ansi "^6.0.0"
1702417024

17025-
"string-width-cjs@npm:string-width@^4.2.0":
17026-
version "4.2.3"
17027-
resolved "https://registry.npmjs.org/string-width/-/string-width-4.2.3.tgz#269c7117d27b05ad2e536830a8ec895ef9c6d010"
17028-
integrity sha512-wKyQRQpjJ0sIp62ErSZdGsjMJWsap5oRNihHhu6G7JVO/9jIB6UyevL+tXuOqrng8j/cxKTWyWUwvSTriiZz/g==
17029-
dependencies:
17030-
emoji-regex "^8.0.0"
17031-
is-fullwidth-code-point "^3.0.0"
17032-
strip-ansi "^6.0.1"
17033-
17034-
"string-width@^1.0.2 || 2 || 3 || 4", string-width@^4.1.0, string-width@^4.2.0, string-width@^4.2.3:
17025+
"string-width-cjs@npm:string-width@^4.2.0", "string-width@^1.0.2 || 2 || 3 || 4", string-width@^4.1.0, string-width@^4.2.0, string-width@^4.2.3:
1703517026
version "4.2.3"
1703617027
resolved "https://registry.npmjs.org/string-width/-/string-width-4.2.3.tgz#269c7117d27b05ad2e536830a8ec895ef9c6d010"
1703717028
integrity sha512-wKyQRQpjJ0sIp62ErSZdGsjMJWsap5oRNihHhu6G7JVO/9jIB6UyevL+tXuOqrng8j/cxKTWyWUwvSTriiZz/g==
@@ -17153,7 +17144,7 @@ string_decoder@~1.1.1:
1715317144
dependencies:
1715417145
safe-buffer "~5.1.0"
1715517146

17156-
"strip-ansi-cjs@npm:strip-ansi@^6.0.1":
17147+
"strip-ansi-cjs@npm:strip-ansi@^6.0.1", strip-ansi@^6.0.0, strip-ansi@^6.0.1:
1715717148
version "6.0.1"
1715817149
resolved "https://registry.npmjs.org/strip-ansi/-/strip-ansi-6.0.1.tgz#9e26c63d30f53443e9489495b2105d37b67a85d9"
1715917150
integrity sha512-Y38VPSHcqkFrCpFnQ9vuSXmquuv5oXOKpGeT6aGrr3o3Gc9AlVa6JBfUSOCnbxGGZF+/0ooI7KrPuUSztUdU5A==
@@ -17174,13 +17165,6 @@ strip-ansi@^5.1.0, strip-ansi@^5.2.0:
1717417165
dependencies:
1717517166
ansi-regex "^4.1.0"
1717617167

17177-
strip-ansi@^6.0.0, strip-ansi@^6.0.1:
17178-
version "6.0.1"
17179-
resolved "https://registry.npmjs.org/strip-ansi/-/strip-ansi-6.0.1.tgz#9e26c63d30f53443e9489495b2105d37b67a85d9"
17180-
integrity sha512-Y38VPSHcqkFrCpFnQ9vuSXmquuv5oXOKpGeT6aGrr3o3Gc9AlVa6JBfUSOCnbxGGZF+/0ooI7KrPuUSztUdU5A==
17181-
dependencies:
17182-
ansi-regex "^5.0.1"
17183-
1718417168
strip-ansi@^7.0.1, strip-ansi@^7.1.0:
1718517169
version "7.1.0"
1718617170
resolved "https://registry.npmjs.org/strip-ansi/-/strip-ansi-7.1.0.tgz#d5b6568ca689d8561370b0707685d22434faff45"
@@ -18188,7 +18172,7 @@ workerpool@^6.5.1:
1818818172
resolved "https://registry.npmjs.org/workerpool/-/workerpool-6.5.1.tgz#060f73b39d0caf97c6db64da004cd01b4c099544"
1818918173
integrity sha512-Fs4dNYcsdpYSAfVxhnl1L5zTksjvOJxtC5hzMNl+1t9B8hTJTdKDyZ5ju7ztgPy+ft9tBFXoOlDNiOT9WUXZlA==
1819018174

18191-
"wrap-ansi-cjs@npm:wrap-ansi@^7.0.0":
18175+
"wrap-ansi-cjs@npm:wrap-ansi@^7.0.0", wrap-ansi@^7.0.0:
1819218176
version "7.0.0"
1819318177
resolved "https://registry.npmjs.org/wrap-ansi/-/wrap-ansi-7.0.0.tgz#67e145cff510a6a6984bdf1152911d69d2eb9e43"
1819418178
integrity sha512-YVGIj2kamLSTxw6NsZjoBxfSwsn0ycdesmc4p+Q21c5zPuZ1pl+NfxVdxPtdHvmNVOQ6XSYG4AUtyt/Fi7D16Q==
@@ -18206,15 +18190,6 @@ wrap-ansi@^6.0.1, wrap-ansi@^6.2.0:
1820618190
string-width "^4.1.0"
1820718191
strip-ansi "^6.0.0"
1820818192

18209-
wrap-ansi@^7.0.0:
18210-
version "7.0.0"
18211-
resolved "https://registry.npmjs.org/wrap-ansi/-/wrap-ansi-7.0.0.tgz#67e145cff510a6a6984bdf1152911d69d2eb9e43"
18212-
integrity sha512-YVGIj2kamLSTxw6NsZjoBxfSwsn0ycdesmc4p+Q21c5zPuZ1pl+NfxVdxPtdHvmNVOQ6XSYG4AUtyt/Fi7D16Q==
18213-
dependencies:
18214-
ansi-styles "^4.0.0"
18215-
string-width "^4.1.0"
18216-
strip-ansi "^6.0.0"
18217-
1821818193
wrap-ansi@^8.1.0:
1821918194
version "8.1.0"
1822018195
resolved "https://registry.npmjs.org/wrap-ansi/-/wrap-ansi-8.1.0.tgz#56dc22368ee570face1b49819975d9b9a5ead214"

0 commit comments

Comments
 (0)