Skip to content

Commit

Permalink
feat: take alternate path when line length is greater than printWidth
Browse files Browse the repository at this point in the history
  • Loading branch information
tylerturdenpants committed Jun 29, 2020
1 parent 0d6b060 commit 09c4440
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 36 deletions.
65 changes: 31 additions & 34 deletions lib/rules/decorator-position.js
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ module.exports = {
//
// NOTE: implementing this will need a breaking change
// {
// printWidth: 50,
// properties: 'prefer-inline',
// methods: 'above',
// overrides: {
Expand Down Expand Up @@ -204,8 +205,6 @@ function placeDecoratorsBesideProperty(context, node, options) {
continue;
}
const config = normalizeConfig(decoratorConfig, INTENT.SAME_LINE);
const info = decoratorInfo(node, config);

const decorator = node.decorators[0];
const token = context.getSourceCode().getTokenAfter(decorator, { includeComments: true });

Expand All @@ -217,6 +216,16 @@ function placeDecoratorsBesideProperty(context, node, options) {
const totalLineLength = calculateTotalLineLength(context, node, token) + whitespaceLength;
const lessThanOrEqualToPrintWidth = totalLineLength <= Number(options.printWidth);

if (!lessThanOrEqualToPrintWidth) {
const forwardOptions = {
...options,
};
forwardOptions.overrides[ABOVE] = options.overrides[PREFER_INLINE];
placeDecoratorsAboveProperty(context, node, forwardOptions);
}

const info = decoratorInfo(node, config);

if (!info.needsTransform) {
continue;
}
Expand Down Expand Up @@ -249,40 +258,29 @@ function placeDecoratorsAboveProperty(context, node, options) {
const info = decoratorInfo(node, config);

const decorator = node.decorators[0];
const token = context.getSourceCode().getTokenAfter(decorator, { includeComments: true });

const whitespaceStart = decorator.range[1];
const whitespaceEnd = token.range[0] - 1;

const whitespaceLength = whitespaceEnd - whitespaceStart;
// eslint-disable-next-line no-unused-vars
const totalLineLength = calculateTotalLineLength(context, node, token) + whitespaceLength;
const greaterThanOrEqualToPrintWidth = true;

if (greaterThanOrEqualToPrintWidth) {
if (!info.needsTransform) {
continue;
}
if (!info.needsTransform) {
continue;
}

context.report({
node,
message: `Expected @${info.name} to be on the line above.`,
context.report({
node,
message: `Expected @${info.name} to be on the line above.`,

fix(fixer) {
const indentation = decorator.loc.start.column - 1;
const padding = indentation > 0 ? ' '.repeat(indentation) : '';
const token = context.getSourceCode().getTokenAfter(decorator, { includeComments: true });
fix(fixer) {
const indentation = decorator.loc.start.column - 1;
const padding = indentation > 0 ? ' '.repeat(indentation) : '';
const token = context.getSourceCode().getTokenAfter(decorator, { includeComments: true });

const whitespaceStart = decorator.range[1];
const whitespaceEnd = token.range[0] - 1;
const whitespaceStart = decorator.range[1];
const whitespaceEnd = token.range[0] - 1;

// delete the space(s) between the decorator and the
// decorated thing with a newline, matching the
// indentation of the decorator
return fixer.replaceTextRange([whitespaceStart, whitespaceEnd], `\n${padding}`);
},
});
}
// delete the space(s) between the decorator and the
// decorated thing with a newline, matching the
// indentation of the decorator
return fixer.replaceTextRange([whitespaceStart, whitespaceEnd], `\n${padding}`);
},
});
}
}

Expand Down Expand Up @@ -338,9 +336,8 @@ function lineLength(userOptions, filePath) {
const prettierOptions = Object.assign({}, prettierRcOptions, eslintPrettierOptions, {
filePath,
});
const r = Object.assign({}, prettierOptions, userOptions);
// console.log(r);
return r;

return Object.assign({}, prettierOptions, userOptions);
}

function normalizeConfig(config, intent) {
Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@
"lint:js": "eslint . --cache",
"start": "yarn run test:watch",
"test": "jest",
"test:debug": "node --inspect node_modules/.bin/jest --watch --runInBand",
"test:debug-watch": "node --inspect node_modules/.bin/jest --runInBand",
"test:debug": "node --inspect node_modules/.bin/jest --runInBand",
"test:debug-watch": "node --inspect node_modules/.bin/jest --watch --runInBand",
"test:coverage": "jest --coverage",
"test:watch": "jest --watchAll",
"update": "node ./scripts/update-rules.js",
Expand Down
37 changes: 37 additions & 0 deletions tests/lib/rules/decorator-position.js
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,28 @@ ruleTester.run('JS: decorator-position', rule, {
`,
options: [{ overrides: { 'prefer-inline': ['@foo'] } }],
},
{
code: stripIndent`
class Foo {
@foo('bizbangbarbazboo') fizz;
@action('bidgbarbazboo') fizz;
}
`,
options: [{ printWidth: 30 }],
},
{
code: stripIndent`
class Foo {
@foo('bizbangbarbazboo')
fizz;
@action('bidgbarbazboo')
fizz;
}
`,
options: [{ printWidth: 10 }],
},
{
code: stripIndent`
class Foo {
Expand All @@ -133,6 +155,21 @@ ruleTester.run('JS: decorator-position', rule, {
},
],
invalid: [
{
code: stripIndent`
class Foo {
@alias('foo.bar.baz.someReallyLongPropertyNameThatIsTooLongToBeInlineOrItBreaksPrettier') foo;
}
`,
options: [{ printWidth: 50 }],
errors: [{ message: 'Expected @alias to be on the line above.' }],
output: stripIndent`
class Foo {
@alias('foo.bar.baz.someReallyLongPropertyNameThatIsTooLongToBeInlineOrItBreaksPrettier')
foo;
}
`,
},
{
code: stripIndent`
class Foo {
Expand Down

0 comments on commit 09c4440

Please sign in to comment.