{"record":{"id":"badd33188ab26492","repo":"karatelabs/karate","slug":"invalid-sitename-this","errorCode":null,"errorMessage":"invalid ${siteName}: this","messagePattern":"invalid (.+?): this","errorType":"exception","errorClass":"ParserException","httpStatus":null,"severity":"error","filePath":"karate-js/src/main/java/io/karatelabs/parser/JsParser.java","lineNumber":1724,"sourceCode":"                    throw new ParserException(\"invalid \" + siteName + \": parenthesized destructuring pattern\");\n                }\n            }\n            n = inner;\n        }\n        switch (n.type) {\n            case REF_EXPR -> {\n                // REF_EXPR is single-arg arrow `x => ...` when its first child is an\n                // FN_ARROW_EXPR rather than the IDENT token; that form is invalid.\n                if (n.size() >= 1 && !n.getFirst().isToken()\n                        && n.getFirst().type == NodeType.FN_ARROW_EXPR) {\n                    throw new ParserException(\"invalid \" + siteName + \": arrow function\");\n                }\n                // `this` lexes as IDENT (see JsLexer.keywordOrIdent) but per spec\n                // the ThisExpression has AssignmentTargetType=invalid.\n                if (n.size() >= 1 && n.getFirst().isToken()\n                        && n.getFirst().token.type == IDENT\n                        && \"this\".equals(n.getFirst().getText())) {\n                    throw new ParserException(\"invalid \" + siteName + \": this\");\n                }\n            }\n            case REF_DOT_EXPR, REF_BRACKET_EXPR -> {\n                // Plain member access; a `?.` would have been caught earlier by\n                // the optional-chain branch above with a more specific message.\n                // `import.meta` is a meta-property whose AssignmentTargetType is\n                // invalid; `import` is not a reserved word in our lexer so it\n                // parses as a normal REF_DOT_EXPR — flag the literal shape here.\n                if (n.type == NodeType.REF_DOT_EXPR && isImportMeta(n)) {\n                    throw new ParserException(\"invalid \" + siteName + \": import.meta\");\n                }\n            }\n            case FN_CALL_EXPR -> {\n                // Web-compat carve-out (Annex B B.3.5): allow `f() = 1` in non-strict mode.\n                // Logical-assignment operators (||=, &&=, ??=) are ES2021 and the carve-out\n                // does NOT apply to them; the caller passes noCallCarveOut=true in that case.\n                if (noCallCarveOut) {\n                    throw new ParserException(\"invalid \" + siteName + \": call expression\");","sourceCodeStart":1706,"sourceCodeEnd":1742,"githubUrl":"https://github.com/karatelabs/karate/blob/a22eb90246d958d15a47bf436693d0121ad2812d/karate-js/src/main/java/io/karatelabs/parser/JsParser.java#L1706-L1742","documentation":"Per the ECMAScript spec, the `this` keyword is a ThisExpression whose AssignmentTargetType is invalid, so it cannot appear on the left-hand side of an assignment or in a binding position. The lexer emits `this` as IDENT, but the parser explicitly flags it here when validating assignment targets.","triggerScenarios":"Code like `this = obj`, `[this] = arr`, or `({a: this} = o)` where `this` occupies an assignment/destructuring target position.","commonSituations":"Mistakenly trying to rebind `this` (a habit from other languages such as Python where `self` is assignable); typo writing `this =` instead of `self = this`-style patterns.","solutions":["Assign to a normal variable instead of `this`","Use `const self = this;` if you need an alias for the current context","Use an arrow function to lexically capture `this` rather than rebinding it"],"exampleFix":"// before\nthis = {};\n// after\nconst self = this;\n// mutate properties instead: this.foo = 1;","handlingStrategy":"validation","validationCode":"if (/\\bthis\\s*=[^=]/.test(code)) {\n  throw new Error('`this` cannot be assigned');\n}","typeGuard":"function assignsThis(node) {\n  return node.type === 'AssignmentExpression' &&\n    node.left.type === 'ThisExpression';\n}","tryCatchPattern":"try {\n  karate.eval(code);\n} catch (e) {\n  if (String(e.message).includes(': this')) { /* rewrite to variable */ }\n  throw e;\n}","preventionTips":["Never assign to `this`; use `const self = this` for aliases","Use arrow functions to bind context instead of rebinding","Static-lint snippets for ThisExpression assignment targets"],"tags":["javascript","parser","this-keyword","assignment-target"],"backgroundTag":"invalid-argument-value","analyzedSha":"a22eb90246d958d15a47bf436693d0121ad2812d","analyzedAt":"2026-09-12T09:01:00.220Z","contentChangedAt":"2026-09-12T09:01:00.220Z","schemaVersion":2},"datasetVersion":"2026-09-19T12:17:13.211Z"}