mirror of
https://github.com/vercel-labs/scriptc.git
synced 2026-10-24 06:14:01 +08:00
46 lines
1.2 KiB
JavaScript
46 lines
1.2 KiB
JavaScript
// The commander shape in miniature: a getter/setter method whose declared
|
|
// OVERLOADS (index.d.ts) say `name(): string` / `name(v): this`, while the
|
|
// JS body infers the union `string | Chainy`. --npm-static projects these
|
|
// safe overload groups onto this exported runtime class before TS 7 parses
|
|
// it, preserving precise calls while compiling the body below.
|
|
'use strict';
|
|
|
|
class Chainy {
|
|
constructor() {
|
|
this.parent = null;
|
|
this._name = '';
|
|
this._tag = '';
|
|
}
|
|
name(v) {
|
|
if (v === undefined) return this._name;
|
|
this._name = v;
|
|
return this;
|
|
}
|
|
tag(v) {
|
|
if (v === undefined) return this._tag;
|
|
this._tag = v;
|
|
return this;
|
|
}
|
|
render() {
|
|
return this._name + '#' + this._tag;
|
|
}
|
|
readOwnName() {
|
|
const read = (value) => value.name();
|
|
return read(this);
|
|
}
|
|
/** @param {Array} args */
|
|
findOwnName(args) {
|
|
const current = args.length > 1 ? this : null;
|
|
return current && args.find((arg) => current.name() === arg);
|
|
}
|
|
ancestorNames() {
|
|
const names = [];
|
|
for (let current = this; current; current = current.parent) {
|
|
names.push(current.name());
|
|
}
|
|
return names;
|
|
}
|
|
}
|
|
|
|
module.exports = { Chainy };
|