Skip to content

Commit

Permalink
feat: update rules
Browse files Browse the repository at this point in the history
  • Loading branch information
splincode committed Jun 26, 2024
1 parent 5211151 commit 5ee8cbc
Show file tree
Hide file tree
Showing 15 changed files with 2,497 additions and 3,135 deletions.
5,428 changes: 2,404 additions & 3,024 deletions package-lock.json

Large diffs are not rendered by default.

13 changes: 10 additions & 3 deletions projects/eslint-plugin-experience/all.js
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ module.exports = {
},
overrides: [
{
files: ['*.ts'],
files: ['*.ts', '*.tsx', '*.js'],
parser: '@typescript-eslint/parser',
plugins: [
'@babel',
Expand Down Expand Up @@ -740,6 +740,12 @@ module.exports = {
'vars-on-top': 'error',
},
},
{
files: ['*.js'],
rules: {
'@typescript-eslint/explicit-function-return-type': 'off',
},
},
{
files: [
'*.component.ts',
Expand Down Expand Up @@ -951,8 +957,9 @@ module.exports = {

function projectJsonExist(filename) {
try {
const path = require('path').resolve(filename);
return require('fs').existsSync(path) ? filename : undefined;
const path = require('node:path').resolve(filename);

return require('node:fs').existsSync(path) ? filename : undefined;
} catch {
return undefined;
}
Expand Down
4 changes: 2 additions & 2 deletions projects/eslint-plugin-experience/configs/entry-points.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
const {globSync} = require('glob');
const {readFileSync} = require('fs');
const {readFileSync} = require('node:fs');

const allPackageJSONs = globSync(`**/package.json`, {
const allPackageJSONs = globSync('**/package.json', {
ignore: ['node_modules/**', 'dist/**'],
}).filter(path => !readJSON(path).private);
const packageNames = allPackageJSONs.map(path => readJSON(path).name);
Expand Down
17 changes: 8 additions & 9 deletions projects/eslint-plugin-experience/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@
"@babel/eslint-parser": "^7.24.7",
"@babel/eslint-plugin": "^7.24.7",
"@typescript-eslint/eslint-plugin": "^7.12.0",
"@typescript-eslint/parser": "^7.12.0",
"@typescript-eslint/types": "^7.12.0",
"@typescript-eslint/typescript-estree": "^7.12.0",
"@typescript-eslint/utils": "^7.12.0",
"@typescript-eslint/parser": "^7.14.1",
"@typescript-eslint/types": "^7.14.1",
"@typescript-eslint/typescript-estree": "^7.14.1",
"@typescript-eslint/utils": "^7.14.1",
"babel-plugin-macros": "^3.1.0",
"eslint": "^8.57.0",
"eslint-config-airbnb-base": "^15.0.0",
Expand All @@ -25,19 +25,18 @@
"eslint-plugin-eslint-comments": "^3.2.0",
"eslint-plugin-file-progress": "^1.4.0",
"eslint-plugin-import": "^2.29.1",
"eslint-plugin-jest": "^28.5.0",
"eslint-plugin-jest": "^28.6.0",
"eslint-plugin-node-import": "^1.0.4",
"eslint-plugin-perfectionist": "^2.10.0",
"eslint-plugin-perfectionist": "^2.11.0",
"eslint-plugin-playwright": "^1.6.2",
"eslint-plugin-prettier": "^5.1.3",
"eslint-plugin-promise": "^6.2.0",
"eslint-plugin-rxjs": "^5.0.3",
"eslint-plugin-simple-import-sort": "^12.1.0",
"eslint-plugin-sonarjs": "^1.0.3",
"eslint-plugin-unicorn": "^53.0.0",
"eslint-plugin-unicorn": "^54.0.0",
"eslint-plugin-unused-imports": "^3.2.0",
"glob": "*",
"js-yaml": "^4.1.0"
"glob": "^10.4.2"
},
"publishConfig": {
"access": "public"
Expand Down
10 changes: 5 additions & 5 deletions projects/eslint-plugin-experience/rules/decorator-key-sort.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@ module.exports = {
ClassDeclaration(node) {
const decorators = Array.from(node.decorators ?? []);

for (const decorator of decorators) {
decorators.forEach(decorator => {
const expression = decorator.expression;

const decoratorName = expression.callee?.name ?? ``;
const decoratorName = expression.callee?.name ?? '';

if (decoratorName in (ORDER || {})) {
const orderList = ORDER[decoratorName];
Expand Down Expand Up @@ -56,7 +56,7 @@ module.exports = {
}
}
}
}
});
},
};
},
Expand All @@ -65,8 +65,8 @@ module.exports = {
schema: [
{
additionalProperties: true,
description: `Decorators names and their keys order`,
type: `object`,
description: 'Decorators names and their keys order',
type: 'object',
},
],
type: 'problem',
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
const MESSAGE_ID = `invalid-injection-token-description`;
const ERROR_MESSAGE = `InjectionToken's description should contain token's name`;
const MESSAGE_ID = 'invalid-injection-token-description';
const ERROR_MESSAGE = "InjectionToken's description should contain token's name";

/**
* @type {import('eslint').Rule.RuleModule}
Expand Down
47 changes: 25 additions & 22 deletions projects/eslint-plugin-experience/rules/no-deep-imports.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
const path = require('path');
const path = require('node:path');

const MESSAGE_ID = `no-deep-imports`;
const ERROR_MESSAGE = `Deep imports of Taiga UI packages are prohibited`;
const MESSAGE_ID = 'no-deep-imports';
const ERROR_MESSAGE = 'Deep imports of Taiga UI packages are prohibited';

const DEFAULT_OPTIONS = {
currentProject: ``,
deepImport: `(?<=^@taiga-ui/[\\w-]+)(/.+)$`,
currentProject: '',
deepImport: '(?<=^@taiga-ui/[\\w-]+)(/.+)$',
ignoreImports: [],
importDeclaration: `^@taiga-ui*`,
projectName: `(?<=^@taiga-ui/)([-\\w]+)`,
importDeclaration: '^@taiga-ui*',
projectName: '(?<=^@taiga-ui/)([-\\w]+)',
};

/**
Expand All @@ -33,7 +33,7 @@ module.exports = {
const isInsideTheSameEntryPoint = source => {
const filePath = path
.relative(context.getCwd(), context.getFilename())
.replace(/\\+/g, '/');
.replaceAll(/\\+/g, '/');

const [currentFileProjectName] =
(currentProject && filePath.match(new RegExp(currentProject, 'g'))) || [];
Expand All @@ -55,7 +55,7 @@ module.exports = {
[`ImportDeclaration[source.value=/${importDeclaration}/]`]({
source: sourceNode,
}) {
const importSource = sourceNode?.value || ``;
const importSource = sourceNode?.value || '';

if (
!isDeepImport(importSource) ||
Expand All @@ -71,7 +71,7 @@ module.exports = {

return fixer.replaceTextRange(
[start + 1, end - 1], // keeps quotes
importSource.replace(new RegExp(deepImport, 'g'), ``),
importSource.replaceAll(new RegExp(deepImport, 'g'), ''),
);
},
messageId: MESSAGE_ID,
Expand All @@ -82,7 +82,7 @@ module.exports = {
},
meta: {
docs: {description: ERROR_MESSAGE},
fixable: `code`,
fixable: 'code',
messages: {
[MESSAGE_ID]: ERROR_MESSAGE,
},
Expand All @@ -91,28 +91,31 @@ module.exports = {
additionalProperties: false,
properties: {
currentProject: {
description: `RegExp string to pick out current project name of processed file`,
type: `string`,
description:
'RegExp string to pick out current project name of processed file',
type: 'string',
},
deepImport: {
description: `RegExp string to pick out deep import part`,
type: `string`,
description: 'RegExp string to pick out deep import part',
type: 'string',
},
ignoreImports: {
description: `RegExp string to exclude import declarations which is selected by importDeclaration-option`,
description:
'RegExp string to exclude import declarations which is selected by importDeclaration-option',
items: {
type: `string`,
type: 'string',
},
type: `array`,
type: 'array',
},
importDeclaration: {
description: `RegExp string to detect import declarations for which this rule should be applied`,
type: `string`,
description:
'RegExp string to detect import declarations for which this rule should be applied',
type: 'string',
},
},
type: `object`,
type: 'object',
},
],
type: `problem`,
type: 'problem',
},
};
43 changes: 6 additions & 37 deletions projects/eslint-plugin-experience/rules/no-implicit-public.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
const ESLintUtils = require('@typescript-eslint/utils').ESLintUtils;

/**
* @type {import('eslint').Rule.RuleModule}
*/
Expand Down Expand Up @@ -30,51 +28,26 @@ function checkImplicitPublic(context, node) {
}

const name = node?.key?.name || node?.parameter?.name;
const services = ESLintUtils.getParserServices(context);
const superClass = classRef?.superClass ?? null;
const implements = classRef?.implements ?? [];

let hasFieldInParentClass = false;
let hasFieldInInterfaces = false;

if (superClass) {
const type = services.getTypeAtLocation(superClass);

hasFieldInParentClass = type.symbol?.members.has(name) ?? false;
}

for (const implement of implements) {
const type = services.getTypeAtLocation(implement);

if (type.symbol?.members.has(name)) {
hasFieldInInterfaces = true;
break;
}
}

let range = node?.parameter?.range ?? node.key.range ?? node.range;

const hasPublicDecorators = getDecorators(node?.decorators ?? []).find(decorator =>
['Inject', 'Input', 'Output'].includes(decorator),
);

if (node.kind === 'set' || node.kind === 'get') {
const [start, end] = node.key.range;

range = [start - node.kind.length - 1, end - node.kind.length - 1];
} else if (node.kind === 'method' && node.key?.object?.name === 'Symbol') {
const [start, end] = range;

range = [range[0] - 1, range[1] - 1];
range = [start - 1, end - 1];
}

if (node.type === 'PropertyDefinition' && node.decorators.length > 0) {
const [, end] = node.decorators[node.decorators.length - 1]?.range;
const [, end] = node?.decorators?.[node.decorators.length - 1]?.range ?? [];

range = [end + 1, end + 2];
}

const marked = ` public `;
const marked = ' public ';

context.report({
fix: fixer => fixer.insertTextBeforeRange(range, marked),
Expand All @@ -83,19 +56,15 @@ function checkImplicitPublic(context, node) {
});
}

function getDecorators(decorators) {
return decorators
?.map(decorator => decorator?.expression?.callee?.name)
.filter(Boolean);
}

/**
* @param {import('eslint').Rule.Node} node
*/
function getClass(node) {
if (!!node.parent && node.parent?.type === 'ClassDeclaration') {
return node.parent;
} else if (!!node.parent) {
}

if (node.parent) {
return getClass(node.parent);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ module.exports = {
const members = Array.from(node?.body?.body ?? []);

members.forEach(member => {
if (member?.key?.type === `PrivateIdentifier`) {
if (member?.key?.type === 'PrivateIdentifier') {
context.report({
message: `Please don't use "#${member?.key?.name}" instead of "private ${member?.key?.name}"`,
node,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ module.exports = {

if (isSimpleForOf) {
context.report({
message: `Don't use simple "for-of" instead of "forEach"`,
message: 'Don\'t use simple "for-of" instead of "forEach"',
node,
});
}
Expand Down
20 changes: 10 additions & 10 deletions projects/eslint-plugin-experience/rules/prefer-deep-imports.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
const fs = require('fs');
const fs = require('node:fs');
const glob = require('glob');

const MESSAGE_ID = `prefer-deep-imports`;
const ERROR_MESSAGE = `Import via root level entry point are prohibited for this package`;
const MESSAGE_ID = 'prefer-deep-imports';
const ERROR_MESSAGE = 'Import via root level entry point are prohibited for this package';

module.exports = {
create(context) {
Expand All @@ -11,7 +11,6 @@ module.exports = {
return {
[`ImportDeclaration[source.value=${getFilterRegExp(importFilter)}]`](
importDeclaration,
...rest
) {
const importedEntities = importDeclaration.specifiers;
const packageName = importDeclaration.source.value;
Expand Down Expand Up @@ -64,7 +63,7 @@ module.exports = {
},
meta: {
docs: {description: ERROR_MESSAGE},
fixable: `code`,
fixable: 'code',
messages: {
[MESSAGE_ID]: ERROR_MESSAGE,
},
Expand All @@ -74,14 +73,15 @@ module.exports = {
properties: {
// i.e. "/^@taiga-ui\\u002F(core|cdk|kit)$/"
importFilter: {
description: `RegExp string to detect import declarations for which this rule should be applied`,
type: [`string`, `array`],
description:
'RegExp string to detect import declarations for which this rule should be applied',
type: ['string', 'array'],
},
},
type: `object`,
type: 'object',
},
],
type: `problem`,
type: 'problem',
},
};

Expand All @@ -91,7 +91,7 @@ function findNearestEntryPoint(filePath) {
for (let i = pathSegments.length - 1; i >= 0; i--) {
const possibleEntryPoint = pathSegments.slice(0, i).join('/');

if (fs.existsSync(possibleEntryPoint + '/ng-package.json')) {
if (fs.existsSync(`${possibleEntryPoint}/ng-package.json`)) {
return possibleEntryPoint.replace(/^node_modules\//, '');
}
}
Expand Down
Loading

0 comments on commit 5ee8cbc

Please sign in to comment.