Skip to content

Commit

Permalink
starting point for adding allow/deny list options to no-namespace
Browse files Browse the repository at this point in the history
  • Loading branch information
chimericdream committed Oct 3, 2020
1 parent c51b6a9 commit 7b745bd
Show file tree
Hide file tree
Showing 2 changed files with 118 additions and 1 deletion.
48 changes: 47 additions & 1 deletion src/rules/no-namespace.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,40 @@ module.exports = {
url: docsUrl('no-namespace'),
},
fixable: 'code',
schema: [],
schema: [
{
oneOf: [
{
type: 'object',
properties: {
allowList: {
type: 'array',
},
},
additionalProperties: false,
},
{
type: 'object',
properties: {
denyList: {
type: 'array',
},
},
additionalProperties: false,
},
],
},
],
},

create: function (context) {
return {
'ImportNamespaceSpecifier': function (node) {
const options = context.options[0] || {}
const [mode, identifiers] = Array.isArray(options.denyList)
? ['deny', options.denyList]
: ['allow', options.allowList || []]

const scopeVariables = context.getScope().variables
const namespaceVariable = scopeVariables.find((variable) =>
variable.defs[0].node === node
Expand All @@ -31,6 +59,10 @@ module.exports = {
const namespaceIdentifiers = namespaceReferences.map(reference => reference.identifier)
const canFix = namespaceIdentifiers.length > 0 && !usesNamespaceAsObject(namespaceIdentifiers)

if (!shouldReport(mode, identifiers, namespaceIdentifiers)) {
return
}

context.report({
node,
message: `Unexpected namespace import.`,
Expand Down Expand Up @@ -87,6 +119,20 @@ module.exports = {
},
}

/**
*
* @param {'deny' | 'allow'} mode
* @param {string[]} list
* @param {TSESTree.Identifier[]} namespaceIdentifiers
*/
function shouldReport(mode, list, identifiers) {
for (const identifier of identifiers) {
if (list.includes(identifier)) {
return mode === 'deny'
}
}
}

/**
* @param {Identifier[]} namespaceIdentifiers
* @returns {boolean} `true` if the namespace variable is more than just a glorified constant
Expand Down
71 changes: 71 additions & 0 deletions tests/src/rules/no-namespace.js
Original file line number Diff line number Diff line change
Expand Up @@ -78,9 +78,80 @@ ruleTester.run('no-namespace', require('rules/no-namespace'), {
{ code: 'import { a, b } from \'./foo\';', parserOptions: { ecmaVersion: 2015, sourceType: 'module' } },
{ code: 'import bar from \'bar\';', parserOptions: { ecmaVersion: 2015, sourceType: 'module' } },
{ code: 'import bar from \'./bar\';', parserOptions: { ecmaVersion: 2015, sourceType: 'module' } },
{
code: 'import * as foo from \'foo\';',
options: [{
denyList: ['bar'],
}],
parserOptions: { ecmaVersion: 2015, sourceType: 'module' },
},
{
code: 'import * as bar from \'foo\';',
options: [{
denyList: ['bar'],
}],
parserOptions: { ecmaVersion: 2015, sourceType: 'module' },
},
{
code: 'import * as foo from \'foo\';',
options: [{
allowList: ['foo'],
}],
parserOptions: { ecmaVersion: 2015, sourceType: 'module' },
},
{
code: 'import * as lib from \'some-lib\';',
options: [{
allowList: ['some-lib'],
}],
parserOptions: { ecmaVersion: 2015, sourceType: 'module' },
},
{
code: 'import * as scopedLib from \'@scoped/lib\';',
options: [{
allowList: ['@scoped/lib'],
}],
parserOptions: { ecmaVersion: 2015, sourceType: 'module' },
},
],

invalid: [
test({
code: 'import * as foo from \'foo\';',
output: 'import * as foo from \'foo\';',
options: [{
allowList: ['bar'],
}],
errors: [ {
line: 1,
column: 8,
message: ERROR_MESSAGE,
} ],
}),
test({
code: 'import * as bar from \'foo\';',
output: 'import * as bar from \'foo\';',
options: [{
allowList: ['bar'],
}],
errors: [ {
line: 1,
column: 8,
message: ERROR_MESSAGE,
} ],
}),
test({
code: 'import * as foo from \'foo\';',
output: 'import * as foo from \'foo\';',
options: [{
denyList: ['foo'],
}],
errors: [ {
line: 1,
column: 8,
message: ERROR_MESSAGE,
} ],
}),
test({
code: 'import * as foo from \'foo\';',
output: 'import * as foo from \'foo\';',
Expand Down

0 comments on commit 7b745bd

Please sign in to comment.