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

updated import-scope #345

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 35 additions & 1 deletion src/rules/import-scope.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,40 @@ const isMethodImport = name => getMethodImportFromName(name) && !includes(name,
const isMethodPackageImport = name => getMethodImportFromName(name) && includes(name, '.')
const allImportsAreOfType = (node, types) => every(node.specifiers, specifier => includes(types, specifier.type))

/**
* Creates a fix(fixer) method for `full` imports
* @param {*} context
* @param {*} node
* @returns
*/
const createFullFix = (context, node) => {
const sourceCode = context.getSourceCode();
const text = sourceCode.getText(node);
if ( !/.*(lodash)';$/.test(text) ) {
return () => [];
}

const regexp = new RegExp(/(\w+)/g);
const matches = text.match(regexp);
if ( !matches ) {
return () => [];
}

const libs = matches
.filter( match => !['import', 'from', 'lodash'].includes(match))
if (!libs || libs.length < 1) {
return () => [];
}

const imports = libs
.sort()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please fix the formatting

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi there - (no idea what I'm doing) - by "fix the formatting" you mean what exactly? The lint passes. So, I am not sure what needs to be fixed. Thank you for any clarification.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nvm, I'll fix it

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @idok -

  1. The .eslintrc.json needed root: true in order for the linter to work. From there, I was able to lint -- --fix

  2. I added a test suite, package-to-method for import-scope.

Let me know if there is more work to be done, or if I am completely off track. Thank you for your patience.

.map(lib => `import ${lib} from 'lodash/${lib}';`)
.join('\n');

return (fixer) => [fixer.insertTextAfter(node, imports), fixer.remove(node)];
}


module.exports = {
meta: {
type: 'problem',
Expand All @@ -54,7 +88,7 @@ module.exports = {
}
} else if ((isMethodImport(node.source.value) && importType !== 'method') ||
(isMethodPackageImport(node.source.value) && importType !== 'method-package')) {
context.report({node, message: messages[importType]})
context.report({node, message: messages[importType], fix: createFullFix(context, node)})
}
},
VariableDeclarator(node) {
Expand Down