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 Oct 28, 2021
1 parent 92f21d4 commit 274305a
Show file tree
Hide file tree
Showing 345 changed files with 11,278 additions and 52 deletions.
7 changes: 7 additions & 0 deletions .github/workflows/ci.yml
Expand Up @@ -94,6 +94,12 @@ jobs:
run: |
yarn build
- 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 @@ -292,6 +298,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

publish_canary_version:
Expand Down
2 changes: 2 additions & 0 deletions .nxignore
@@ -0,0 +1,2 @@
# See the notes in the file for why this is needed
packages/ast-spec/tests/util/typescript-estree-import.ts
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 @@ -196,5 +196,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",
],
},
]
}
1 change: 1 addition & 0 deletions .vscode/settings.json
Expand Up @@ -31,6 +31,7 @@
".tsx.shot",
".js.shot",
".jsx.shot",
".shot",
],
"extends": "jest_snapshot"
},
Expand Down
3 changes: 3 additions & 0 deletions package.json
Expand Up @@ -72,6 +72,7 @@
},
"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": "^13.1.0",
Expand Down Expand Up @@ -109,6 +110,8 @@
"glob": "^7.1.7",
"husky": "^7.0.4",
"jest": "^27.3.1",
"jest-diff": "^27.0.2",
"jest-snapshot": "^27.0.2",
"jest-specific-snapshot": "^5.0.0",
"lerna": "^3.22.1",
"lint-staged": "^11.1.2",
Expand Down
1 change: 1 addition & 0 deletions packages/ast-spec/jest.config.js
Expand Up @@ -17,4 +17,5 @@ module.exports = {
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 extends 'Thing' {}
@@ -0,0 +1,3 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

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

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

exports[`AST Fixtures declaration ClassDeclaration _error_ extends-literal Error Alignment 1`] = `"No errors"`;
@@ -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 274305a

Please sign in to comment.