Skip to content

Commit

Permalink
initial working commit
Browse files Browse the repository at this point in the history
  • Loading branch information
amilajack committed Feb 9, 2019
1 parent aa3905b commit b294348
Show file tree
Hide file tree
Showing 10 changed files with 208 additions and 59 deletions.
6 changes: 4 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
],
"homepage": "https://github.com/amilajack/eslint-plugin-compat#readme",
"scripts": {
"build": "cross-env NODE_ENV=production rm -rf lib && babel src --out-dir lib",
"build": "cross-env NODE_ENV=production rm -rf lib && babel src --out-dir lib --source-maps inline",
"flow": "flow",
"flow-typed": "flow-typed install --ignoreDeps peer dev",
"lint": "eslint --cache --format=node_modules/eslint-formatter-pretty .",
Expand Down Expand Up @@ -59,9 +59,11 @@
},
"dependencies": {
"@babel/runtime": "^7.3.1",
"ast-metadata-inferer": "^0.1.1-0",
"browserslist": "^4.4.1",
"caniuse-db": "^1.0.30000935",
"mdn-browser-compat-data": "^0.0.66"
"mdn-browser-compat-data": "^0.0.66",
"semver": "^5.6.0"
},
"peerDependencies": {
"eslint": "^3.0.0 || ^4.0.0 || ^5.0.0"
Expand Down
4 changes: 2 additions & 2 deletions src/Lint.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,11 @@ export default function Lint(
targets: Targets = ['chrome', 'firefox', 'safari', 'edge'],
polyfills: Set<string> = new Set()
): isValidObject {
// Find the corresponding rules for a eslintNode by it's ASTNodeType
// Find the corresponding rules for a eslintNode by it's astNodeType
const failingRule = rules
.filter(
(rule: Node): boolean =>
rule.ASTNodeType === eslintNode.type &&
rule.astNodeType === eslintNode.type &&
// Check if polyfill is provided
!polyfills.has(rule.id)
)
Expand Down
8 changes: 5 additions & 3 deletions src/LintTypes.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
// @flow
export type node = {
type?: string,
name?: string
type?: 'MemberExpression' | 'NewExpression' | 'CallExpression',
name?: string,
object: string,
property: string | void
};

export type Target = {
Expand All @@ -24,7 +26,7 @@ export type ESLintNode = {
} & node;

export type Node = {
ASTNodeType: string,
astNodeType: string,
id: string,
object: string,
property?: string,
Expand Down
2 changes: 1 addition & 1 deletion src/Versioning.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// @flow
import browserslist from 'browserslist'; // eslint-disable-line
import browserslist from 'browserslist';
import type { BrowserListConfig } from './rules/compat';

type TargetListItem = {
Expand Down
59 changes: 24 additions & 35 deletions src/providers/CanIUseProvider.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// @flow
// $FlowFixMe: Flow import error
import caniuseRecord from 'caniuse-db/fulldata-json/data-2.0.json'; // eslint-disable-line
import caniuseRecords from 'caniuse-db/fulldata-json/data-2.0.json';
import type { Node, ESLintNode, Targets, Target } from '../LintTypes';

type TargetMetadata = {
Expand All @@ -14,7 +14,7 @@ type CanIUseStats = {
}
};

type CanIUseRecord = {
type CanIUseRecords = {
data: CanIUseStats
};

Expand Down Expand Up @@ -71,7 +71,7 @@ function formatTargetNames(target: Target): string {
}

/**
* Check version for the range format.
* Check if a browser version is in the range format
* ex. 10.0-10.2
*/
function versionIsRange(version: string): boolean {
Expand All @@ -88,10 +88,11 @@ function compareRanges(targetVersion: number, statsVersion: string): boolean {
/*
* Check the CanIUse database to see if targets are supported
*/
function canIUseSupported(
stats: CanIUseStats,
function canIUseIsNotSupported(
node: Node,
{ version, target, parsedVersion }: Target
): boolean {
const { stats } = (caniuseRecords: CanIUseRecords).data[node.id];
const targetStats = stats[target];
return versionIsRange(version)
? Object.keys(targetStats).some(
Expand All @@ -111,9 +112,8 @@ export function getUnsupportedTargets(
node: Node,
targets: Targets
): Array<string> {
const { stats } = (caniuseRecord: CanIUseRecord).data[node.id];
return targets
.filter(target => canIUseSupported(stats, target))
.filter(target => canIUseIsNotSupported(node, target))
.map(formatTargetNames);
}

Expand Down Expand Up @@ -146,119 +146,108 @@ function isValid(
return true;
}

return getUnsupportedTargets(node, targets).length === 0;
return !getUnsupportedTargets(node, targets).length;
}

//
// TODO: Migrate to compat-db
// TODO: Refactor isValid(), remove from rules
//

const CanIUseProvider: Array<Node> = [
// new ServiceWorker()
{
id: 'serviceworkers',
ASTNodeType: 'NewExpression',
astNodeType: 'NewExpression',
object: 'ServiceWorker'
},
{
id: 'serviceworkers',
ASTNodeType: 'MemberExpression',
astNodeType: 'MemberExpression',
object: 'navigator',
property: 'serviceWorker'
},
// document.querySelector()
{
id: 'queryselector',
ASTNodeType: 'MemberExpression',
astNodeType: 'MemberExpression',
object: 'document',
property: 'querySelector'
},
// WebAssembly
{
id: 'wasm',
ASTNodeType: 'MemberExpression',
astNodeType: 'MemberExpression',
object: 'WebAssembly'
},
// IntersectionObserver
{
id: 'intersectionobserver',
ASTNodeType: 'NewExpression',
astNodeType: 'NewExpression',
object: 'IntersectionObserver'
},
// PaymentRequest
{
id: 'payment-request',
ASTNodeType: 'NewExpression',
astNodeType: 'NewExpression',
object: 'PaymentRequest'
},
// Promises
{
id: 'promises',
ASTNodeType: 'NewExpression',
astNodeType: 'NewExpression',
object: 'Promise'
},
{
id: 'promises',
ASTNodeType: 'MemberExpression',
astNodeType: 'MemberExpression',
object: 'Promise',
property: 'resolve'
},
{
id: 'promises',
ASTNodeType: 'MemberExpression',
astNodeType: 'MemberExpression',
object: 'Promise',
property: 'all'
},
{
id: 'promises',
ASTNodeType: 'MemberExpression',
astNodeType: 'MemberExpression',
object: 'Promise',
property: 'race'
},
{
id: 'promises',
ASTNodeType: 'MemberExpression',
astNodeType: 'MemberExpression',
object: 'Promise',
property: 'reject'
},
// fetch
{
id: 'fetch',
ASTNodeType: 'CallExpression',
astNodeType: 'CallExpression',
object: 'fetch'
},
// document.currentScript()
{
id: 'document-currentscript',
ASTNodeType: 'MemberExpression',
astNodeType: 'MemberExpression',
object: 'document',
property: 'currentScript'
},
// URL
{
id: 'url',
ASTNodeType: 'NewExpression',
astNodeType: 'NewExpression',
object: 'URL'
},
// URLSearchParams
{
id: 'urlsearchparams',
ASTNodeType: 'NewExpression',
astNodeType: 'NewExpression',
object: 'URLSearchParams'
},
// performance.now()
{
id: 'high-resolution-time',
ASTNodeType: 'MemberExpression',
astNodeType: 'MemberExpression',
object: 'performance',
property: 'now'
},
{
id: 'object-values',
ASTNodeType: 'MemberExpression',
object: 'Object',
property: 'values'
}
].map(rule =>
Object.assign({}, rule, {
Expand Down
Loading

0 comments on commit b294348

Please sign in to comment.