Skip to content

Commit

Permalink
feat(eslint-plugin): [keyword-spacing] Support spacing in import-type…
Browse files Browse the repository at this point in the history
… syntax (#5977)

* feat/issue5362-keyword-spacing---support-import-type-syntax

* uncomment

* Update packages/eslint-plugin/tests/rules/keyword-spacing.test.ts

Co-authored-by: Josh Goldberg <git@joshuakgoldberg.com>
  • Loading branch information
omril1 and JoshuaKGoldberg committed Nov 15, 2022
1 parent 8af1b4d commit 6a735e1
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 0 deletions.
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,
]);
},
});
}
},
};
},
});
38 changes: 38 additions & 0 deletions packages/eslint-plugin/tests/rules/keyword-spacing.test.ts
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: '*' } }],
},
],
});

0 comments on commit 6a735e1

Please sign in to comment.