@akinon/eslint-plugin-projectzero 1.3.0 → 1.5.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/CHANGELOG.md CHANGED
@@ -1,5 +1,14 @@
1
1
  # @akinon/eslint-plugin-projectzero
2
2
 
3
+ ## 1.5.0
4
+
5
+ ### Minor Changes
6
+
7
+ - Add check-locale rule
8
+ - Add rules for extended Metadata import
9
+
10
+ ## 1.4.0
11
+
3
12
  ## 1.3.0
4
13
 
5
14
  ## 1.2.0
package/configs/core.ts CHANGED
@@ -5,6 +5,8 @@ export default {
5
5
  '@akinon/projectzero/link-import': 'error',
6
6
  '@akinon/projectzero/router-import': 'error',
7
7
  '@akinon/projectzero/skip-trailing-slash-redirect': 'error',
8
+ '@akinon/projectzero/check-locale': 'error',
9
+ '@akinon/projectzero/meta-data-import': 'error',
8
10
  'no-restricted-syntax': [
9
11
  'error',
10
12
  {
@@ -7,6 +7,8 @@ exports.default = {
7
7
  '@akinon/projectzero/link-import': 'error',
8
8
  '@akinon/projectzero/router-import': 'error',
9
9
  '@akinon/projectzero/skip-trailing-slash-redirect': 'error',
10
+ '@akinon/projectzero/check-locale': 'error',
11
+ '@akinon/projectzero/meta-data-import': 'error',
10
12
  'no-restricted-syntax': [
11
13
  'error',
12
14
  {
@@ -0,0 +1,85 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
14
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
15
+ }) : function(o, v) {
16
+ o["default"] = v;
17
+ });
18
+ var __importStar = (this && this.__importStar) || function (mod) {
19
+ if (mod && mod.__esModule) return mod;
20
+ var result = {};
21
+ if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
22
+ __setModuleDefault(result, mod);
23
+ return result;
24
+ };
25
+ Object.defineProperty(exports, "__esModule", { value: true });
26
+ const utils_1 = require("@typescript-eslint/utils");
27
+ const path = __importStar(require("path"));
28
+ const fs = __importStar(require("fs"));
29
+ const MONOREPO_LOCALES_DIR = path.resolve(__dirname, '../../../../apps/projectzeropwa/public/locales/en');
30
+ const STANDARD_REPO_LOCALES_DIR = path.join(process.cwd(), 'public/locales/en');
31
+ const LOCALES_DIR = fs.existsSync(MONOREPO_LOCALES_DIR)
32
+ ? MONOREPO_LOCALES_DIR
33
+ : STANDARD_REPO_LOCALES_DIR;
34
+ const getAllKeysFromObject = (obj, prefix = '') => {
35
+ return Object.keys(obj).reduce((result, key) => {
36
+ if (typeof obj[key] === 'object' && !Array.isArray(obj[key])) {
37
+ return result.concat(getAllKeysFromObject(obj[key], `${prefix}${key}.`));
38
+ }
39
+ return result.concat(`${prefix}${key}`);
40
+ }, []);
41
+ };
42
+ exports.default = utils_1.ESLintUtils.RuleCreator.withoutDocs({
43
+ create(context) {
44
+ return {
45
+ CallExpression(node) {
46
+ if (node.callee.type !== 'Identifier' || node.callee.name !== 't') {
47
+ return;
48
+ }
49
+ const [firstArg] = node.arguments;
50
+ if (firstArg &&
51
+ firstArg.type === 'Literal' &&
52
+ typeof firstArg.value === 'string') {
53
+ const key = firstArg.value;
54
+ const filename = `${key.split('.')[0]}.json`;
55
+ const filepath = path.join(LOCALES_DIR, filename);
56
+ if (fs.existsSync(filepath)) {
57
+ const jsonContent = JSON.parse(fs.readFileSync(filepath, 'utf-8'));
58
+ const allKeys = getAllKeysFromObject(jsonContent, filename.replace('.json', '.'));
59
+ if (!allKeys.includes(key)) {
60
+ context.report({
61
+ node: firstArg,
62
+ messageId: 'missingLocalizationKey'
63
+ });
64
+ }
65
+ }
66
+ else {
67
+ context.report({
68
+ node: firstArg,
69
+ messageId: 'missingLocalizationFile'
70
+ });
71
+ }
72
+ }
73
+ }
74
+ };
75
+ },
76
+ meta: {
77
+ messages: {
78
+ missingLocalizationKey: 'The localization key does not exist in the JSON files.',
79
+ missingLocalizationFile: 'The localization file does not exist.'
80
+ },
81
+ type: 'problem',
82
+ schema: []
83
+ },
84
+ defaultOptions: []
85
+ });
@@ -9,11 +9,15 @@ const image_import_1 = __importDefault(require("./image-import"));
9
9
  const link_import_1 = __importDefault(require("./link-import"));
10
10
  const router_import_1 = __importDefault(require("./router-import"));
11
11
  const skip_trailing_slash_redirect_1 = __importDefault(require("./skip-trailing-slash-redirect"));
12
+ const check_locale_1 = __importDefault(require("./check-locale"));
13
+ const meta_data_import_1 = __importDefault(require("./meta-data-import"));
12
14
  const rules = {
13
15
  'client-url': client_url_1.default,
14
16
  'image-import': image_import_1.default,
15
17
  'link-import': link_import_1.default,
16
18
  'router-import': router_import_1.default,
17
- 'skip-trailing-slash-redirect': skip_trailing_slash_redirect_1.default
19
+ 'skip-trailing-slash-redirect': skip_trailing_slash_redirect_1.default,
20
+ 'check-locale': check_locale_1.default,
21
+ 'meta-data-import': meta_data_import_1.default
18
22
  };
19
23
  exports.rules = rules;
@@ -0,0 +1,39 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ const utils_1 = require("@typescript-eslint/utils");
4
+ exports.default = utils_1.ESLintUtils.RuleCreator.withoutDocs({
5
+ create(context) {
6
+ return {
7
+ ImportDeclaration(node) {
8
+ const targetImportName = 'Metadata';
9
+ const importSpecifiers = node.specifiers
10
+ .filter((specifier) => specifier.type === 'ImportSpecifier')
11
+ .map((specifier) => specifier.local.name);
12
+ if (importSpecifiers.includes(targetImportName) &&
13
+ node.source.value === 'next') {
14
+ context.report({
15
+ messageId: 'metaDataImport',
16
+ node,
17
+ fix: (fixer) => {
18
+ if (node.specifiers.find((s) => s.type === 'ImportSpecifier')) {
19
+ return null;
20
+ }
21
+ return fixer.replaceText(node, node.specifiers
22
+ .map((specifier) => `import { ${specifier.local.name} } from '@akinon/next/types';`)
23
+ .join('\n'));
24
+ }
25
+ });
26
+ }
27
+ }
28
+ };
29
+ },
30
+ meta: {
31
+ messages: {
32
+ metaDataImport: "Import '{ Metadata }' component from '@akinon/next/types'."
33
+ },
34
+ type: 'problem',
35
+ fixable: 'code',
36
+ schema: []
37
+ },
38
+ defaultOptions: []
39
+ });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@akinon/eslint-plugin-projectzero",
3
- "version": "1.3.0",
3
+ "version": "1.5.0",
4
4
  "private": false,
5
5
  "description": "ESLint plugin for Project Zero Next",
6
6
  "main": "dist/index.js",
@@ -0,0 +1,78 @@
1
+ import { ESLintUtils } from '@typescript-eslint/utils';
2
+ import * as path from 'path';
3
+ import * as fs from 'fs';
4
+
5
+ const MONOREPO_LOCALES_DIR = path.resolve(
6
+ __dirname,
7
+ '../../../../apps/projectzeropwa/public/locales/en'
8
+ );
9
+
10
+ const STANDARD_REPO_LOCALES_DIR = path.join(process.cwd(), 'public/locales/en');
11
+
12
+ const LOCALES_DIR = fs.existsSync(MONOREPO_LOCALES_DIR)
13
+ ? MONOREPO_LOCALES_DIR
14
+ : STANDARD_REPO_LOCALES_DIR;
15
+
16
+ const getAllKeysFromObject = (obj: any, prefix = ''): string[] => {
17
+ return Object.keys(obj).reduce((result, key) => {
18
+ if (typeof obj[key] === 'object' && !Array.isArray(obj[key])) {
19
+ return result.concat(getAllKeysFromObject(obj[key], `${prefix}${key}.`));
20
+ }
21
+ return result.concat(`${prefix}${key}`);
22
+ }, [] as string[]);
23
+ };
24
+
25
+ export default ESLintUtils.RuleCreator.withoutDocs({
26
+ create(context: any) {
27
+ return {
28
+ CallExpression(node: any) {
29
+ if (node.callee.type !== 'Identifier' || node.callee.name !== 't') {
30
+ return;
31
+ }
32
+
33
+ const [firstArg] = node.arguments;
34
+
35
+ if (
36
+ firstArg &&
37
+ firstArg.type === 'Literal' &&
38
+ typeof firstArg.value === 'string'
39
+ ) {
40
+ const key = firstArg.value;
41
+
42
+ const filename = `${key.split('.')[0]}.json`;
43
+ const filepath = path.join(LOCALES_DIR, filename);
44
+
45
+ if (fs.existsSync(filepath)) {
46
+ const jsonContent = JSON.parse(fs.readFileSync(filepath, 'utf-8'));
47
+ const allKeys = getAllKeysFromObject(
48
+ jsonContent,
49
+ filename.replace('.json', '.')
50
+ );
51
+
52
+ if (!allKeys.includes(key)) {
53
+ context.report({
54
+ node: firstArg,
55
+ messageId: 'missingLocalizationKey'
56
+ });
57
+ }
58
+ } else {
59
+ context.report({
60
+ node: firstArg,
61
+ messageId: 'missingLocalizationFile'
62
+ });
63
+ }
64
+ }
65
+ }
66
+ };
67
+ },
68
+ meta: {
69
+ messages: {
70
+ missingLocalizationKey:
71
+ 'The localization key does not exist in the JSON files.',
72
+ missingLocalizationFile: 'The localization file does not exist.'
73
+ },
74
+ type: 'problem',
75
+ schema: []
76
+ },
77
+ defaultOptions: []
78
+ });
package/rules/index.ts CHANGED
@@ -3,13 +3,17 @@ import imageImport from './image-import';
3
3
  import linkImport from './link-import';
4
4
  import routerImport from './router-import';
5
5
  import skipTrailingSlashRedirect from './skip-trailing-slash-redirect';
6
+ import checkLocale from './check-locale';
7
+ import metaDataImport from './meta-data-import';
6
8
 
7
9
  const rules = {
8
10
  'client-url': clientUrl,
9
11
  'image-import': imageImport,
10
12
  'link-import': linkImport,
11
13
  'router-import': routerImport,
12
- 'skip-trailing-slash-redirect': skipTrailingSlashRedirect
14
+ 'skip-trailing-slash-redirect': skipTrailingSlashRedirect,
15
+ 'check-locale': checkLocale,
16
+ 'meta-data-import': metaDataImport
13
17
  };
14
18
 
15
19
  export { rules };
@@ -0,0 +1,50 @@
1
+ import { ESLintUtils } from '@typescript-eslint/utils';
2
+
3
+ export default ESLintUtils.RuleCreator.withoutDocs({
4
+ create(context) {
5
+ return {
6
+ ImportDeclaration(node) {
7
+ const targetImportName = 'Metadata';
8
+
9
+ const importSpecifiers = node.specifiers
10
+ .filter((specifier) => specifier.type === 'ImportSpecifier')
11
+ .map((specifier) => specifier.local.name);
12
+
13
+ if (
14
+ importSpecifiers.includes(targetImportName) &&
15
+ node.source.value === 'next'
16
+ ) {
17
+ context.report({
18
+ messageId: 'metaDataImport',
19
+ node,
20
+ fix: (fixer) => {
21
+ if (node.specifiers.find((s) => s.type === 'ImportSpecifier')) {
22
+ return null;
23
+ }
24
+
25
+ return fixer.replaceText(
26
+ node,
27
+ node.specifiers
28
+ .map(
29
+ (specifier) =>
30
+ `import { ${specifier.local.name} } from '@akinon/next/types';`
31
+ )
32
+ .join('\n')
33
+ );
34
+ }
35
+ });
36
+ }
37
+ }
38
+ };
39
+ },
40
+ meta: {
41
+ messages: {
42
+ metaDataImport:
43
+ "Import '{ Metadata }' component from '@akinon/next/types'."
44
+ },
45
+ type: 'problem',
46
+ fixable: 'code',
47
+ schema: []
48
+ },
49
+ defaultOptions: []
50
+ });