diff --git a/src/rules/javascript/content-scripts-file-absent.js b/src/rules/javascript/content-scripts-file-absent.js index 4a83c102..b5b092c4 100644 --- a/src/rules/javascript/content-scripts-file-absent.js +++ b/src/rules/javascript/content-scripts-file-absent.js @@ -1,8 +1,11 @@ -import { isBrowserNamespace, normalizePath } from 'utils'; +import * as path from 'path'; + +import { isBrowserNamespace } from 'utils'; import { CONTENT_SCRIPT_NOT_FOUND, CONTENT_SCRIPT_EMPTY } from 'messages/javascript'; export default { create(context) { + const dirname = path.dirname(context.getFilename()); const existingFiles = context.settings.existingFiles || {}; return { MemberExpression(node) { @@ -36,9 +39,15 @@ export default { }); return; } - const normalizedName = normalizePath(fileValue); + let normalizedName = path.resolve(dirname, fileValue); + if (path.isAbsolute(fileValue)) { + normalizedName = path.join(path.resolve('.'), path.normalize(fileValue)); + } + let existingFileNames = Object.keys(existingFiles); + existingFileNames = existingFileNames.map((fileName) => path.resolve(fileName)); + // If file exists then we are good. - if (Object.prototype.hasOwnProperty.call(existingFiles, normalizedName)) { + if (existingFileNames.includes(normalizedName)) { return; } // File not exists report an issue. diff --git a/tests/rules/javascript/test.content_scripts_file_absent.js b/tests/rules/javascript/test.content_scripts_file_absent.js index 1d5d59a6..9104fb99 100644 --- a/tests/rules/javascript/test.content_scripts_file_absent.js +++ b/tests/rules/javascript/test.content_scripts_file_absent.js @@ -34,9 +34,20 @@ describe('content_scripts_file_absent', () => { }); it('should not show an error when content script file exists', async () => { - const code = `browser.tabs.executeScript({ file: '/content_scripts/existingFile.js' });`; + const code = ` + browser.tabs.executeScript({ file: '/content_scripts/existingFile.js' }); + + // File path is relative to current file. + browser.tabs.executeScript({ file: 'anotherFolder/contentScript.js' }); + browser.tabs.executeScript({ file: 'contentScript.js' }); + `; const fileRequiresContentScript = 'file-requires-content-script.js'; - const jsScanner = createJsScanner(code, fileRequiresContentScript, { 'content_scripts/existingFile.js': '' }); + const existingFiles = { + 'content_scripts/existingFile.js': '', + 'anotherFolder/contentScript.js': '', + 'contentScript.js': '', + }; + const jsScanner = createJsScanner(code, fileRequiresContentScript, existingFiles); const { linterMessages } = await jsScanner.scan(); expect(linterMessages).toEqual([]);