Skip to content

Fix test cases #118

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Dec 7, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@
"@types/eslint-visitor-keys": "^1.0.0",
"@types/mocha": "^9.0.0",
"@types/node": "^16.0.0",
"@types/semver": "^7.3.9",
"@typescript-eslint/eslint-plugin": "^5.4.0",
"@typescript-eslint/parser": "^5.4.0",
"code-red": "^0.2.3",
Expand All @@ -75,6 +76,7 @@
"nyc": "^15.1.0",
"prettier": "^2.0.5",
"prettier-plugin-svelte": "^2.5.0",
"semver": "^7.3.5",
"string-replace-loader": "^3.0.3",
"svelte": "^3.44.1",
"ts-node": "^10.4.0",
Expand Down
47 changes: 42 additions & 5 deletions tests/src/parser/parser.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
/* global require -- node */
import assert from "assert"
import fs from "fs"
import semver from "semver"

import { traverseNodes } from "../../../src/traverse"
import { parseForESLint } from "../../../src"
Expand All @@ -24,6 +26,7 @@ describe("Check for AST.", () => {
inputFileName,
outputFileName,
scopeFileName,
requirements,
} of listupFixtures()) {
describe(inputFileName, () => {
let result: any
Expand All @@ -34,11 +37,26 @@ describe("Check for AST.", () => {
const output = fs.readFileSync(outputFileName, "utf8")
assert.strictEqual(astJson, output)
})
it("most to generate the expected scope.", () => {
const json = scopeToJSON(result.scopeManager)
const output = fs.readFileSync(scopeFileName, "utf8")
assert.strictEqual(json, output)
})
if (canTest(requirements, "scope"))
it("most to generate the expected scope.", () => {
let json: any = scopeToJSON(result.scopeManager)
let output: any = fs.readFileSync(scopeFileName, "utf8")

if (
result.services?.program // use ts parser
) {
// eslint-disable-next-line @typescript-eslint/no-require-imports, @typescript-eslint/no-var-requires -- ignore
const pkg = require("@typescript-eslint/parser/package.json")
if (!semver.satisfies(pkg.version, "^5.6.0")) {
// adjust global scope
json = JSON.parse(json)
output = JSON.parse(output)
json.variables = output.variables
}
}

assert.deepStrictEqual(json, output)
})

it("location must be correct.", () => {
// check tokens
Expand All @@ -58,6 +76,25 @@ describe("Check for AST.", () => {
}
})

function canTest(
requirements: { scope?: Record<string, string> },
key: "scope",
) {
const obj = requirements[key]
if (obj) {
if (
Object.entries(obj).some(([pkgName, pkgVersion]) => {
// eslint-disable-next-line @typescript-eslint/no-require-imports, @typescript-eslint/no-var-requires -- ignore
const pkg = require(`${pkgName}/package.json`)
return !semver.satisfies(pkg.version, pkgVersion)
})
) {
return false
}
}
return true
}

function checkTokens(ast: SvelteProgram, input: string) {
const allTokens = [...ast.tokens, ...ast.comments].sort(
(a, b) => a.range[0] - b.range[0],
Expand Down
13 changes: 13 additions & 0 deletions tests/src/parser/test-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@ export function* listupFixtures(): IterableIterator<{
outputFileName: string
scopeFileName: string
typeFileName: string | null
requirements: {
scope?: Record<string, string>
}
getRuleOutputFileName: (ruleName: string) => string
}> {
yield* listupFixturesImpl(AST_FIXTURE_ROOT)
Expand All @@ -33,6 +36,9 @@ function* listupFixturesImpl(dir: string): IterableIterator<{
outputFileName: string
scopeFileName: string
typeFileName: string | null
requirements: {
scope?: Record<string, string>
}
getRuleOutputFileName: (ruleName: string) => string
}> {
for (const filename of fs.readdirSync(dir)) {
Expand All @@ -50,6 +56,10 @@ function* listupFixturesImpl(dir: string): IterableIterator<{
/input\.svelte$/u,
"type-output.svelte",
)
const requirementsFileName = inputFileName.replace(
/input\.svelte$/u,
"requirements.json",
)

const input = fs.readFileSync(inputFileName, "utf8")
yield {
Expand All @@ -58,6 +68,9 @@ function* listupFixturesImpl(dir: string): IterableIterator<{
outputFileName,
scopeFileName,
typeFileName: fs.existsSync(typeFileName) ? typeFileName : null,
requirements: fs.existsSync(requirementsFileName)
? JSON.parse(fs.readFileSync(requirementsFileName, "utf-8"))
: {},
getRuleOutputFileName: (ruleName) => {
return inputFileName.replace(
/input\.svelte$/u,
Expand Down