Skip to content

Commit

Permalink
add smoke tests and fix failures
Browse files Browse the repository at this point in the history
  • Loading branch information
keithamus committed Oct 17, 2022
1 parent 893ebcc commit 11cc153
Show file tree
Hide file tree
Showing 10 changed files with 160 additions and 8 deletions.
2 changes: 1 addition & 1 deletion .eslintrc.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module.exports = {
parserOptions: {
ecmaVersion: 6
ecmaVersion: 2022
},
env: {
es6: true,
Expand Down
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -70,9 +70,9 @@ See [browserslist/browserslist](https://github.com/browserslist/browserslist) fo
- [no-async-iteration](./docs/no-async-iteration.md)
- [no-bigint](./docs/no-bigint.md)
- [no-bind-operator](./docs/no-bind-operator.md)
- [no-computed-class-fields](./docs/no-computed-class-fields.md)
- [no-computed-public-class-fields](./docs/no-computed-public-class-fields.md)
- [no-do-expression](./docs/no-do-expression.md)
- [no-dynamic-import](./docs/no-dynamic-import.md)
- [no-dynamic-imports](./docs/no-dynamic-imports.md)
- [no-edge-destructure-bug](./docs/no-edge-destructure-bug.md)
- [no-exponentiation-operator](./docs/no-exponentiation-operator.md)
- [no-nullish-coalescing](./docs/no-nullish-coalescing.md)
Expand All @@ -85,7 +85,7 @@ See [browserslist/browserslist](https://github.com/browserslist/browserslist) fo
- [no-public-instance-class-fields](./docs/no-public-instance-class-fields.md)
- [no-public-static-class-fields](./docs/no-public-static-class-fields.md)
- [no-regexp-lookbehind](./docs/no-regexp-lookbehind.md)
- [no-regexp-named-groups](./docs/no-regexp-named-groups.md)
- [no-regexp-named-group](./docs/no-regexp-named-group.md)
- [no-regexp-s-flag](./docs/no-regexp-s-flag.md)

## Inspiration
Expand Down
2 changes: 2 additions & 0 deletions docs/no-async-iteration.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,6 @@ These will not be allowed because they are not supported in the following browse
- Firefox < 57
- Chrome < 63

## What is the Fix?

This can be safely disabled if you intend to compile code with the `@babel/plugin-proposal-async-generator-functions` Babel plugin
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# no-computed-class-fields
# no-computed-public-class-fields

This prevents the use of computed properties as Class Fields

Expand Down
2 changes: 1 addition & 1 deletion docs/no-dynamic-import.md → docs/no-dynamic-imports.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# no-dynamic-import
# no-dynamic-imports

This prevents the use of Dynamic Imports.

Expand Down
2 changes: 1 addition & 1 deletion docs/no-edge-destructure-bug.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Edge 15-17 Destructuring Bug
# no-edge-destructure-bug

There's an interesting bug within Edge 15-17 where if you use _object destructuring_ with _default assignments_ _only_ on the _second_ argument of an _arrow function_, then you'll get a SyntaxError. In other words:

Expand Down
2 changes: 1 addition & 1 deletion docs/no-exponentiation-operator.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# No Exponentiation Operator
# no-exponentiation-operator

This prevents use of the ES2017 Expontentiation Operator:

Expand Down
File renamed without changes.
150 changes: 150 additions & 0 deletions test/check-rules.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,150 @@
/* globals describe, it*/
const config = require('../lib/index')
const fs = require('fs')
const assert = require('assert')
const path = require('path')
const docDir = './docs'

const RuleTester = require('eslint').RuleTester
const ruleTester = new RuleTester({env: {es2020: true}, parserOptions: {sourceType: 'module'}})

function rulesFromDir(dir) {
try {
return fs.readdirSync(`./${dir}`).map(f => path.basename(f, path.extname(f)))
} catch {
return []
}
}

function* extractCodeblocks(lines) {
let inCodeBlock = false
let codeLines = []
let startLine = 0
let endLine = 0
let lang = ''
for (const i in lines) {
const line = lines[i]
if (!inCodeBlock && line.startsWith('```')) {
lang = line.slice(3)
startLine = i
codeLines = []
inCodeBlock = true
continue
} else if (inCodeBlock && line.startsWith('```')) {
endLine = i
yield {code: codeLines, startLine, endLine, lang}
inCodeBlock = false
continue
}
if (inCodeBlock) {
codeLines.push(line)
}
}
}

describe('smoke tests', () => {
it('has file for each exported rule and rule for each exported file', () => {
assert.deepStrictEqual(
Object.keys(config.rules).sort(),
rulesFromDir('lib/rules').sort(),
'Expected lib/rules/*.js to be inside lib/index.js#rules'
)
})

for (const flavour in config.configs) {
describe(`${flavour} config`, () => {
it('exports valid rules', () => {
const exportedRules = new Set(Object.keys(config.rules))
const ceRules = Object.keys(config.configs[flavour].rules || []).filter(rule => rule.startsWith('escompat/'))
const violations = ceRules.filter(rule => !exportedRules.has(rule.replace(/^escompat\//, '')))
assert.deepStrictEqual(violations, [], 'All custom-elements/ rules should exist in lib/index.js#rules')
})
})
}
})

describe('test coverage', () => {
it('has tests for each rule and rules for each test', () => {
const tests = rulesFromDir('test').filter(name => name !== 'check-rules')
assert.deepStrictEqual(rulesFromDir('lib/rules'), tests, 'Expected lib/rules/*.js to have same files as test/*.js')
})
})

describe('documentation', () => {
it('has rule for each doc file and doc file for each rule', () => {
assert.deepStrictEqual(rulesFromDir(docDir), rulesFromDir('lib/rules'))
})

it('has readme link to each doc', () => {
const contents = fs.readFileSync(`./README.md`, 'utf-8').split('\n')
const i = contents.indexOf('## Rules')
let n = contents.findIndex((line, index) => index > i && line.startsWith('#'))
if (n < i) n = contents.length
const ruleLinks = contents
.slice(i + 1, n)
.filter(Boolean)
.map(x => x.trim())
const desiredRuleLinks = rulesFromDir(docDir).map(rule => `- [${rule}](${docDir}/${rule}.md)`)
assert.deepStrictEqual(desiredRuleLinks, ruleLinks, `Expected each rule in ${docDir}/*.md to have README link`)
})

for (const doc of rulesFromDir(docDir)) {
it(`has correct headings in ${doc}.md`, () => {
const contents = fs.readFileSync(`${docDir}/${doc}.md`, 'utf-8').split('\n')
let consume = true
const headings = contents.filter(line => {
// Discard lines that aren't headers or thumbs
if (!(line.startsWith('#') || line.startsWith('\ud83d'))) return false
// Ignore all sub headings/thumbs between `### Options` and `## When Not To Use It`
if (line === '### Options') {
consume = false
return true
} else if (line === '## When Not To Use It') {
consume = true
}
return consume
})
const desiredHeadings = [
`# ${doc}`,
'## What is the Fix?',
].filter(Boolean)
assert.deepStrictEqual(headings, desiredHeadings, 'Expected doc to have correct headings')
})

it(`has working examples in ${doc}.md`, () => {
const rules = {valid: [], invalid: []}
const lines = fs.readFileSync(`${docDir}/${doc}.md`, 'utf-8').split('\n')

for (const {code, startLine} of extractCodeblocks(lines)) {
const validIndex = lines.lastIndexOf('👍 Examples of **correct** code for this rule:', startLine)
const invalidIndex = lines.lastIndexOf('👎 Examples of **incorrect** code for this rule:', startLine)

if (validIndex === invalidIndex) {
continue
}

let filename = ''
if (code[0].match(/\s*\/\/ .*\.[jt]s$/)) {
filename = code[0].replace('// ', '').trim()
}

if (validIndex > invalidIndex) {
rules.valid.push({code: code.join('\n')})
} else {
rules.invalid.push({code: code.join('\n'), errors: 1, filename})
}
}

const rule = require(`../lib/rules/${doc}`)
ruleTester.run(doc, rule, rules)
})

it(`has javascript examples in ${doc}.md`, () => {
const lines = fs.readFileSync(`${docDir}/${doc}.md`, 'utf-8').split('\n')
for (const {lang, startLine} of extractCodeblocks(lines)) {
assert.equal(lang, 'js', `Expected codeblock on line ${startLine} to equal "js"`)
}
})
}
})

File renamed without changes.

0 comments on commit 11cc153

Please sign in to comment.