New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Refactor ShellString into a real class #651
Comments
So, the issue with creating a class that inherits from var util = require('util');
function ShellString(stdout, stderr, code) {
this._value = '' + stdout;
Object.defineProperty(this, 'length', {
get: function () {
return this._value.length;
},
});
this.stdout = stdout;
this.stderr = stderr;
this.code = code;
}
util.inherits(ShellString, String);
ShellString.prototype.toString = ShellString.prototype.valueOf = function () {
return this._value;
}; The above is basically the minimum viable implementation. This is significantly easier with ES6 semantics though: class ShellString extends String {
constructor(stdout, stderr, code) {
super(stdout);
this.stdout = stdout;
this.stderr = stderr;
this.code = code;
}
} Of course, using this method, we permanently lose compatibility with Node v0.12 and under. |
I just tried this out. I saw some errors using the // define ShellString class like you did
ShellString.prototype.foo = function () {
return 'hi';
};
// ...
var s = new ShellString();
s.foo(); // TypeError: s.foo is not a function (**on node v4, not v6**) Maybe we could do something with your first approach. We could try a for loop over all attributes, and wrap them with getters and setters. Although I'm not sure if |
Btw, to define methods on a class: class ShellString extends String {
constructor() {
...
}
foo() {
return 'hi';
}
} |
Can't you also do it via |
Bumping to v0.9 milestone |
Sounds like this is easier to fix when we drop v4 (#873). Let's postpone until then. |
Seems like shelljs are testing node v8 and have a min req on node v8 in pkg.json I think classes came to node v6 so it would be possible to modernize this lib into using classes, const and var and some other new things... would you be open to me sending some PR that modernizes this a bit? Also, what are your view on dropping suport for older node versions that have ended it's support? |
I would be open as long as it runs in node v8 without transpiling. If you're interested in modernizing the code more generally (to use ES6 / ES2015 features everywhere) could you please file a separate issue to track that?
In general, I'm OK with dropping support for unsupported node versions (based on https://nodejs.org/en/about/releases/). I'd like to keep our min version at v8 for the time being just because the last release (0.8.4) supports >=4 and I'd feel uncomfortable jumping straight to >=12 in the next release (v0.9). |
jumping from v4 to v8 wouldn't that require a major release? |
ShellString
is a huge hack right now, and isn't really implemented as a class with a prototype. This means it can't be modified at runtime, which significantly limits what plugins can do. We should refactor it into an extendable class (preferably one which inherits all string methods).We should also consider a
ShellArray
class for methods likels()
, to make implementation easier.This blocks #486
The text was updated successfully, but these errors were encountered: