Skip to content
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

fix(eslint-plugin): [member-delimiter-style] autofixer result is not as expected when comments after the delimiter with option delimiter: 'none' #5029

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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,
},
],
},
],
});