Skip to content

Commit

Permalink
prevent-abbreviations: Fix shorthand import/export detection (#1215)
Browse files Browse the repository at this point in the history
  • Loading branch information
fisker committed Apr 27, 2021
1 parent 1276db5 commit a669e31
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 7 deletions.
8 changes: 5 additions & 3 deletions rules/utils/is-shorthand-export-identifier.js
@@ -1,6 +1,8 @@
'use strict';

module.exports = identifier =>
const isShorthandExportIdentifier = identifier =>
identifier.parent.type === 'ExportSpecifier' &&
identifier.parent.exported.name === identifier.name &&
identifier.parent.local.name === identifier.name;
identifier.parent.exported === identifier &&
identifier.parent.local === identifier;

module.exports = isShorthandExportIdentifier;
8 changes: 5 additions & 3 deletions rules/utils/is-shorthand-import-identifier.js
@@ -1,6 +1,8 @@
'use strict';

module.exports = identifier =>
const isShorthandImportIdentifier = identifier =>
identifier.parent.type === 'ImportSpecifier' &&
identifier.parent.imported.name === identifier.name &&
identifier.parent.local.name === identifier.name;
identifier.parent.imported === identifier &&
identifier.parent.local === identifier;

module.exports = isShorthandImportIdentifier;
55 changes: 54 additions & 1 deletion test/prevent-abbreviations.mjs
Expand Up @@ -1491,6 +1491,12 @@ runTest({
options: customOptions,
errors: createErrors()
},
{
code: 'import {err as err} from "err";',
output: 'import {err as error} from "err";',
options: customOptions,
errors: createErrors()
},
{
code: outdent`
import {
Expand Down Expand Up @@ -1588,6 +1594,18 @@ runTest({
errors: createErrors()
},

{
code: outdent`
let err;
export {err as err};
`,
output: outdent`
let error;
export {error as err};
`,
errors: createErrors()
},

noFixingTestCase({
code: 'export const err = {}',
errors: createErrors()
Expand Down Expand Up @@ -1773,7 +1791,24 @@ runTest.babel({
`,
options: checkPropertiesOptions,
errors: createErrors()
})
}),
{
code: 'import {err as err} from "err";//2',
output: 'import {err as error} from "err";//2',
options: customOptions,
errors: createErrors()
},
{
code: outdent`
let err;
export {err as err};//2
`,
output: outdent`
let error;
export {error as err};//2
`,
errors: createErrors()
}
]
});

Expand Down Expand Up @@ -1880,6 +1915,24 @@ runTest.typescript({
export type PreloadProps<TExtraProperties = null> = {};
`,
errors: [...createErrors(), ...createErrors()]
},

{
code: 'import {err as err} from "err";//',
output: 'import {err as error} from "err";//',
options: customOptions,
errors: createErrors()
},
{
code: outdent`
let err;
export {err as err};//
`,
output: outdent`
let error;
export {error as err};//
`,
errors: createErrors()
}
]
});

0 comments on commit a669e31

Please sign in to comment.