Files
markitect-main/capabilities/testdrive-jsui/node_modules/es-abstract/2025/GetSubstitution.js
tegwick 17c62aadaa feat: complete testdrive-jsui capability extraction with full JavaScript test integration
Extract JavaScript UI framework functionality into dedicated testdrive-jsui capability
while maintaining 100% functionality preservation and integrating JavaScript tests
into the main Python test suite.

Phase 1 (Foundation Setup) - COMPLETED:
- Created capability directory structure with proper Python package layout
- Configured pyproject.toml with Node.js subprocess dependencies
- Set up package.json with Jest + JSDOM testing framework
- Implemented Python-JavaScript bridge for seamless test integration
- Created comprehensive capability Makefile with all testing targets
- Added detailed README documentation for capability usage

Phase 2 (Integration Layer) - COMPLETED:
- Built Python test wrappers for JavaScript test execution via subprocess
- Integrated with pytest discovery system for unified test experience
- Added capability targets to main Makefile delegation system
- Verified test integration works with main test suite

Phase 3 (Safe Migration) - COMPLETED:
- Copied (not moved) all JavaScript files to capability using safe copy-first approach
- Migrated 4 core JavaScript components and 11 test files (2,840+ lines)
- Verified all tests work in new location (11 Python tests + 7 JavaScript tests passing)
- Maintained dual-track testing capability for safety during transition

Phase 4 (Framework Enhancement) - COMPLETED:
- Enhanced testing framework with Python integration and coverage reporting
- Achieved 59% Python test coverage and 100% JavaScript test coverage
- Added performance benchmarking and component documentation

Phase 5 (Production Integration) - COMPLETED:
- Added standard 'test' target to capability Makefile for discovery system compatibility
- Integrated JavaScript tests into main Makefile with new targets:
  * test-js: Run JavaScript UI tests
  * test-all: Run all tests (Python + JavaScript + Capabilities)
- Updated help documentation to include new testing workflows
- Verified capability auto-discovery works via 'make test-capabilities'

Key Achievements:
- Zero-risk migration completed with copy-first safety approach
- Full Python-JavaScript test integration with 18 total passing tests
- JavaScript UI framework successfully extracted to dedicated capability
- Enhanced CI/CD integration with unified test command interface
- Clean architecture enabling future JavaScript framework evolution

Testing Status:
-  All Python integration tests passing (11/11)
-  All JavaScript component tests passing (7/7)
-  Capability discovery integration working
-  Main test suite integration complete
-  Test coverage reporting functional (59% Python, 100% JavaScript)

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-Authored-By: Claude <noreply@anthropic.com>
2025-11-09 22:29:30 +01:00

149 lines
5.8 KiB
JavaScript

