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

feat(eslint-plugin): [keyword-spacing] Support spacing in import-type syntax #5977

32 changes: 32 additions & 0 deletions packages/eslint-plugin/src/rules/keyword-spacing.ts
@@ -1,3 +1,4 @@
import type { TSESTree } from '@typescript-eslint/utils';
import { AST_TOKEN_TYPES } from '@typescript-eslint/utils';

import * as util from '../util';
Expand Down Expand Up @@ -50,6 +51,37 @@ export default util.createRule<Options, MessageIds>({
// make sure to reset the type afterward so we don't permanently mutate the AST
asToken.type = oldTokenType;
},
'ImportDeclaration[importKind=type]'(
node: TSESTree.ImportDeclaration,
): void {
const typeToken = sourceCode.getFirstToken(node, { skip: 1 })!;
const punctuatorToken = sourceCode.getTokenAfter(typeToken)!;
const spacesBetweenTypeAndPunctuator =
punctuatorToken.range[0] - typeToken.range[1];
if (context.options[0].after && spacesBetweenTypeAndPunctuator === 0) {
context.report({
loc: punctuatorToken.loc,
messageId: 'expectedBefore',
data: { value: punctuatorToken.value },
fix(fixer) {
return fixer.insertTextBefore(punctuatorToken, ' ');
},
});
}
if (!context.options[0].after && spacesBetweenTypeAndPunctuator > 0) {
context.report({
loc: punctuatorToken.loc,
messageId: 'unexpectedBefore',
data: { value: punctuatorToken.value },
fix(fixer) {
return fixer.removeRange([
typeToken.range[1],
typeToken.range[1] + spacesBetweenTypeAndPunctuator,
]);
},
});
}
},
};
},
});
42 changes: 40 additions & 2 deletions packages/eslint-plugin/tests/rules/keyword-spacing.test.ts
Expand Up @@ -12,8 +12,8 @@ import { RuleTester } from '../RuleTester';
// Helpers
//------------------------------------------------------------------------------

const BOTH = { before: true, after: true };
const NEITHER = { before: false, after: false };
const BOTH: Options[0] = { before: true, after: true };
const NEITHER: Options[0] = { before: false, after: false };
JoshuaKGoldberg marked this conversation as resolved.
Show resolved Hide resolved

/**
* Creates an option object to test an 'overrides' option.
Expand Down Expand Up @@ -114,6 +114,16 @@ ruleTester.run('keyword-spacing', rule, {
options: [{ overrides: { as: {} } }],
parserOptions: { ecmaVersion: 6, sourceType: 'module' },
},
{
code: 'import type { foo } from "foo";',
options: [{ overrides: { as: {} } }],
parserOptions: { ecmaVersion: 6, sourceType: 'module' },
},
{
code: "import type * as Foo from 'foo'",
options: [{ overrides: { as: {} } }],
parserOptions: { ecmaVersion: 6, sourceType: 'module' },
},
],
invalid: [
//----------------------------------------------------------------------
Expand Down Expand Up @@ -152,5 +162,33 @@ ruleTester.run('keyword-spacing', rule, {
parserOptions: { ecmaVersion: 6, sourceType: 'module' },
errors: expectedAfter('as'),
},
{
code: 'import type{ foo } from "foo";',
output: 'import type { foo } from "foo";',
options: [{ after: true, before: true }],
parserOptions: { ecmaVersion: 6, sourceType: 'module' },
errors: [{ messageId: 'expectedBefore', data: { value: '{' } }],
},
{
code: 'import type { foo } from"foo";',
output: 'import type{ foo } from"foo";',
options: [{ after: false, before: true }],
parserOptions: { ecmaVersion: 6, sourceType: 'module' },
errors: [{ messageId: 'unexpectedBefore', data: { value: '{' } }],
},
{
code: 'import type* as foo from "foo";',
output: 'import type * as foo from "foo";',
options: [{ after: true, before: true }],
parserOptions: { ecmaVersion: 6, sourceType: 'module' },
errors: [{ messageId: 'expectedBefore', data: { value: '*' } }],
},
{
code: 'import type * as foo from"foo";',
output: 'import type* as foo from"foo";',
options: [{ after: false, before: true }],
parserOptions: { ecmaVersion: 6, sourceType: 'module' },
errors: [{ messageId: 'unexpectedBefore', data: { value: '*' } }],
},
],
});