Skip to content

Commit cf39255

Browse files
authored
fix: respect semicolon (#841)
1 parent 513afb3 commit cf39255

File tree

2 files changed

+9
-3
lines changed

2 files changed

+9
-3
lines changed

packages/plugins/eslint-plugin-react-x/src/rules/prefer-react-namespace-import.spec.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,11 @@ ruleTester.run(RULE_NAME, rule, {
9595
},
9696
},
9797
},
98+
{
99+
code: `import React from 'react'`,
100+
errors: [{ type: AST_NODE_TYPES.ImportDeclaration, messageId: "preferReactNamespaceImport" }],
101+
output: `import * as React from 'react'`,
102+
},
98103
],
99104
valid: [
100105
{

packages/plugins/eslint-plugin-react-x/src/rules/prefer-react-namespace-import.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,19 +37,20 @@ export default createRule<[], MessageID>({
3737
const isTypeImport = node.parent.importKind === "type";
3838
const importStringPrefix = `import${isTypeImport ? " type" : ""}`;
3939
const importSourceQuoted = `${quote}${importSource}${quote}`;
40+
const sourceCode = context.sourceCode.getText(node.parent);
41+
const semiColon = sourceCode.endsWith(";") ? ";" : "";
4042
if (!hasOtherSpecifiers) {
4143
return fixer.replaceText(
4244
node.parent,
43-
`${importStringPrefix} * as ${node.local.name} from ${importSourceQuoted};`,
45+
`${importStringPrefix} * as ${node.local.name} from ${importSourceQuoted}${semiColon}`,
4446
);
4547
}
4648

4749
// remove the default specifier and prepend the namespace import specifier
48-
const sourceCode = context.sourceCode.getText(node.parent);
4950
const specifiers = sourceCode.slice(sourceCode.indexOf("{"), sourceCode.indexOf("}") + 1);
5051
return fixer.replaceText(
5152
node.parent,
52-
`${importStringPrefix} * as ${node.local.name} from ${importSourceQuoted};\n${importStringPrefix} ${specifiers} from ${importSourceQuoted};`,
53+
`${importStringPrefix} * as ${node.local.name} from ${importSourceQuoted}${semiColon}\n${importStringPrefix} ${specifiers} from ${importSourceQuoted}${semiColon}`,
5354
);
5455
},
5556
});

0 commit comments

Comments
 (0)