Skip to content

Commit

Permalink
Adding linter and fixing IE11 / external override bugs.
Browse files Browse the repository at this point in the history
  • Loading branch information
joeldenning committed Aug 31, 2020
1 parent 47be99e commit 4b0c3aa
Show file tree
Hide file tree
Showing 6 changed files with 458 additions and 22 deletions.
28 changes: 28 additions & 0 deletions .eslintrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
{
"extends": ["important-stuff"],
"plugins": ["es5"],
"parser": "@babel/eslint-parser",
"rules": {
"es5/no-es6-methods": "error",
"es5/no-es6-static-methods": "error",
"es5/no-binary-and-octal-literals": "error",
"es5/no-classes": "off",
"es5/no-for-of": "off",
"es5/no-generators": "error",
"es5/no-object-super": "error",
"es5/no-typeof-symbol": "error",
"es5/no-unicode-code-point-escape": "error",
"es5/no-unicode-regex": "error",
"es5/no-computed-properties": "off",
"es5/no-destructuring": "off",
"es5/no-default-parameters": "off",
"es5/no-spread": "off",
"es5/no-modules": "off",
"es5/no-exponentiation-operator": "off",
"es5/no-block-scoping": "off",
"es5/no-arrow-functions": "off",
"es5/no-shorthand-properties": "off",
"es5/no-rest-parameters": "off",
"es5/no-template-literals": "off"
}
}
9 changes: 7 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,12 @@
"copy-test-files": "copyfiles test/**/* dist -f",
"test-serve": "serve .",
"test": "concurrently yarn:watch yarn:copy-test-files yarn:test-serve",
"lint": "eslint src",
"prepublishOnly": "yarn build"
},
"husky": {
"hooks": {
"pre-commit": "pretty-quick --staged"
"pre-commit": "concurrently \"yarn lint\" \"pretty-quick --staged\""
}
},
"browserslist": [
Expand All @@ -27,13 +28,17 @@
"dist"
],
"devDependencies": {
"@babel/core": "^7.11.0",
"@babel/core": "^7.11.4",
"@babel/eslint-parser": "^7.11.4",
"@babel/plugin-proposal-class-properties": "^7.10.4",
"@babel/plugin-transform-react-jsx": "^7.10.4",
"@babel/preset-env": "^7.11.0",
"concurrently": "^5.2.0",
"copyfiles": "^2.3.0",
"cross-env": "^7.0.2",
"eslint": "^7.7.0",
"eslint-config-important-stuff": "^1.1.0",
"eslint-plugin-es5": "^1.5.0",
"husky": "^4.2.5",
"preact": "^10.4.6",
"prettier": "^2.0.5",
Expand Down
14 changes: 9 additions & 5 deletions src/api/js-api.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { escapeStringRegexp } from "./js-api-util";
import { includes } from "../util/includes.js";

const localStoragePrefix = "import-map-override:";
const disabledOverridesLocalStorageKey = "import-map-overrides-disabled";
Expand Down Expand Up @@ -40,6 +41,7 @@ if (domainsElement) {
const deniedDomains = content.slice(denyListPrefix.length).split(",");
isDisabled = deniedDomains.some(matchHostname);
} else {
// eslint-disable-next-line no-console
console.log(
`Invalid ${domainsMeta} meta content attribute - must start with ${allowListPrefix} or ${denyListPrefix}`
);
Expand Down Expand Up @@ -217,7 +219,7 @@ function init() {
},
disableOverride(moduleName) {
const disabledOverrides = imo.getDisabledOverrides();
if (!disabledOverrides.includes(moduleName)) {
if (!includes(disabledOverrides, moduleName)) {
localStorage.setItem(
disabledOverridesLocalStorageKey,
JSON.stringify(disabledOverrides.concat(moduleName))
Expand Down Expand Up @@ -250,7 +252,7 @@ function init() {
return disabledOverrides ? JSON.parse(disabledOverrides) : [];
},
isDisabled(moduleName) {
return imo.getDisabledOverrides().includes(moduleName);
return includes(imo.getDisabledOverrides(), moduleName);
},
getExternalOverrides() {
let localStorageValue = localStorage.getItem(
Expand All @@ -261,7 +263,7 @@ function init() {
addExternalOverride(url) {
url = new URL(url, document.baseURI).href;
const overrides = imo.getExternalOverrides();
if (overrides.includes(url)) {
if (includes(overrides, url)) {
return false;
} else {
localStorage.setItem(
Expand All @@ -274,7 +276,7 @@ function init() {
},
removeExternalOverride(url) {
const overrides = imo.getExternalOverrides();
if (overrides.includes(url)) {
if (includes(overrides, url)) {
localStorage.setItem(
externalOverridesLocalStorageKey,
JSON.stringify(overrides.filter((override) => override !== url))
Expand Down Expand Up @@ -305,7 +307,9 @@ function init() {
(externalOverrideMapPromises[importMapUrl] = fetchExternalMap(
importMapUrl
));
return promise.then(() => invalidExternalMaps.includes(importMapUrl));
return promise.then(() =>
includes(imo.invalidExternalMaps, importMapUrl)
);
},
invalidExternalMaps: [],
};
Expand Down
14 changes: 7 additions & 7 deletions src/ui/list/list.component.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { h, Component, render } from "preact";
import { importMapType } from "../../api/js-api";
import { includes } from "../../util/includes.js";
import ModuleDialog from "./module-dialog.component";
import ExternalImportMap from "./external-importmap-dialog.component";
import { devLibs } from "../dev-lib-overrides.component";
Expand Down Expand Up @@ -84,7 +84,7 @@ export default class List extends Component {
moduleName,
defaultUrl: this.state.notOverriddenMap.imports[moduleName],
overrideUrl: overrideMap[moduleName],
disabled: disabledModules.includes(moduleName),
disabled: includes(disabledModules, moduleName),
};
if (mod.disabled) {
disabledOverrides.push(mod);
Expand Down Expand Up @@ -124,12 +124,12 @@ export default class List extends Component {
Object.keys(overrideMap)
.filter(this.filterModuleNames)
.forEach((moduleName) => {
if (!notOverriddenKeys.includes(moduleName)) {
if (!includes(notOverriddenKeys, moduleName)) {
const mod = {
moduleName,
defaultUrl: null,
overrideUrl: overrideMap[moduleName],
disabled: disabledModules.includes(moduleName),
disabled: includes(disabledModules, moduleName),
};

if (mod.disabled) {
Expand Down Expand Up @@ -422,7 +422,7 @@ export default class List extends Component {

filterModuleNames = (moduleName) => {
return this.state.searchVal.trim().length > 0
? moduleName.includes(this.state.searchVal)
? includes(moduleName, this.state.searchVal)
: true;
};
}
Expand Down Expand Up @@ -467,10 +467,10 @@ function getExternalMaps() {
workingNextPageMaps = [];

for (let externalMap of allExternalMaps) {
if (window.importMapOverrides.invalidExternalMaps.includes(externalMap)) {
if (includes(window.importMapOverrides.invalidExternalMaps, externalMap)) {
brokenMaps.push(externalMap);
} else {
if (allCurrentPageMaps.includes(externalMap)) {
if (includes(allCurrentPageMaps, externalMap)) {
workingCurrentPageMaps.push(externalMap);
} else {
workingNextPageMaps.push(externalMap);
Expand Down
14 changes: 14 additions & 0 deletions src/util/includes.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
export function includes(obj, item) {
if (Array.isArray(obj)) {
for (let i = 0; i < obj.length; i++) {
if (obj[i] === item) {
return true;
}
}
return false;
} else if (typeof obj === "string") {
return obj.indexOf(item) >= 0;
} else {
throw Error(`Can't call includes on ${typeof obj}`);
}
}
Loading

0 comments on commit 4b0c3aa

Please sign in to comment.