@emberbox/lint 0.1.0 → 0.2.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/README.md CHANGED
@@ -31,7 +31,8 @@ them there. JavaScript files are still checked by `no-undef`.
31
31
  | Option | Default | Meaning |
32
32
  |---|---|---|
33
33
  | `src` | `src` | the source directory |
34
- | `catalogue` | none | the German vue-i18n catalogue; with it every template text goes through the catalogue, and the catalogue is checked for missing, unused and plural messages |
34
+ | `catalogue` | none | the German vue-i18n catalogue; with it every template text goes through the catalogue, and the catalogue is checked for missing, unused and plural messages (plural messages unless `plurals` is set) |
35
+ | `plurals` | `false` | allow vue-i18n plural messages such as `{n} Alarm \| {n} Alarme`; without it a bare `\|` in the catalogue is an error, since it usually wanted escaping |
35
36
  | `layers` | `[]` | which source directories may not import from which |
36
37
  | `maxFunctionLines` | `79` | the longest function allowed |
37
38
  | `ignores`, `nodeFiles` | `[]` | further files to skip, or that run in Node |
@@ -66,7 +67,10 @@ export default {
66
67
  allowed, in TypeScript, Vue, JavaScript and Python.
67
68
  - `text`: further files only the em dash check reads.
68
69
  - `browserStorage`: a catalogue of every `localStorage` and `sessionStorage` key with its purpose;
69
- every key the code uses must be declared, every declared key used.
70
+ every key the code uses must be declared, every declared key used. A key a dependency stores, such
71
+ as `@emberbox/ui`'s colour mode `vueuse-color-scheme`, is declared with `"source": "@emberbox/ui"`; it is
72
+ listed for completeness and not expected in the project's own code. Entries with a `source` are not
73
+ checked against the dependency, so remove one when the dependency stops storing its key.
70
74
  - `duplication`: a jscpd config file, relative to the working directory.
71
75
 
72
76
  ## License
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@emberbox/lint",
3
- "version": "0.1.0",
3
+ "version": "0.2.0",
4
4
  "description": "Shared linters of the Emberbox tools: an ESLint config with the Emberbox rules and the emberbox-lint runner.",
5
5
  "type": "module",
6
6
  "license": "MIT",
@@ -50,7 +50,9 @@ function keysIn(content) {
50
50
 
51
51
  /**
52
52
  * Compares the keys the code uses with the catalogue: every used key must be declared, every
53
- * declared key used, and every key a literal or a constant of the same file.
53
+ * declared key used, and every key a literal or a constant of the same file. An entry with a
54
+ * `source`, the package that stores the key on the project's behalf (such as `@emberbox/ui` for the
55
+ * colour mode), is declared so the catalogue is complete, and is not expected in the project's code.
54
56
  *
55
57
  * @param root the absolute repository root
56
58
  * @param options `catalogue`, the catalogue file, and `sources`, the source folder, both relative to the root
@@ -59,7 +61,9 @@ function keysIn(content) {
59
61
  export function checkBrowserStorage(root, {catalogue, sources}) {
60
62
  const reporter = createReporter(root)
61
63
  const catalog = JSON.parse(readFileSync(join(root, catalogue), 'utf-8'))
62
- const declared = new Set((catalog.entries ?? []).map(entry => entry.key))
64
+ const entries = catalog.entries ?? []
65
+ const declared = new Set(entries.map(entry => entry.key))
66
+ const fromDependencies = new Set(entries.filter(entry => entry.source).map(entry => entry.key))
63
67
  const used = new Map()
64
68
  const folder = join(root, sources)
65
69
  for (const file of [...walk(folder, '.ts'), ...walk(folder, '.vue')]) {
@@ -73,8 +77,11 @@ export function checkBrowserStorage(root, {catalogue, sources}) {
73
77
  for (const [key, file] of used) {
74
78
  if (!declared.has(key)) reporter.error(file, 0, `browser storage key '${key}' is not declared in ${catalogue}`, 'undeclared')
75
79
  }
80
+ for (const key of fromDependencies) {
81
+ if (used.has(key)) reporter.error(catalogue, 0, `browser storage key '${key}' is used by this project's own code, so it must not name a source`, 'source')
82
+ }
76
83
  for (const key of declared) {
77
- if (!used.has(key)) reporter.error(catalogue, 0, `declared browser storage key '${key}' is no longer used`, 'stale')
84
+ if (!used.has(key) && !fromDependencies.has(key)) reporter.error(catalogue, 0, `declared browser storage key '${key}' is no longer used`, 'stale')
78
85
  }
79
86
  return reporter.finish(`Browser storage lint passed. ${used.size} keys used, ${declared.size} declared in ${catalogue}.`)
80
87
  }
package/src/eslint.mjs CHANGED
@@ -63,9 +63,10 @@ function layerZones(src, {layer, above}) {
63
63
  * @param src the source directory
64
64
  * @param catalogue the catalogue module
65
65
  * @param cwd the directory ESLint runs in
66
+ * @param plurals whether the catalogue may hold vue-i18n plural messages
66
67
  * @returns the config entries
67
68
  */
68
- async function i18nConfigs(src, catalogue, cwd) {
69
+ async function i18nConfigs(src, catalogue, cwd, plurals) {
69
70
  const snapshots = await localeSnapshots(catalogue, cwd)
70
71
  const sourceRule = {
71
72
  name: 'emberbox/vue-i18n',
@@ -94,7 +95,7 @@ async function i18nConfigs(src, catalogue, cwd) {
94
95
  settings: {'vue-i18n': {localeDir: [snapshots, catalogue], messageSyntaxVersion: '^11.0.0'}},
95
96
  rules: {
96
97
  '@intlify/vue-i18n/valid-message-syntax': 'error',
97
- 'emberbox/i18n-no-plural-messages': 'error',
98
+ ...(plurals ? {} : {'emberbox/i18n-no-plural-messages': 'error'}),
98
99
  'emberbox/i18n-unused-keys': ['error', {sources: src, german: [{file: catalogue}]}],
99
100
  },
100
101
  }
@@ -131,6 +132,7 @@ function keepOffCatalogue(catalogue) {
131
132
  * @param {string} [options.cwd] the directory ESLint runs in, the working directory by default
132
133
  * @param {string} [options.src] the source directory, `src` by default
133
134
  * @param {string} [options.catalogue] the German catalogue module, such as `src/i18n/de.ts`
135
+ * @param {boolean} [options.plurals] allow vue-i18n plural messages such as `{n} Alarm | {n} Alarme`; without it a bare `|` in the catalogue is an error
134
136
  * @param {{layer: string, above: string[]}[]} [options.layers] import boundaries between source directories
135
137
  * @param {number} [options.maxFunctionLines] the longest function allowed, 79 by default
136
138
  * @param {string[]} [options.ignores] further files ESLint skips
@@ -138,8 +140,8 @@ function keepOffCatalogue(catalogue) {
138
140
  * @returns {Promise<import('eslint').Linter.Config[]>} the config
139
141
  */
140
142
  export async function emberboxEslint(options = {}) {
141
- const {cwd = process.cwd(), src = 'src', catalogue, layers = [], maxFunctionLines = 79, ignores = [], nodeFiles = []} = options
142
- const i18n = catalogue ? await i18nConfigs(src, catalogue, cwd) : {settings: {}, configs: []}
143
+ const {cwd = process.cwd(), src = 'src', catalogue, plurals = false, layers = [], maxFunctionLines = 79, ignores = [], nodeFiles = []} = options
144
+ const i18n = catalogue ? await i18nConfigs(src, catalogue, cwd, plurals) : {settings: {}, configs: []}
143
145
  return [
144
146
  {name: 'emberbox/ignores', ignores: ['node_modules/**', 'dist/**', 'coverage/**', 'public/**', ...ignores]},
145
147
  {
@@ -8,8 +8,10 @@ const PLURAL = 1
8
8
  * Refuses a message vue-i18n reads as plural choices.
9
9
  *
10
10
  * <p>A bare `|` splits a message into choices, and `t()` then shows the first of them unless a
11
- * count is passed, so a pipe written as text silently cuts the sentence short. Nothing here passes
12
- * counts to pick a plural form, so every such message is a pipe that wanted escaping as `{'|'}`.
11
+ * count is passed, so a pipe written as text silently cuts the sentence short. In a project that
12
+ * passes no counts to pick a plural form, every such message is a pipe that wanted escaping as
13
+ * `{'|'}`; a project that uses plural messages turns this rule off with the config's `plurals`
14
+ * option.
13
15
  * The message is read by the compiler vue-i18n itself uses, not by pattern.
14
16
  *
15
17
  * @type {import('eslint').Rule.RuleModule}