@scandipwa/eslint-plugin-scandipwa-guidelines
Advanced tools
Comparing version 1.7.0 to 1.8.0
@@ -6,46 +6,57 @@ /** | ||
const CLASS_DECLARATION = 'ClassDeclaration'; | ||
const FUNCTION_DECLARATION = 'FunctionDeclaration'; | ||
const VARIABLE_DECLARATION = 'VariableDeclaration'; | ||
const shouldGetExported = [ | ||
CLASS_DECLARATION, | ||
FUNCTION_DECLARATION, | ||
VARIABLE_DECLARATION, | ||
]; | ||
const shouldBeExported = (node) => { | ||
return ( | ||
node.type !== VARIABLE_DECLARATION || | ||
!node.declarations.find((one) => one.id.type === 'ObjectPattern') | ||
); | ||
}; | ||
const getName = (node) => { | ||
if ([CLASS_DECLARATION, FUNCTION_DECLARATION].includes(node.type)) { | ||
return node.id.name; | ||
} | ||
return 'This variable'; | ||
}; | ||
module.exports = { | ||
meta: { | ||
docs: { | ||
description: 'Everything declared in module on the first nesting level should be exported.', | ||
description: | ||
'Everything declared in module on the first nesting level should be exported.', | ||
category: 'Coding standard', | ||
recommended: false | ||
recommended: false, | ||
}, | ||
fixable: 'code' | ||
fixable: 'code', | ||
}, | ||
create: context => ({ | ||
VariableDeclaration(node) { | ||
const { parent, declarations: [{ id: { loc, name } }] } = node; | ||
const { type } = parent || {}; | ||
create: (context) => ({ | ||
Program(node) { | ||
const { body } = node; | ||
if (parent.type !== 'Program') { | ||
return; | ||
} | ||
if (type !== 'ExportNamedDeclaration') { | ||
context.report({ | ||
loc, | ||
message: `Variable ${name} must be exported (as non default) to allow proper extension.`, | ||
fix: fixer => fixer.insertTextBefore(node, 'export ') | ||
body | ||
.filter((levelOneNode) => shouldGetExported.includes(levelOneNode.type)) | ||
.map((exportable) => { | ||
if (shouldBeExported(exportable)) { | ||
context.report({ | ||
node: exportable, | ||
message: `${getName( | ||
exportable | ||
)} must be exported (as non default) to allow proper extension`, | ||
fix: (fixer) => fixer.insertTextBefore(exportable, 'export '), | ||
}); | ||
} | ||
}); | ||
} | ||
}, | ||
ClassDeclaration(node) { | ||
const { parent, id: { loc, name } } = node; | ||
const { type } = parent || {}; | ||
if (type !== 'ExportNamedDeclaration') { | ||
context.report({ | ||
loc, | ||
message: `Class ${name} must be exported (as non default) to allow proper extension.`, | ||
fix: fixer => { | ||
if (parent.type === 'Program') { | ||
fixer.insertTextBefore(node, 'export ') | ||
} | ||
} | ||
}); | ||
} | ||
} | ||
}) | ||
}), | ||
}; |
@@ -6,5 +6,2 @@ /** | ||
// eslint-disable-next-line fp/no-let, scandipwa-extensibility/no-non-extensible-components | ||
let classCount; | ||
module.exports = { | ||
@@ -15,22 +12,23 @@ meta: { | ||
category: 'Coding standard', | ||
recommended: false | ||
recommended: false, | ||
}, | ||
fixable: 'code' | ||
fixable: 'code', | ||
}, | ||
create: context => ({ | ||
Program() { | ||
classCount = 0; | ||
}, | ||
ClassDeclaration(node) { | ||
classCount += 1; | ||
if (classCount > 1) { | ||
context.report({ | ||
node, | ||
message: 'Only one class per file is allowed', | ||
fix: fixer => fixer.remove(node) | ||
create: (context) => ({ | ||
Program(node) { | ||
const classes = node.body | ||
.map((node) => node.type.match(/^Export/) ? node.declaration : node) | ||
.filter((val) => val && val.type === 'ClassDeclaration'); | ||
if (classes.length > 1) { | ||
classes.slice(1).forEach((redundantClass) => { | ||
context.report({ | ||
node: redundantClass, | ||
message: 'Only one class per file is allowed', | ||
}); | ||
}); | ||
} | ||
} | ||
}) | ||
}, | ||
}), | ||
}; |
@@ -88,13 +88,31 @@ /** | ||
return splitReverseFilePath.slice(1).reduce( | ||
(acc, cur, _, array) => { | ||
if (cur === 'app' || cur === 'sw') { | ||
// Mutate the initial array to break cycle | ||
array.splice(1); | ||
return acc; | ||
} | ||
const generatePluginPart = () => { | ||
const pluginRootIndex = context.getFilename().indexOf('/src/scandipwa/'); | ||
if (pluginRootIndex === -1) { | ||
return ''; | ||
} | ||
return [capitalise(cur), acc].filter(Boolean).join('/'); | ||
}, | ||
['JS', 'QUERY'].includes(postfix.toUpperCase()) ? '' : postfix | ||
return context.getFilename() | ||
.slice(0, pluginRootIndex) | ||
.split('/') | ||
.reverse() | ||
.slice(0, 2) | ||
.reverse() | ||
.join('/') | ||
.concat('/'); | ||
} | ||
return generatePluginPart().concat( | ||
splitReverseFilePath.slice(1).reduce( | ||
(acc, cur, _, array) => { | ||
if (cur === 'app' || cur === 'sw') { | ||
// Mutate the initial array to break cycle | ||
array.splice(1); | ||
return acc; | ||
} | ||
return [capitalise(cur), acc].filter(Boolean).join('/'); | ||
}, | ||
['JS', 'QUERY'].includes(postfix.toUpperCase()) ? '' : postfix | ||
) | ||
); | ||
@@ -116,2 +134,6 @@ }; | ||
if (node.type === 'ClassDeclaration') { | ||
return generateBaseNamespace(); | ||
} | ||
let stack = []; | ||
@@ -139,2 +161,9 @@ const collect = (node, namespaceContainer) => { | ||
const isPlugin = (node) => { | ||
return node && | ||
node.id && | ||
node.id.name && | ||
node.id.name.match(/[P|p]lugin/); | ||
} | ||
module.exports = { | ||
@@ -158,3 +187,3 @@ meta: { | ||
if (!namespace) { | ||
if (!namespace && !isPlugin(node)) { | ||
context.report({ | ||
@@ -161,0 +190,0 @@ node, |
{ | ||
"name": "@scandipwa/eslint-plugin-scandipwa-guidelines", | ||
"version": "1.7.0", | ||
"version": "1.8.0", | ||
"description": "Eslint rules for ScandiPWA", | ||
@@ -5,0 +5,0 @@ "keywords": [ |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
38256
28
916