Skip to content

Commit

Permalink
fix(eslint-plugin): [member-delimiter-style] autofixer result is not …
Browse files Browse the repository at this point in the history
…as expected when comments after the delimiter with option `delimiter: 'none'` (#5029)
  • Loading branch information
holazz committed May 21, 2022
1 parent 9f3121b commit ed7b5f6
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 0 deletions.
26 changes: 26 additions & 0 deletions packages/eslint-plugin/src/rules/member-delimiter-style.ts
Expand Up @@ -35,6 +35,7 @@ interface MakeFixFunctionParams {
optsNone: boolean;
optsSemi: boolean;
lastToken: LastTokenType;
commentsAfterLastToken: LastTokenType | undefined;
missingDelimiter: boolean;
lastTokenLine: string;
isSingleLine: boolean;
Expand Down Expand Up @@ -74,10 +75,29 @@ const isLastTokenEndOfLine = (token: LastTokenType, line: string): boolean => {
return positionInLine === line.length - 1;
};

const isCommentsEndOfLine = (
token: LastTokenType,
comments: LastTokenType | undefined,
line: string,
): boolean => {
if (!comments) {
return false;
}

if (comments.loc.end.line > token.loc.end.line) {
return true;
}

const positionInLine = comments.loc.end.column;

return positionInLine === line.length;
};

const makeFixFunction = ({
optsNone,
optsSemi,
lastToken,
commentsAfterLastToken,
missingDelimiter,
lastTokenLine,
isSingleLine,
Expand All @@ -86,6 +106,7 @@ const makeFixFunction = ({
if (
optsNone &&
!isLastTokenEndOfLine(lastToken, lastTokenLine) &&
!isCommentsEndOfLine(lastToken, commentsAfterLastToken, lastTokenLine) &&
!isSingleLine
) {
return null;
Expand Down Expand Up @@ -206,6 +227,10 @@ export default util.createRule<Options, MessageIds>({
return;
}

const commentsAfterLastToken = sourceCode
.getCommentsAfter(lastToken)
.pop();

const sourceCodeLines = sourceCode.getLines();
const lastTokenLine = sourceCodeLines[lastToken?.loc.start.line - 1];

Expand Down Expand Up @@ -255,6 +280,7 @@ export default util.createRule<Options, MessageIds>({
optsNone,
optsSemi,
lastToken,
commentsAfterLastToken,
missingDelimiter,
lastTokenLine,
isSingleLine: opts.type === 'single-line',
Expand Down
36 changes: 36 additions & 0 deletions packages/eslint-plugin/tests/rules/member-delimiter-style.test.ts
Expand Up @@ -3586,5 +3586,41 @@ type Foo = {
},
],
},
{
code: `
type Foo = {
a: true; /** something */ /** some
thing */ b: true; /** something */ c: false; // something
}
`,
output: `
type Foo = {
a: true /** something */ /** some
thing */ b: true; /** something */ c: false // something
}
`,
options: [
{
multiline: { delimiter: 'none' },
},
],
errors: [
{
messageId: 'unexpectedSemi',
line: 3,
column: 11,
},
{
messageId: 'unexpectedSemi',
line: 4,
column: 20,
},
{
messageId: 'unexpectedSemi',
line: 4,
column: 47,
},
],
},
],
});

0 comments on commit ed7b5f6

Please sign in to comment.