Skip to content

Commit

Permalink
feat(ast-spec): add fixtures
Browse files Browse the repository at this point in the history
Add fixture tests for each and every AST node.
To make things more readable I:
- separated the AST from the tokens
  - this stops the individual snapshot from being so long it's impossible to understand
  - if there are changes to tokens - then they should be more obvious now. Before you had to expand the snapshot in github to understand that the token was part of the tokens array.
- always write error snapshot, and always write AST/token snapshots
  - This is just for consistency and to make it easier whilst you're developing.
  - It will prevent us from accidentally leaving behind error snapshots if they weren't supposed to be there.
- added a custom snapshot serializer
  - Having learned how useful they are with scope manager - I created a new one to improve the look of the snapshots.
  - Instead of sorting alphabetically, I place `type` at the top, and `range`/`loc` at the end.
  - Instead of outputting `Object` ahead of every node, instead it outputs the node `type`.
  - I adjusted the output `range`/`loc` so they take up fewer lines and are more compact.

I prefixed the snapshot names with numbers just so we can control the sorting of them. No other reason.
  • Loading branch information
bradzacher committed Apr 24, 2022
1 parent b30f7ef commit d699cbf
Show file tree
Hide file tree
Showing 362 changed files with 12,452 additions and 70 deletions.
7 changes: 7 additions & 0 deletions .github/workflows/ci.yml
Expand Up @@ -96,6 +96,12 @@ jobs:
# Website will be built by the Netlify GitHub App
yarn build --exclude website
- name: Run unit tests for ast-spec
run: yarn test
working-directory: packages/ast-spec
env:
CI: true

- name: Run unit tests for typescript-estree
run: npx nx test @typescript-eslint/typescript-estree
env:
Expand Down Expand Up @@ -308,6 +314,7 @@ jobs:
env:
CI: true

# ast-spec is internal only - so don't care about compat on other versions
# eslint-plugin-internal is internal only - so don't care about compat on other versions

website_tests:
Expand Down
1 change: 1 addition & 0 deletions .prettierignore
Expand Up @@ -12,6 +12,7 @@ packages/eslint-plugin-tslint/tests/test-tslint-rules-directory/alwaysFailRule.j
packages/eslint-plugin/src/configs/*.json
.all-contributorsrc
CONTRIBUTORS.md
packages/ast-spec/src/*/*/fixtures/_error_/*/fixture.ts

# Ignore CHANGELOG.md files to avoid issues with automated release job
CHANGELOG.md
Expand Down
32 changes: 32 additions & 0 deletions .vscode/launch.json
Expand Up @@ -242,5 +242,37 @@
"${workspaceFolder}/packages/scope-manager/dist/index.js",
],
},
{
"type": "node",
"request": "launch",
"name": "Run currently opened ast-spec test",
"cwd": "${workspaceFolder}/packages/ast-spec/",
"program": "${workspaceFolder}/node_modules/jest/bin/jest.js",
"args": [
"--runInBand",
"--no-cache",
"--no-coverage",
"${fileBasename}"
],
"sourceMaps": true,
"console": "integratedTerminal",
"internalConsoleOptions": "neverOpen",
"skipFiles": [
"${workspaceFolder}/packages/experimental-utils/src/index.ts",
"${workspaceFolder}/packages/experimental-utils/dist/index.js",
"${workspaceFolder}/packages/experimental-utils/src/ts-estree.ts",
"${workspaceFolder}/packages/experimental-utils/dist/ts-estree.js",
"${workspaceFolder}/packages/parser/src/index.ts",
"${workspaceFolder}/packages/parser/dist/index.js",
"${workspaceFolder}/packages/typescript-estree/src/index.ts",
"${workspaceFolder}/packages/typescript-estree/dist/index.js",
"${workspaceFolder}/packages/types/src/index.ts",
"${workspaceFolder}/packages/types/dist/index.js",
"${workspaceFolder}/packages/visitor-keys/src/index.ts",
"${workspaceFolder}/packages/visitor-keys/dist/index.js",
"${workspaceFolder}/packages/scope-manager/dist/index.js",
"${workspaceFolder}/packages/scope-manager/dist/index.js",
],
},
]
}
5 changes: 1 addition & 4 deletions .vscode/settings.json
Expand Up @@ -27,10 +27,7 @@
{
"icon": "jest_snapshot",
"extensions": [
".ts.shot",
".tsx.shot",
".js.shot",
".jsx.shot",
".shot",
],
"extends": "jest_snapshot"
},
Expand Down
7 changes: 7 additions & 0 deletions package.json
Expand Up @@ -55,6 +55,10 @@
"node": "^12.22.0 || ^14.17.0 || >=16.0.0"
},
"devDependencies": {
"@babel/code-frame": "^7.14.5",
"@babel/eslint-parser": "^7.15.4",
"@babel/parser": "^7.15.7",
"@babel/types": "^7.15.6",
"@commitlint/cli": "^16.0.1",
"@commitlint/config-conventional": "^16.0.0",
"@nrwl/cli": "13.8.5",
Expand All @@ -63,6 +67,7 @@
"@nrwl/workspace": "13.8.5",
"@swc/core": "^1.2.159",
"@swc/jest": "^0.2.20",
"@types/babel__code-frame": "^7.0.3",
"@types/debug": "^4.1.7",
"@types/eslint-visitor-keys": "^1.0.0",
"@types/glob": "^7.2.0",
Expand Down Expand Up @@ -93,6 +98,8 @@
"glob": "^8.0.1",
"husky": "^7.0.4",
"jest": "^27.3.1",
"jest-diff": "^27.3.1",
"jest-snapshot": "^27.3.1",
"jest-specific-snapshot": "^5.0.0",
"lerna": "^4.0.0",
"lint-staged": "^12.0.2",
Expand Down
5 changes: 5 additions & 0 deletions packages/ast-spec/jest.config.js
Expand Up @@ -5,4 +5,9 @@
module.exports = {
...require('../../jest.config.base.js'),
testRegex: ['./tests/.+\\.test\\.ts$'],
collectCoverage: false,
collectCoverageFrom: ['src/**/*.{js,jsx,ts,tsx}'],
moduleFileExtensions: ['ts', 'tsx', 'js', 'jsx', 'json', 'node'],
coverageReporters: ['text-summary', 'lcov'],
setupFilesAfterEnv: ['./tests/util/setupJest.ts'],
};
12 changes: 12 additions & 0 deletions packages/ast-spec/package.json
Expand Up @@ -32,16 +32,28 @@
"build": "tsc -b tsconfig.build.json && api-extractor run --local",
"clean": "tsc -b tsconfig.build.json --clean",
"postclean": "rimraf dist && rimraf _ts3.4 && rimraf .rollup.cache && rimraf coverage",
"clean-fixtures": "rimraf -g \"./src/**/fixtures/**/snapshots\"",
"format": "prettier --write \"./**/*.{ts,js,json,md}\" --ignore-path ../../.prettierignore",
"lint": "eslint . --ext .js,.ts --ignore-path='../../.eslintignore'",
"test": "jest",
"typecheck": "tsc -p tsconfig.json --noEmit"
},
"funding": {
"type": "opencollective",
"url": "https://opencollective.com/typescript-eslint"
},
"devDependencies": {
"@babel/core": "*",
"@babel/eslint-parser": "*",
"@babel/parser": "*",
"@microsoft/api-extractor": "^7.18.16",
"@types/babel__core": "*",
"glob": "*",
"jest-diff": "*",
"jest-snapshot": "*",
"jest-specific-snapshot": "*",
"make-dir": "*",
"pretty-format": "*",
"typescript": "*"
}
}
1 change: 1 addition & 0 deletions packages/ast-spec/src/base/TSHeritageBase.ts
Expand Up @@ -3,6 +3,7 @@ import type { Expression } from '../unions/Expression';
import type { BaseNode } from './BaseNode';

