Tslint: Unsure what object-literal-shorthand rule is reporting here

Created on 22 Aug 2016  路  4Comments  路  Source: palantir/tslint

I just updated to TSLint 3.15.0 and the new rule object-literal-shorthand seems to complain about this code:

const fullFilePath = sourceFile.fileName;
const lastSlashIdx = fullFilePath.lastIndexOf("/");
const filePath = fullFilePath.substr(0, lastSlashIdx + 1);
const fileName = fullFilePath.substr(lastSlashIdx + 1, fullFilePath.length);
const fileNameParts = fileName.split(".");
return {
    fileName: fileName,
    fileNameParts: fileNameParts,
    filePath: filePath
};

For each line in the return statement it throws: Expected property shorthand in object literal.

I'm not really sure what "shorthand" means in this context... and there is no documentation for this rule right now.


It seems that moving things from the variable declaration directly into the object will fix some of these warnings, but I can only do so much.

const fullFilePath = sourceFile.fileName;
const lastSlashIdx = fullFilePath.lastIndexOf("/");
const fileName = fullFilePath.substr(lastSlashIdx + 1, fullFilePath.length);
return {
    fileName: fileName,
    fileNameParts: fileName.split("."),
    filePath: fullFilePath.substr(0, lastSlashIdx + 1)
};

Doing that now only makes it throw one error for the first line of the return statement, but if I do what I think it's asking me to do here, it make my code more complex, which is not something I want to do.


This code satisfies the linter, but lines 3 & 5 are now duplicated :sob:

const fullFilePath = sourceFile.fileName;
const lastSlashIdx = fullFilePath.lastIndexOf("/");
const fileName = fullFilePath.substr(lastSlashIdx + 1, fullFilePath.length);
return {
    fileName: fullFilePath.substr(lastSlashIdx + 1, fullFilePath.length),
    fileNameParts: fileName.split("."),
    filePath: fullFilePath.substr(0, lastSlashIdx + 1)
};

I'll have to turn the rule off for now.

Question

Most helpful comment

yes. you would probably be best off satisfying the rule this way:

return {
    fileName,
    fileNameParts,
    filePath
};

All 4 comments

it's similar to this eslint rule: http://eslint.org/docs/rules/object-shorthand
more docs: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Object_initializer

we just updated the docs a few min ago: http://palantir.github.io/tslint/rules/object-literal-shorthand/

feel free to send PRs to improve that rule's metadata :)

So this is the intended behavior then?

yes. you would probably be best off satisfying the rule this way:

return {
    fileName,
    fileNameParts,
    filePath
};

ok, interesting. I think I understand now., I'll have to look into this more. Thanks & sorry to both with a non-bug!

Was this page helpful?
0 / 5 - 0 ratings