eslint-plugin-chrome-extensions 2.4.2 → 2.6.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.
Files changed (47) hide show
  1. package/README.md +272 -115
  2. package/package.json +6 -8
  3. package/src/data/apis.js +166 -115
  4. package/src/data/deprecations.js +5 -7
  5. package/src/data/i18n.js +19 -69
  6. package/src/data/manifest.js +61 -57
  7. package/src/data/permissions.js +23 -30
  8. package/src/data/regex.js +10 -0
  9. package/src/index.js +7 -124
  10. package/src/rules/manifest-key-order.js +8 -8
  11. package/src/rules/manifest-spacing.js +5 -5
  12. package/src/rules/no-content-style-leak.js +6 -4
  13. package/src/rules/no-deprecated-apis.js +4 -4
  14. package/src/rules/no-duplicate-manifest-keys.js +3 -4
  15. package/src/rules/no-insecure-messaging.js +4 -4
  16. package/src/rules/no-invalid-locales.js +89 -0
  17. package/src/rules/no-invalid-permissions.js +10 -13
  18. package/src/rules/no-missing-default-locale.js +44 -21
  19. package/src/rules/no-missing-i18n-keys.js +24 -25
  20. package/src/rules/no-missing-permissions.js +7 -6
  21. package/src/rules/no-missing-web-accessible-resources.js +7 -6
  22. package/src/rules/no-remote-code.js +12 -12
  23. package/src/rules/no-sensitive-permissions.js +75 -0
  24. package/src/rules/no-unrestricted-host-permissions.js +35 -19
  25. package/src/rules/no-unused-i18n-keys.js +11 -10
  26. package/src/rules/no-unused-permissions.js +31 -23
  27. package/src/rules/require-license-file.js +4 -5
  28. package/src/rules/require-mv3.js +5 -6
  29. package/src/rules/require-short-name-when-useful.js +4 -3
  30. package/src/rules/valid-filenames.js +3 -10
  31. package/src/rules/valid-license-file.js +18 -11
  32. package/src/rules/valid-manifest-icons.js +35 -6
  33. package/src/rules/valid-mv3-schema.js +22 -22
  34. package/src/rules/valid-service-worker.js +34 -13
  35. package/src/rules/valid-url-fields.js +49 -41
  36. package/src/rules/valid-version-field.js +5 -9
  37. package/src/utils/extension.js +52 -33
  38. package/src/utils/i18n.js +58 -0
  39. package/src/utils/init.js +107 -0
  40. package/src/utils/license.js +56 -0
  41. package/src/utils/node.js +20 -19
  42. package/src/utils/pkg.js +12 -10
  43. package/src/utils/rules.js +6 -4
  44. package/src/utils/text.js +13 -0
  45. package/src/data/license.js +0 -28
  46. package/src/data/text-parser.js +0 -11
  47. package/src/utils/bundle.js +0 -10
@@ -1,8 +1,9 @@
1
- import { getMemberPath } from '../utils/node.js'
2
- import { readManifest } from '../utils/extension.js'
3
- import { findExtensionRoot, isPredefined, readDefaultLocaleMessages, re as i18nRe } from '../data/i18n.js'
1
+ import extension from '../utils/extension.js'
2
+ import i18nUtils from '../utils/i18n.js'
3
+ import nodeUtils from '../utils/node.js'
4
4
 
5
- const re = { file: { js: /\.(cjs|js|jsx|mjs|ts|tsx)$/i, manifest: /manifest\.json$/, text: /\.(css|htm|html)$/i }}
5
+ import i18nData from '../data/i18n.js'
6
+ import regex from '../data/regex.js'
6
7
 