export interface TSHeritageBase extends BaseNode {
// TODO(error handling) - this should be restricted to MemberExpression | Identifier
expression: Expression;
typeParameters?: TSTypeParameterInstantiation;
}
@@ -0,0 +1 @@
class Foo implements 'thing' {}
@@ -0,0 +1,3 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`AST Fixtures declaration ClassDeclaration _error_ implements-non-identifier TSESTree - Error 1`] = `"NO ERROR"`;
@@ -0,0 +1,3 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`AST Fixtures declaration ClassDeclaration _error_ implements-non-identifier Babel - Error 1`] = `[SyntaxError: Unexpected token (1:21)]`;
@@ -0,0 +1,3 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`AST Fixtures declaration ClassDeclaration _error_ implements-non-identifier Error Alignment 1`] = `"Babel errored but TSESTree didn't"`;
@@ -0,0 +1 @@
class Foo;
@@ -0,0 +1,3 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`AST Fixtures declaration ClassDeclaration _error_ no-body TSESTree - Error 1`] = `[TSError: '{' expected.]`;
@@ -0,0 +1,3 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`AST Fixtures declaration ClassDeclaration _error_ no-body Babel - Error 1`] = `[SyntaxError: Unexpected token, expected "{" (1:9)]`;
@@ -0,0 +1,3 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`AST Fixtures declaration ClassDeclaration _error_ no-body Error Alignment 1`] = `"Both errored"`;
@@ -0,0 +1 @@
class 'Foo' {}
@@ -0,0 +1,3 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`AST Fixtures declaration ClassDeclaration _error_ non-identifier-name TSESTree - Error 1`] = `[TSError: '{' expected.]`;
@@ -0,0 +1,3 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`AST Fixtures declaration ClassDeclaration _error_ non-identifier-name Babel - Error 1`] = `[SyntaxError: A class name is required. (1:6)]`;
@@ -0,0 +1,3 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`AST Fixtures declaration ClassDeclaration _error_ non-identifier-name Error Alignment 1`] = `"Both errored"`;
@@ -0,0 +1 @@
abstract class Foo {}
@@ -0,0 +1,47 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`AST Fixtures declaration ClassDeclaration abstract TSESTree - AST 1`] = `
Program {
type: "Program",
body: Array [
ClassDeclaration {
type: "ClassDeclaration",
abstract: true,
body: ClassBody {
type: "ClassBody",
body: Array [],
range: [19, 21],
loc: {
start: { column: 19, line: 1 },
end: { column: 21, line: 1 },
},
},
id: Identifier {
type: "Identifier",
name: "Foo",
range: [15, 18],
loc: {
start: { column: 15, line: 1 },
end: { column: 18, line: 1 },
},
},
superClass: null,
range: [0, 21],
loc: {
start: { column: 0, line: 1 },
end: { column: 21, line: 1 },
},
},
],
sourceType: "script",
range: [0, 22],
loc: {
start: { column: 0, line: 1 },
end: { column: 0, line: 2 },
},
}
`;
@@ -0,0 +1,56 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`AST Fixtures declaration ClassDeclaration abstract TSESTree - Tokens 1`] = `
Array [
Identifier {
type: "Identifier",
value: "abstract",
range: [0, 8],
loc: {
start: { column: 0, line: 1 },
end: { column: 8, line: 1 },
},
},
Keyword {
type: "Keyword",
value: "class",
range: [9, 14],
loc: {
start: { column: 9, line: 1 },
end: { column: 14, line: 1 },
},
},
Identifier {
type: "Identifier",
value: "Foo",
range: [15, 18],
loc: {
start: { column: 15, line: 1 },
end: { column: 18, line: 1 },
},
},
Punctuator {
type: "Punctuator",
value: "{",
range: [19, 20],
loc: {
start: { column: 19, line: 1 },
end: { column: 20, line: 1 },
},
},
Punctuator {
type: "Punctuator",
value: "}",
range: [20, 21],
loc: {
start: { column: 20, line: 1 },
end: { column: 21, line: 1 },
},
},
]
`;
@@ -0,0 +1,47 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`AST Fixtures declaration ClassDeclaration abstract Babel - AST 1`] = `
Program {
type: "Program",
body: Array [
ClassDeclaration {
type: "ClassDeclaration",
abstract: true,
body: ClassBody {
type: "ClassBody",
body: Array [],
range: [19, 21],
loc: {
start: { column: 19, line: 1 },
end: { column: 21, line: 1 },
},
},
id: Identifier {
type: "Identifier",
name: "Foo",
range: [15, 18],
loc: {
start: { column: 15, line: 1 },
end: { column: 18, line: 1 },
},
},
superClass: null,
range: [0, 21],
loc: {
start: { column: 0, line: 1 },
end: { column: 21, line: 1 },
},
},
],
sourceType: "script",
range: [0, 22],
loc: {
start: { column: 0, line: 1 },
end: { column: 0, line: 2 },
},
}
`;

0 comments on commit d699cbf

Please sign in to comment.