Utilizing Records and Tuples for Immutable Data Structures in JavaScript
Introduction to using Records and Tuples in JavaScript to create immutable data structures, reducing errors from unintended mutations.
0 likes
137 views
Rule Content
{
"title": "Utilizing Records and Tuples for Immutable Data Structures in JavaScript",
"description": "Enforce the use of Records and Tuples to create deeply immutable data structures, preventing unintended mutations and enhancing code reliability.",
"category": "JavaScript Cursor Rules",
"rules": [
{
"id": "prefer-records-tuples",
"description": "Prefer using Records (#{}) and Tuples (#[]) over Objects ({}) and Arrays ([]) for immutable data structures.",
"severity": "warning",
"pattern": [
{
"type": "ObjectExpression",
"guard": "isImmutableObject"
},
{
"type": "ArrayExpression",
"guard": "isImmutableArray"
}
],
"message": "Consider using a Record (#{}) or Tuple (#[]) for immutable data structures to prevent unintended mutations."
},
{
"id": "no-mutable-values-in-records-tuples",
"description": "Ensure that Records and Tuples contain only primitive values or other Records and Tuples.",
"severity": "error",
"pattern": [
{
"type": "RecordExpression",
"guard": "containsMutableValues"
},
{
"type": "TupleExpression",
"guard": "containsMutableValues"
}
],
"message": "Records and Tuples must only contain primitive values or other Records and Tuples."
},
{
"id": "no-mutation-of-records-tuples",
"description": "Disallow any attempts to mutate Records and Tuples after their creation.",
"severity": "error",
"pattern": [
{
"type": "AssignmentExpression",
"left": {
"type": "MemberExpression",
"object": {
"type": "RecordExpression"
}
}
},
{
"type": "AssignmentExpression",
"left": {
"type": "MemberExpression",
"object": {
"type": "TupleExpression"
}
}
}
],
"message": "Records and Tuples are immutable; modifying their properties or elements is not allowed."
}
],
"functions": {
"isImmutableObject": "function(node) { /* logic to determine if object is intended to be immutable */ }",
"isImmutableArray": "function(node) { /* logic to determine if array is intended to be immutable */ }",
"containsMutableValues": "function(node) { /* logic to check if node contains mutable values */ }"
}
}