7
8
  export default {
8
9
  meta: {
@@ -12,9 +13,7 @@ export default {
12
13
  category: 'Possible Errors',
13
14
  recommended: true,
14
15
  manifest: true,
15
- css: true,
16
- html: true,
17
- json: true
16
+ css: true
18
17
  },
19
18
  schema: [{
20
19
  type: 'object',
@@ -33,41 +32,41 @@ export default {
33
32
  },
34
33
 
35
34
  create(context) {
36
- const filename = context.filename
37
- if (!filename || filename == '<input>') return {}
38
- const manifest = readManifest(context) ; if (!manifest?.default_locale) return {}
39
- const root = findExtensionRoot(filename) ; if (!root) return {}
40
- const i18n = readDefaultLocaleMessages(manifest, root) ; if (!i18n) return {}
35
+ const filename = context.filename ; if (!filename || filename == '<input>') return {}
36
+ const manifest = extension.manifest.read(context) ; if (!manifest?.default_locale) return {}
37
+ const root = extension.findRoot(context) ; if (!root) return {}
38
+ const i18n = i18nUtils.readDefaultLocaleMessages(manifest, root) ; if (!i18n) return {}
41
39
  const opts = context.options[0] || {},
42
40
  ignored = new Set(opts.ignoreKeys || []),
43
- isMissing = key => !isPredefined(key) && !ignored.has(key) && !(key in i18n.messages),
41
+ isMissing = key => !i18nUtils.isPredefined(key) && !ignored.has(key) && !(key in i18n.messages),
44
42
  loc = { line: 1, column: 1 }
45
- if (re.file.js.test(filename))
43
+ if (regex.filename.script.test(filename))
46
44
  return {
47
45
  CallExpression(node) {
48
46
  if (node.callee.type != 'MemberExpression') return
49
- const path = getMemberPath(node.callee)
50
- if (path != 'chrome.i18n.getMessage' && path != 'browser.i18n.getMessage') return
47
+ const path = nodeUtils.getMemberPath(node.callee) ; if (!i18nData.re.getMsg.path.test(path)) return
51
48
  const arg = node.arguments[0] ; if (!arg) return
52
49
  let key
53
- if (arg.type == 'Literal' && typeof arg.value == 'string') key = arg.value
54
- else if (arg.type == 'TemplateLiteral' && arg.expressions.length == 0)
50
+ if (arg.type == 'Literal' && typeof arg.value == 'string')
51
+ key = arg.value
52
+ else if (arg.type == 'TemplateLiteral' && arg.expressions.length === 0)
55
53
  key = arg.quasis[0].value.cooked
56
54
  if (key && isMissing(key))
57
55
  context.report({ loc, messageId: 'missingKey', data: { key, locale: i18n.locale }})
58
56
  }
59
57
  }
60
- if (!re.file.text.test(filename) && !re.file.manifest.test(filename)) return {}
61
- const seen = new Set()
62
- const scanPlaceholders = () => {
63
- for (const match of context.sourceCode.text.matchAll(i18nRe.msgPlaceholder)) {
58
+ if (!regex.filename.css.test(filename) && !regex.filename.manifest.test(filename))
59
+ return {}
60
+ const seenKeys = new Set()
61
+ function scanPlaceholders() {
62
+ for (const match of context.sourceCode.text.matchAll(i18nData.re.msgPlaceholder)) {
64
63
  const key = match[1]
65
- if (seen.has(key)) continue
66
- seen.add(key)
64
+ if (seenKeys.has(key)) continue
65
+ seenKeys.add(key)
67
66
  if (isMissing(key))
68
67
  context.report({ loc, messageId: 'missingKey', data: { key, locale: i18n.locale }})
69
68
  }
70
69
  }
71
- return { 'Document': scanPlaceholders, 'Program': scanPlaceholders }
70
+ return { 'Document': scanPlaceholders, 'Program': scanPlaceholders, 'StyleSheet': scanPlaceholders }
72
71
  }
73
72
  }
@@ -1,6 +1,7 @@
1
- import { findPermission } from '../data/apis.js'
2
- import { getMemberPath } from '../utils/node.js'
3
- import { readManifest } from '../utils/extension.js'
1
+ import extension from '../utils/extension.js'
2
+ import nodeUtils from '../utils/node.js'
3
+
4
+ import apis from '../data/apis.js'
4
5
 
5
6
  function queriesActiveTab(node) {
6
7
  const arg = node.arguments[0] ; if (arg?.type != 'ObjectExpression') return false
@@ -27,13 +28,13 @@ export default {
27
28
  },
28
29
 
29
30
  create(context) {
30
- const manifest = readManifest(context) ; if (!manifest) return {}
31
+ const manifest = extension.manifest.read(context) ; if (!manifest) return {}
31
32
  const declared = new Set([...(manifest.permissions || []), ...(manifest.optional_permissions || [])])
32
33
  return {
33
34
  CallExpression(node) {
34
35
  if (node.callee.type != 'MemberExpression') return
35
- const api = getMemberPath(node.callee) ; if (!api) return
36
- const permission = findPermission(api) ; if (!permission || declared.has(permission)) return
36
+ const api = nodeUtils.getMemberPath(node.callee) ; if (!api) return
37
+ const permission = apis.findPermission(api) ; if (!permission || declared.has(permission)) return
37
38
  const isActiveTabQuery = permission == 'tabs' && queriesActiveTab(node)
38
39
  if (isActiveTabQuery && declared.has('activeTab')) return
39
40
  const messageId = `missing${ isActiveTabQuery ? 'ActiveTab' : 'Permission' }`
@@ -1,5 +1,5 @@
1
- import { getMemberPath } from '../utils/node.js'
2
- import { file, readManifest } from '../utils/extension.js'
1
+ import extension from '../utils/extension.js'
2
+ import nodeUtils from '../utils/node.js'
3
3
 
4
4
  const re = { leadingSlash: /^\//, regexSpecial: /[.+?^${}()|[\]\\]/g }
5
5
 
@@ -35,13 +35,14 @@ export default {
35
35
  },
36
36
 
37
37
  create(context) {
38
- const manifest = readManifest(context) ; if (!manifest) return {}
39
- if (!file.isContentScript(context, manifest)) return {}
38
+ const manifest = extension.manifest.read(context) ; if (!manifest) return {}
39
+ if (!extension.file.isContentScript(context, manifest)) return {}
40
40
  const regexes = collectWARpatterns(manifest).map(patternToRegex)
41
41
  return {
42
42
  CallExpression(node) {
43
- if (node.callee.type != 'MemberExpression') return
44
- if (getMemberPath(node.callee) != 'chrome.runtime.getURL') return
43
+ if (node.callee.type != 'MemberExpression'
44
+ || nodeUtils.getMemberPath(node.callee) != 'chrome.runtime.getURL'
45
+ ) return
45
46
  const arg = node.arguments[0] ; if (arg?.type != 'Literal' || typeof arg.value != 'string') return
46
47
  const resourcePath = arg.value.replace(re.leadingSlash, '')
47
48
  for (const regex of regexes) if (regex.test(resourcePath)) return
@@ -1,7 +1,7 @@
1
- import { remoteCodePatterns } from '../data/manifest.js'
2
- import { getMemberPath } from '../utils/node.js'
1
+ import nodeUtils from '../utils/node.js'
3
2
 
4
- const re = { url: /^https?:\/\// }
3
+ import manifest from '../data/manifest.js'
4
+ import regex from '../data/regex.js'
5
5
 
6
6
  export default {
7
7
  meta: {
@@ -22,7 +22,7 @@ export default {
22
22
  properties: {
23
23
  patterns: {
24
24
  type: 'array',
25
- items: { enum: remoteCodePatterns },
25
+ items: { enum: manifest.patterns.remoteCode },
26
26
  description: 'Detection paths to enable'
27
27
  },
28
28
  checkExecuteScriptClosures: {
@@ -45,7 +45,7 @@ export default {
45
45
 
46
46
  create(context) {
47
47
  const opts = context.options[0] || {},
48
- patterns = new Set(opts.patterns || remoteCodePatterns),
48
+ patterns = new Set(opts.patterns || manifest.regexmoteCode),
49
49
  checkClosures = opts.checkExecuteScriptClosures !== false,
50
50
  scriptElems = new Set()
51
51
  function isCreateScript(node) {
@@ -53,10 +53,10 @@ export default {
53
53
  && node.callee?.property?.name == 'createElement'
54
54
  && node.arguments[0]?.value == 'script'
55
55
  }
56
- function isLiteralUrl(node) {
56
+ function isLiteralURL(node) {
57
57
  return node?.type == 'Literal'
58
58
  && typeof node.value == 'string'
59
- && re.url.test(node.value)
59
+ && regex.url.test(node.value)
60
60
  }
61
61
  function isObjectAssign(node) {
62
62
  return node?.type == 'CallExpression'
@@ -67,7 +67,7 @@ export default {
67
67
  function isScriptSrcAssignment(left, right) {
68
68
  if (left.type != 'MemberExpression') return false
69
69
  if (left.property?.name != 'src') return false
70
- if (!isLiteralUrl(right)) return false
70
+ if (!isLiteralURL(right)) return false
71
71
  if (isCreateScript(left.object)) return true
72
72
  if (left.object.type == 'Identifier' && scriptElems.has(left.object.name)) return true
73
73
  return false
@@ -87,7 +87,7 @@ export default {
87
87
  },
88
88
  ImportExpression(node) {
89
89
  if (!patterns.has('dynamic-import')) return
90
- if (isLiteralUrl(node.source))
90
+ if (isLiteralURL(node.source))
91
91
  context.report({ node, messageId: 'dynamicImport' })
92
92
  },
93
93
  CallExpression(node) {
@@ -103,10 +103,10 @@ export default {
103
103
  ) context.report({ node, messageId: 'stringTimer', data: { fn: node.callee.name }})
104
104
  if (patterns.has('dynamic-import')
105
105
  && node.callee.type == 'Import'
106
- && isLiteralUrl(node.arguments[0])
106
+ && isLiteralURL(node.arguments[0])
107
107
  ) context.report({ node, messageId: 'dynamicImport' })
108
108
  if (node.callee.type == 'MemberExpression') {
109
- const path = getMemberPath(node.callee)
109
+ const path = nodeUtils.getMemberPath(node.callee)
110
110
  if (path == 'chrome.scripting.executeScript') {
111
111
  const optsNode = node.arguments[0]
112
112
  if (optsNode?.type != 'ObjectExpression') return
@@ -132,7 +132,7 @@ export default {
132
132
  Property(node) {
133
133
  if (!patterns.has('remote-script-src')
134
134
  || node.key?.name != 'src'
135
- || !isLiteralUrl(node.value)
135
+ || !isLiteralURL(node.value)
136
136
  || !isScriptSrcProperty(node)
137
137
  ) return
138
138
  context.report({ node: node.value, messageId: 'remoteScriptSrc' })
@@ -0,0 +1,75 @@
1
+ import extension from '../utils/extension.js'
2
+
3
+ import permissions from '../data/permissions.js'
4
+ import regex from '../data/regex.js'
5
+
6
+ export default {
7
+ meta: {
8
+ type: 'problem',
9
+ docs: {
10
+ description: 'Flag sensitive permissions',
11
+ category: 'Possible Errors',
12
+ recommended: true,
13
+ security: true,
14
+ manifest: true
15
+ },
16
+ defaultOptions: [{ allowedPermissions: [] }],
17
+ schema: [{
18
+ type: 'object',
19
+ properties: {
20
+ allowedPermissions: {
21
+ oneOf: [
22
+ { type: 'string', description: 'Single sensitive permission to allow' },
23
+ {
24
+ type: 'array',
25
+ items: { type: 'string', description: 'Sensitive permission to allow' },
26
+ description: 'List of sensitive permissions to allow'
27
+ }
28
+ ],
29
+ description: 'Sensitive permission names to explicitly allow'
30
+ }
31
+ },
32
+ additionalProperties: false
33
+ }],
34
+ messages: {
35
+ sensitivePermission: '`{{permission}}` is a sensitive permission. Review whether it\'s needed'
36
+ + ' or add it to `allowedPermissions`.'
37
+ }
38
+ },
39
+
40
+ create(context) {
41
+ const opts = context.options[0] || {},
42
+ allowed = new Set(Array.isArray(opts.allowedPermissions) ? opts.allowedPermissions
43
+ : opts.allowedPermissions ? [opts.allowedPermissions] : [])
44
+ function checkPermission(node, permission) {
45
+ if (typeof permission != 'string') return
46
+ if (allowed.has(permission) || !permissions.sensitive.has(permission)) return
47
+ context.report({ node, messageId: 'sensitivePermission', data: { permission }})
48
+ }
49
+ if (regex.filename.manifest.test(extension.file.getName(context))) // lint it directly
50
+ return {
51
+ '*'(node) {
52
+ const keyNode = node.name || node.key ; if (!keyNode) return
53
+ const keyName = keyNode.value ?? keyNode.name
54
+ if (keyName != 'permissions' && keyName != 'optional_permissions') return
55
+ const arrNode = node.value ; if (arrNode?.type != 'Array') return
56
+ const nodeElems = arrNode.elements || arrNode.items || []
57
+ for (const elem of nodeElems) {
58
+ const valueNode = elem.value || elem
59
+ checkPermission(valueNode, valueNode?.value)
60
+ }
61
+ }
62
+ }
63
+ const manifest = extension.manifest.read(context) ; if (!manifest) return {}
64
+ const allPermissions = [...(manifest.permissions || []), ...(manifest.optional_permissions || [])]
65
+ return {
66
+ 'Program:exit'() {
67
+ for (const permission of allPermissions) {
68
+ if (allowed.has(permission) || !permissions.sensitive.has(permission)) continue
69
+ context.report({ loc: { line: 1, column: 0 },
70
+ messageId: 'sensitivePermission', data: { permission }})
71
+ }
72
+ }
73
+ }
74
+ }
75
+ }
@@ -1,6 +1,7 @@
1
- import { file } from '../utils/extension.js'
1
+ import extension from '../utils/extension.js'
2
+ import regex from '../data/regex.js'
2
3
 
3
- const re = { broadHost: /^(?:<all_urls>|\*:\/\/\*\/\*|\w+:\/\/\*\/\*)$/, manifestFilename: /manifest\.json$/ },
4
+ const re = { broadHost: /^(?:<all_urls>|\*:\/\/\*\/\*|\w+:\/\/\*\/\*)$/ },
4
5
  hostKeys = new Set(['host_permissions', 'optional_host_permissions', 'optional_permissions', 'permissions'])
5
6
 
6
7
  export default {
@@ -9,7 +10,8 @@ export default {
9
10
  docs: {
10
11
  description: 'Disallow broad host permissions that grant access to every site',
11
12
  category: 'Possible Errors',
12
- recommended: false,
13
+ recommended: true,
14
+ security: true,
13
15
  manifest: true
14
16
  },
15
17
  defaultOptions: [{ allowedPatterns: [] }],
@@ -31,23 +33,37 @@ export default {
31
33
  },
32
34
 
33
35
  create(context) {
34
- if (!re.manifestFilename.test(file.getName(context))) return {}
35
- const allowed = new Set(context.options[0]?.allowedPatterns || [])
36
+ const allowedPatterns = new Set(context.options[0]?.allowedPatterns || [])
37
+ function isBroad(pattern) {
38
+ return typeof pattern == 'string'
39
+ && !allowedPatterns.has(pattern)
40
+ && re.broadHost.test(pattern)
41
+ }
42
+ if (regex.filename.manifest.test(extension.file.getName(context))) // lint it directly
43
+ return {
44
+ '*'(node) {
45
+ const keyNode = node.name || node.key ; if (!keyNode) return
46
+ const keyName = keyNode.value ?? keyNode.name ; if (!hostKeys.has(keyName)) return
47
+ const arrNode = node.value ; if (arrNode?.type != 'Array') return
48
+ const nodeElems = arrNode.elements || arrNode.items || []
49
+ for (const elem of nodeElems) {
50
+ const valueNode = elem.value || elem,
51
+ pattern = valueNode?.value
52
+ if (!isBroad(pattern)) continue
53
+ context.report({ node: valueNode, messageId: 'unrestrictedHost', data: { pattern }})
54
+ }
55
+ }
56
+ }
57
+ const manifest = extension.manifest.read(context) ; if (!manifest) return {}
36
58
  return {
37
- '*'(node) {
38
- const keyNode = node.name || node.key ; if (!keyNode) return
39
- const keyName = keyNode.value ?? keyNode.name
40
- if (!hostKeys.has(keyName)) return
41
- const arrNode = node.value ; if (arrNode?.type != 'Array') return
42
- const elems = arrNode.elements || arrNode.items || []
43
- for (const elem of elems) {
44
- const valueNode = elem.value || elem,
45
- pattern = valueNode?.value
46
- if (typeof pattern != 'string'
47
- || allowed.has(pattern)
48
- || !re.broadHost.test(pattern)
49
- ) continue
50
- context.report({ node: valueNode, messageId: 'unrestrictedHost', data: { pattern }})
59
+ 'Program:exit'() {
60
+ for (const key of hostKeys) {
61
+ const patterns = manifest[key] // eslint-disable-line security/detect-object-injection
62
+ if (!Array.isArray(patterns)) continue
63
+ for (const pattern of patterns)
64
+ if (isBroad(pattern))
65
+ context.report({ loc: { line: 1, column: 1 },
66
+ messageId: 'unrestrictedHost', data: { pattern }})
51
67
  }
52
68
  }
53
69
  }
@@ -1,7 +1,8 @@
1
- import { collectUsedKeys, defaultScanExtensions, findExtensionRoot, readDefaultLocaleMessages } from '../data/i18n.js'
2
- import { readManifest } from '../utils/extension.js'
1
+ import extension from '../utils/extension.js'
2
+ import i18nUtils from '../utils/i18n.js'
3
3
 
4
- const re = { manifestFile: /manifest\.json$/ }
4
+ import i18nData from '../data/i18n.js'
5
+ import regex from '../data/regex.js'
5
6
 
6
7
  export default {
7
8
  meta: {
@@ -26,7 +27,7 @@ export default {
26
27
  },
27
28
  additionalProperties: false
28
29
  }],
29
- defaultOptions: [{ ignoreKeys: [], scanExtensions: defaultScanExtensions }],
30
+ defaultOptions: [{ ignoreKeys: [], scanExtensions: i18nData.defaultScanExtensions }],
30
31
  messages: {
31
32
  unusedKey: '`{{key}}` in `_locales/{{locale}}/messages.json` is never referenced.'
32
33
  }
@@ -34,15 +35,15 @@ export default {
34
35
 
35
36
  create(context) {
36
37
  const filename = context.filename
37
- if (!filename || !re.manifestFile.test(filename)) return {}
38
- const manifest = readManifest(context) ; if (!manifest?.default_locale) return {}
39
- const root = findExtensionRoot(filename) ; if (!root) return {}
40
- const i18n = readDefaultLocaleMessages(manifest, root) ; if (!i18n) return {}
38
+ if (!filename || !regex.filename.manifest.test(filename)) return {}
39
+ const manifest = extension.manifest.read(context) ; if (!manifest?.default_locale) return {}
40
+ const root = extension.findRoot(context) ; if (!root) return {}
41
+ const i18n = i18nUtils.readDefaultLocaleMessages(manifest, root) ; if (!i18n) return {}
41
42
  const opts = context.options[0] || {},
42
43
  ignored = new Set(opts.ignoreKeys || []),
43
- scanExtensions = opts.scanExtensions || defaultScanExtensions
44
+ scanExtensions = opts.scanExtensions || i18nData.defaultScanExtensions
44
45
  function check() {
45
- const { used, patterns } = collectUsedKeys(root, scanExtensions)
46
+ const { used, patterns } = i18nUtils.collectUsedKeys(root, scanExtensions)
46
47
  for (const key of Object.keys(i18n.messages)) {
47
48
  if (ignored.has(key) || used.has(key) || patterns.some(patt => patt.test(key))) continue
48
49
  context.report({ loc: { line: 1, column: 1 }, messageId: 'unusedKey',
@@ -1,10 +1,9 @@
1
+ import extension from '../utils/extension.js'
1
2
  import fs from 'node:fs'
2
3
  import path from 'node:path'
3
4
 
4
- import { apiPermissionMap, findPermission } from '../data/apis.js'
5
- import { file, findRoot as findExtensionRoot, readManifest } from '../utils/extension.js'
6
-
7
- const re = { manifestFilename: /manifest\.json$/ }
5
+ import apis from '../data/apis.js'
6
+ import regex from '../data/regex.js'
8
7
 
9
8
  function collectUsedAPIs(manifest, root) {
10
9
  const files = new Set()
@@ -34,7 +33,7 @@ export default {
34
33
  docs: {
35
34
  description: 'Flag manifest permissions whose APIs are never used',
36
35
  category: 'Possible Errors',
37
- recommended: false,
36
+ recommended: true,
38
37
  manifest: true
39
38
  },
40
39
  schema: [],
@@ -44,40 +43,49 @@ export default {
44
43
  },
45
44
 
46
45
  create(context) {
47
- if (!re.manifestFilename.test(file.getName(context))) return {}
48
- const manifest = readManifest(context) ; if (!manifest) return {}
49
- const root = findExtensionRoot(context) ; if (!root) return {}
46
+ const filename = extension.file.getName(context)
47
+ if (!regex.filename.manifest.test(filename) && !regex.filename.script.test(filename)) return {}
48
+ const manifest = extension.manifest.read(context) ; if (!manifest) return {}
49
+ const root = extension.findRoot(context) ; if (!root) return {}
50
50
  const usedAPIs = collectUsedAPIs(manifest, root)
51
- const permissionNodes = new Map()
52
51
  function isUsed(permission) {
53
- let known = false
54
- for (const [, perm] of apiPermissionMap)
55
- if (perm == permission) { known = true ; break }
56
- if (!known) return true // custom permission → not our business
52
+ let knownPerm = false
53
+ for (const [, perm] of apis.permissionMap)
54
+ if (perm == permission) { knownPerm = true ; break }
55
+ if (!knownPerm) return true
57
56
  for (const api of usedAPIs)
58
- if (findPermission(api) == permission) return true
57
+ if (apis.findPermission(api) == permission) return true
59
58
  return false
60
59
  }
60
+ if (!regex.filename.manifest.test(filename)) {
61
+ const loc = { line: 1, column: 1 },
62
+ declared = [...(manifest.permissions || []), ...(manifest.optional_permissions || [])]
63
+ for (const permission of declared)
64
+ if (typeof permission == 'string' && !isUsed(permission))
65
+ context.report({ loc, messageId: 'unusedPermission', data: { permission }})
66
+ return {}
67
+ }
68
+ const permissionNodes = new Map()
61
69
  return {
62
70
  '*'(node) {
63
71
  const keyNode = node.name || node.key ; if (!keyNode) return
64
72
  const keyName = keyNode.value ?? keyNode.name
65
73
  if (keyName != 'permissions' && keyName != 'optional_permissions') return
66
74
  const arrNode = node.value ; if (arrNode?.type != 'Array') return
67
- const elems = arrNode.elements || arrNode.items || []
68
- for (const elem of elems) {
69
- const valueNode = elem.value || elem
70
- const perm = valueNode?.value
71
- if (typeof perm != 'string') continue
72
- if (!permissionNodes.has(perm)) permissionNodes.set(perm, [])
73
- permissionNodes.get(perm).push(valueNode)
75
+ const nodeElems = arrNode.elements || arrNode.items || []
76
+ for (const elem of nodeElems) {
77
+ const valueNode = elem.value || elem,
78
+ permission = valueNode?.value
79
+ if (typeof permission != 'string') continue
80
+ if (!permissionNodes.has(permission)) permissionNodes.set(permission, [])
81
+ permissionNodes.get(permission).push(valueNode)
74
82
  }
75
83
  },
76
84
  'Document:exit'() {
77
85
  for (const [permission, nodes] of permissionNodes) {
78
86
  if (isUsed(permission)) continue
79
- for (const n of nodes)
80
- context.report({ node: n, messageId: 'unusedPermission', data: { permission }})
87
+ for (const node of nodes)
88
+ context.report({ node, messageId: 'unusedPermission', data: { permission }})
81
89
  }
82
90
  }
83
91
  }
@@ -1,7 +1,6 @@
1
+ import license from '../utils/license.js'
1
2
  import path from 'node:path'
2
-
3
- import { findLicenseFile } from '../data/license.js'
4
- import { findRoot as findPkgRoot } from '../utils/pkg.js'
3
+ import pkg from '../utils/pkg.js'
5
4
 
6
5
  export default {
7
6
  meta: {
@@ -20,8 +19,8 @@ export default {
20
19
  create(context) {
21
20
  const { filename } = context
22
21
  function check(node) {
23
- const pkgRoot = findPkgRoot(path.dirname(filename))
24
- if (!pkgRoot || !findLicenseFile(pkgRoot))
22
+ const pkgRoot = pkg.findRoot(path.dirname(filename))
23
+ if (!pkgRoot || !license.find(pkgRoot)) // eslint-disable-line e18e/prefer-array-some
25
24
  context.report({ node, messageId: 'missingFile' })
26
25
  }
27
26
  return { Program: check, Document: check }
@@ -1,6 +1,5 @@
1
- import { file, readManifest } from '../utils/extension.js'
2
-
3
- const re = { manifestFilename: /manifest\.json$/ }
1
+ import extension from '../utils/extension.js'
2
+ import regex from '../data/regex.js'
4
3
 
5
4
  export default {
6
5
  meta: {
@@ -8,7 +7,7 @@ export default {
8
7
  docs: {
9
8
  description: 'Require manifest_version 3',
10
9
  category: 'Possible Errors',
11
- recommended: false,
10
+ recommended: true,
12
11
  manifest: true
13
12
  },
14
13
  schema: [],
@@ -18,7 +17,7 @@ export default {
18
17
  },
19
18
 
20
19
  create(context) {
21
- if (re.manifestFilename.test(file.getName(context))) { // lint it directly
20
+ if (regex.filename.manifest.test(extension.file.getName(context))) { // lint it directly
22
21
  let foundVersion = null, versionNode = null
23
22
  return {
24
23
  '*'(node) {
@@ -33,7 +32,7 @@ export default {
33
32
  }
34
33
  }
35
34
  }
36
- const manifest = readManifest(context) ; if (!manifest || manifest.manifest_version == 3) return {}
35
+ const manifest = extension.manifest.read(context) ; if (!manifest || manifest.manifest_version == 3) return {}
37
36
  return { Program(node) { context.report({ node, messageId: 'notMV3' }) }}
38
37
  }
39
38
  }
@@ -1,7 +1,8 @@
1
- import { file } from '../utils/extension.js'
1
+ import extension from '../utils/extension.js'
2
+ import regex from '../data/regex.js'
2
3
 
3
4
  const defaults = { maxNameLength: 12 },
4
- re = { i18nPlaceholder: /^__MSG_.+__$/, manifestFilename: /manifest\.json$/ }
5
+ re = { i18nPlaceholder: /^__MSG_.+__$/ }
5
6
 
6
7
  function isI18n(value) { return typeof value == 'string' && re.i18nPlaceholder.test(value) }
7
8
 
@@ -43,7 +44,7 @@ export default {
43
44
  },
44
45
 
45
46
  create(context) {
46
- if (!re.manifestFilename.test(file.getName(context))) return {}
47
+ if (!regex.filename.manifest.test(extension.file.getName(context))) return {}
47
48
  const opts = context.options[0] || {},
48
49
  maxNameLength = opts.maxNameLength ?? defaults.maxNameLength,
49
50
  always = opts.always ?? false,
@@ -1,9 +1,10 @@
1
1
  /* eslint-disable security/detect-non-literal-fs-filename -- lint-time project inspection only */
2
2
 
3
+ import extension from '../utils/extension.js'
3
4
  import fs from 'node:fs'
4
5
  import path from 'node:path'
5
6
 
6
- const re = { invalidChars: /[^\w\-.]/, manifestFilename: /manifest\.json$/, reservedPrefix: /^_/ }
7
+ const re = { invalidChars: /[^\w\-.]/, reservedPrefix: /^_/ }
7
8
 
8
9
  const refKeys = [
9
10
  ['action.default_popup', manifest => manifest.action?.default_popup],
@@ -15,14 +16,6 @@ const refKeys = [
15
16
  ['side_panel.default_path', manifest => manifest.side_panel?.default_path]
16
17
  ]
17
18
 
18
- function findExtensionRoot(startPath) {
19
- let dir = path.dirname(startPath)
20
- while (dir != path.dirname(dir)) {
21
- if (fs.existsSync(path.join(dir, 'manifest.json'))) return dir
22
- dir = path.dirname(dir)
23
- }
24
- }
25
-
26
19
  function collectRefs(manifest) {
27
20
  const refs = []
28
21
  for (const [label, get] of refKeys) {
@@ -115,7 +108,7 @@ export default {
115
108
  filename = context.filename
116
109
  if (!filename || filename == '<input>') return {}
117
110
  const basename = path.basename(filename),
118
- extRoot = findExtensionRoot(filename) || path.dirname(filename),
111
+ extRoot = extension.findRoot(context) || path.dirname(filename),
119
112
  segments = path.relative(extRoot, filename).split(path.sep)
120
113
  if (noReservedPrefix)
121
114
  for (const seg of segments)