Skip to content

Commit fe84e80

Browse files
SimeonCXavier Le Cunff
authored andcommitted
Add detection for ES6 via import/export and some other test cases.
1 parent c9b286f commit fe84e80

File tree

2 files changed

+116
-4
lines changed

2 files changed

+116
-4
lines changed

lib/rules/function-component-definition.js

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -217,7 +217,7 @@ module.exports = {
217217
// --------------------------------------------------------------------------
218218
const validatePairs = [];
219219
let hasEs6OrJsx = false;
220-
return {
220+
const astHandlers = {
221221
FunctionDeclaration(node) {
222222
validatePairs.push([node, 'function-declaration']);
223223
},
@@ -230,13 +230,27 @@ module.exports = {
230230
VariableDeclaration(node) {
231231
hasEs6OrJsx = hasEs6OrJsx || node.kind === 'const' || node.kind === 'let';
232232
},
233-
JSXElement() {
234-
hasEs6OrJsx = true;
235-
},
236233
'Program:exit'() {
237234
if (hasEs6OrJsx) fileVarType = 'const';
238235
validatePairs.forEach((pair) => validate(pair[0], pair[1]));
239236
},
240237
};
238+
// if any of the following are detected, assume es6
239+
[
240+
'ImportDeclaration',
241+
'ExportNamedDeclaration',
242+
'ExportDefaultDeclaration',
243+
'ExportAllDeclaration',
244+
'ExportSpecifier',
245+
'ExportDefaultSpecifier',
246+
'JSXElement',
247+
'TSExportAssignment',
248+
'TSImportEqualsDeclaration',
249+
].forEach((es6Flag) => {
250+
astHandlers[es6Flag] = () => {
251+
hasEs6OrJsx = true;
252+
};
253+
});
254+
return astHandlers;
241255
}),
242256
};

tests/lib/rules/function-component-definition.js

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -479,6 +479,56 @@ ruleTester.run('function-component-definition', rule, {
479479
options: [{ namedComponents: 'function-expression' }],
480480
errors: [{ messageId: 'function-expression' }],
481481
},
482+
{
483+
code: `
484+
let Hello = (props) => {
485+
return <div/>;
486+
}
487+
`,
488+
output: `
489+
let Hello = function(props) {
490+
return <div/>;
491+
}
492+
`,
493+
options: [{ namedComponents: 'function-expression' }],
494+
errors: [{ messageId: 'function-expression' }],
495+
},
496+
{
497+
code: `
498+
let Hello;
499+
Hello = (props) => {
500+
return <div/>;
501+
}
502+
`,
503+
output: `
504+
let Hello;
505+
Hello = function(props) {
506+
return <div/>;
507+
}
508+
`,
509+
options: [{ namedComponents: 'function-expression' }],
510+
errors: [{ messageId: 'function-expression' }],
511+
},
512+
{
513+
code: `
514+
let Hello = (props) => {
515+
return <div/>;
516+
}
517+
Hello = function(props) {
518+
return <span/>;
519+
}
520+
`,
521+
output: `
522+
let Hello = function(props) {
523+
return <div/>;
524+
}
525+
Hello = function(props) {
526+
return <span/>;
527+
}
528+
`,
529+
options: [{ namedComponents: 'function-expression' }],
530+
errors: [{ messageId: 'function-expression' }],
531+
},
482532
{
483533
code: `
484534
function Hello(props) {
@@ -619,6 +669,54 @@ ruleTester.run('function-component-definition', rule, {
619669
errors: [{ messageId: 'function-expression' }],
620670
features: ['types'],
621671
},
672+
{
673+
code: `
674+
import * as React from 'react';
675+
function Hello(props: Test) {
676+
return React.createElement('div');
677+
}
678+
`,
679+
output: `
680+
import * as React from 'react';
681+
const Hello = function(props: Test) {
682+
return React.createElement('div');
683+
}
684+
`,
685+
options: [{ namedComponents: 'function-expression' }],
686+
errors: [{ messageId: 'function-expression' }],
687+
features: ['types'],
688+
},
689+
{
690+
code: `
691+
export function Hello(props: Test) {
692+
return React.createElement('div');
693+
}
694+
`,
695+
output: `
696+
export const Hello = function(props: Test) {
697+
return React.createElement('div');
698+
}
699+
`,
700+
options: [{ namedComponents: 'function-expression' }],
701+
errors: [{ messageId: 'function-expression' }],
702+
features: ['types'],
703+
},
704+
{
705+
code: `
706+
function Hello(props) {
707+
return React.createElement('div');
708+
}
709+
export default Hello;
710+
`,
711+
output: `
712+
const Hello = function(props) {
713+
return React.createElement('div');
714+
}
715+
export default Hello;
716+
`,
717+
options: [{ namedComponents: 'function-expression' }],
718+
errors: [{ messageId: 'function-expression' }],
719+
},
622720
{
623721
code: `
624722
var Hello = (props: Test) => {

0 commit comments

Comments
 (0)