Skip to content

Commit

Permalink
use globs instead of regexp
Browse files Browse the repository at this point in the history
  • Loading branch information
Arkadii Berezkin committed Aug 6, 2021
1 parent 0c4e324 commit 7e191f5
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 7 deletions.
17 changes: 11 additions & 6 deletions src/rules/no-namespace.js
Expand Up @@ -3,6 +3,7 @@
* @author Radek Benkel
*/

import minimatch from 'minimatch';
import docsUrl from '../docsUrl';

//------------------------------------------------------------------------------
Expand All @@ -20,23 +21,27 @@ module.exports = {
schema: [{
type: 'object',
properties: {
ignoreFromPattern: {
type: 'string',
ignoreFromPatterns: {
type: 'array',
items: {
type: 'string',
},
},
},
}],
},

create: function (context) {
const firstOption = context.options[0] || {};
const ignoreFromPatternExpression =
firstOption.ignoreFromPattern &&
new RegExp(firstOption.ignoreFromPattern);
const ignoreFromPatterns = firstOption.ignoreFromPatterns;

return {
'ImportNamespaceSpecifier': function (node) {
if (
ignoreFromPatternExpression && ignoreFromPatternExpression.test(node.parent.source.value)
ignoreFromPatterns
&& ignoreFromPatterns.find(
glob => minimatch(node.parent.source.value, glob, { matchBase: true })
)
) {
return;
}
Expand Down
2 changes: 1 addition & 1 deletion tests/src/rules/no-namespace.js
Expand Up @@ -79,7 +79,7 @@ ruleTester.run('no-namespace', require('rules/no-namespace'), {
{ code: 'import bar from \'bar\';', parserOptions: { ecmaVersion: 2015, sourceType: 'module' } },
{ code: 'import bar from \'./bar\';', parserOptions: { ecmaVersion: 2015, sourceType: 'module' } },
{ code: 'import * as bar from \'./ignored-module.ext\';', parserOptions: { ecmaVersion: 2015, sourceType: 'module' }, options: [{
ignoreFromPattern: '\\S+\\.ext',
ignoreFromPatterns: ['*.ext'],
}] },
],

Expand Down

0 comments on commit 7e191f5

Please sign in to comment.