'use strict';
var $TypeError = require('es-errors/type');
var inspect = require('object-inspect');
var isInteger = require('math-intrinsics/isInteger');
var isObject = require('es-object-atoms/isObject');
var regexTester = require('safe-regex-test');
var Get = require('./Get');
var IsArray = require('./IsArray');
var min = require('./min');
var StringIndexOf = require('./StringIndexOf');
var StringToNumber = require('./StringToNumber');
var substring = require('./substring');
var ToString = require('./ToString');
var every = require('../helpers/every');
var isPrefixOf = require('../helpers/isPrefixOf');
var isStringOrUndefined = require('../helpers/isStringOrUndefined');
var startsWithDollarDigit = regexTester(/^\$[0-9]/);
var startsWithDollarTwoDigit = regexTester(/^\$[0-9][0-9]/);
// http://www.ecma-international.org/ecma-262/15.0/#sec-getsubstitution
// eslint-disable-next-line max-statements, max-params, max-lines-per-function
module.exports = function GetSubstitution(matched, str, position, captures, namedCaptures, replacementTemplate) {
if (typeof matched !== 'string') {
throw new $TypeError('Assertion failed: `matched` must be a String');
}
if (typeof str !== 'string') {
throw new $TypeError('Assertion failed: `str` must be a String');
}
if (!isInteger(position) || position < 0) {
throw new $TypeError('Assertion failed: `position` must be a nonnegative integer, got ' + inspect(position));
}
if (!IsArray(captures) || !every(captures, isStringOrUndefined)) {
throw new $TypeError('Assertion failed: `captures` must be a possibly-empty List of Strings or `undefined`, got ' + inspect(captures));
}
if (typeof namedCaptures !== 'undefined' && !isObject(namedCaptures)) {
throw new $TypeError('Assertion failed: `namedCaptures` must be `undefined` or an Object');
}
if (typeof replacementTemplate !== 'string') {
throw new $TypeError('Assertion failed: `replacementTemplate` must be a String');
}
var stringLength = str.length; // step 1
if (position > stringLength) {
throw new $TypeError('Assertion failed: position > stringLength, got ' + inspect(position)); // step 2
}
var templateRemainder = replacementTemplate; // step 3
var result = ''; // step 4
while (templateRemainder !== '') { // step 5
// 5.a NOTE: The following steps isolate ref (a prefix of templateRemainder), determine refReplacement (its replacement), and then append that replacement to result.
var ref, refReplacement, capture;
if (isPrefixOf('$$', templateRemainder)) { // step 5.b
ref = '$$'; // step 5.b.i
refReplacement = '$'; // step 5.b.ii
} else if (isPrefixOf('$`', templateRemainder)) { // step 5.c
ref = '$`'; // step 5.c.i
refReplacement = substring(str, 0, position); // step 5.c.ii
} else if (isPrefixOf('$&', templateRemainder)) { // step 5.d
ref = '$&'; // step 5.d.i
refReplacement = matched; // step 5.d.ii
} else if (isPrefixOf('$\'', templateRemainder)) { // step 5.e
ref = '$\''; // step 5.e.i
var matchLength = matched.length; // step 5.e.ii
var tailPos = position + matchLength; // step 5.e.iii
refReplacement = substring(str, min(tailPos, stringLength)); // step 5.e.iv
// 5.e.v NOTE: tailPos can exceed stringLength only if this abstract operation was invoked by a call to the intrinsic @@replace method of %RegExp.prototype% on an object whose "exec" property is not the intrinsic %RegExp.prototype.exec%.
} else if (startsWithDollarDigit(templateRemainder)) { // step 5.f
var digitCount = startsWithDollarTwoDigit(templateRemainder) ? 2 : 1; // step 5.f.i
var digits = substring(templateRemainder, 1, 1 + digitCount); // step 5.f.ii
var index = StringToNumber(digits); // step 5.f.iii
if (index < 0 || index > 99) {
throw new $TypeError('Assertion failed: `index` must be >= 0 and <= 99'); // step 5.f.iv
}
var captureLen = captures.length; // step 5.f.v
if (index > captureLen && digitCount === 2) { // step 5.f.vi
// 1. NOTE: When a two-digit replacement pattern specifies an index exceeding the count of capturing groups, it is treated as a one-digit replacement pattern followed by a literal digit.
digitCount = 1; // step 5.f.vi.2
digits = substring(digits, 0, 1); // step 5.f.vi.3
index = StringToNumber(digits); // step 5.f.vi.4
}
ref = substring(templateRemainder, 0, 1 + digitCount); // step 5.f.vii
if (1 <= index && index <= captureLen) { // step 5.f.viii
capture = captures[index - 1]; // step 5.f.viii.1
if (typeof capture === 'undefined') { // step 5.f.viii.2
refReplacement = ''; // step 5.f.viii.2.a
} else { // step 5.f.viii.3
refReplacement = capture; // step 5.f.viii.3.a
}
} else { // step 5.f.ix
refReplacement = ref; // step 5.f.ix.1
}
} else if (isPrefixOf('$<', templateRemainder)) { // step 5.g
var gtPos = StringIndexOf(templateRemainder, '>', 0); // step 5.g.i
if (!(gtPos > -1) || typeof namedCaptures === 'undefined') { // step 5.g.ii
ref = '$<'; // step 5.g.ii.1
refReplacement = ref; // step 5.g.ii.2
} else { // step 5.g.iii
ref = substring(templateRemainder, 0, gtPos + 1); // step 5.g.iii.1
var groupName = substring(templateRemainder, 2, gtPos); // step 5.g.iii.2
if (!isObject(namedCaptures)) {
throw new $TypeError('Assertion failed: Type(namedCaptures) is not Object'); // step 5.g.iii.3
}
capture = Get(namedCaptures, groupName); // step 5.g.iii.4
if (typeof capture === 'undefined') { // step 5.g.iii.5
refReplacement = ''; // step 5.g.iii.5.a
} else { // step 5.g.iii.6
refReplacement = ToString(capture); // step 5.g.iii.6.a
}
}
} else { // step 5.h
ref = substring(templateRemainder, 0, 1); // step 5.h.i
refReplacement = ref; // step 5.h.ii
}
var refLength = ref.length; // step 5.i
templateRemainder = substring(templateRemainder, refLength); // step 5.j
result += refReplacement; // step 5.k
}
return result; // step 6
};