Merge pull request #1873 from pardeepshokeen/content-script-bug

fixes relative path in content script rule
This commit is contained in:
Christopher Grebs 2018-03-13 17:37:55 +01:00 коммит произвёл GitHub
Родитель cd3d612aad c925da2050
Коммит 12d12c2678
Не найден ключ, соответствующий данной подписи
Идентификатор ключа GPG: 4AEE18F83AFDEB23
2 изменённых файлов: 25 добавлений и 5 удалений

Просмотреть файл

@ -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.

Просмотреть файл

@ -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([]);