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 2, 2021
1 parent f69c8ef commit 0a510a2
Show file tree
Hide file tree
Showing 56 changed files with 1,955 additions and 12 deletions.
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
2 changes: 1 addition & 1 deletion package.json
Expand Up @@ -17,7 +17,7 @@
"url": "https://github.com/typescript-eslint/typescript-eslint/issues"
},
"scripts": {
"build": "lerna run build --ignore ast-spec",
"build": "lerna run build --ignore @typescript-eslint/ast-spec",
"check:clean-workspace-after-install": "git diff --quiet --exit-code",
"check:configs": "lerna run check:configs",
"check:docs": "lerna run check:docs",
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'],
};
7 changes: 6 additions & 1 deletion packages/ast-spec/package.json
Expand Up @@ -41,6 +41,11 @@
"url": "https://opencollective.com/typescript-eslint"
},
"devDependencies": {
"@microsoft/api-extractor": "^7.13.2"
"@microsoft/api-extractor": "^7.13.2",
"@typescript-eslint/typescript-estree": "4.20.0",
"glob": "*",
"jest-specific-snapshot": "*",
"make-dir": "*",
"pretty-format": "*"
}
}
@@ -0,0 +1 @@
abstract class Foo {}
@@ -0,0 +1,47 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`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[`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,3 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`declaration ClassDeclaration abstract TSESTree - Error 1`] = `"NO ERROR"`;
@@ -0,0 +1 @@
declare class Foo {}
@@ -0,0 +1,47 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`declaration ClassDeclaration declare TSESTree - AST 1`] = `
Program {
type: "Program",
body: Array [
ClassDeclaration {
type: "ClassDeclaration",
body: ClassBody {
type: "ClassBody",
body: Array []

range: [18, 20],
loc: {
start: { column: 18, line: 1 },
end: { column: 20, line: 1 },
},
}
declare: true
id: Identifier {
type: "Identifier",
name: "Foo"

range: [14, 17],
loc: {
start: { column: 14, line: 1 },
end: { column: 17, line: 1 },
},
}
superClass: null

range: [0, 20],
loc: {
start: { column: 0, line: 1 },
end: { column: 20, line: 1 },
},
},
]
sourceType: "script"

range: [0, 21],
loc: {
start: { column: 0, line: 1 },
end: { column: 0, line: 2 },
},
}
`;
@@ -0,0 +1,56 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`declaration ClassDeclaration declare TSESTree - Tokens 1`] = `
Array [
Identifier {
type: "Identifier",
value: "declare"

range: [0, 7],
loc: {
start: { column: 0, line: 1 },
end: { column: 7, line: 1 },
},
},
Keyword {
type: "Keyword",
value: "class"

range: [8, 13],
loc: {
start: { column: 8, line: 1 },
end: { column: 13, line: 1 },
},
},
Identifier {
type: "Identifier",
value: "Foo"

range: [14, 17],
loc: {
start: { column: 14, line: 1 },
end: { column: 17, line: 1 },
},
},
Punctuator {
type: "Punctuator",
value: "{"

range: [18, 19],
loc: {
start: { column: 18, line: 1 },
end: { column: 19, line: 1 },
},
},
Punctuator {
type: "Punctuator",
value: "}"

range: [19, 20],
loc: {
start: { column: 19, line: 1 },
end: { column: 20, line: 1 },
},
},
]
`;
@@ -0,0 +1,3 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`declaration ClassDeclaration declare TSESTree - Error 1`] = `"NO ERROR"`;
@@ -0,0 +1,3 @@
@decoratorOne
@decoratorTwo
class Foo {}
@@ -0,0 +1,86 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`declaration ClassDeclaration decoratorMany TSESTree - AST 1`] = `
Program {
type: "Program",
body: Array [
ClassDeclaration {
type: "ClassDeclaration",
body: ClassBody {
type: "ClassBody",
body: Array []

range: [38, 40],
loc: {
start: { column: 10, line: 3 },
end: { column: 12, line: 3 },
},
}
decorators: Array [
Decorator {
type: "Decorator",
expression: Identifier {
type: "Identifier",
name: "decoratorOne"

range: [1, 13],
loc: {
start: { column: 1, line: 1 },
end: { column: 13, line: 1 },
},
}

range: [0, 13],
loc: {
start: { column: 0, line: 1 },
end: { column: 13, line: 1 },
},
},
Decorator {
type: "Decorator",
expression: Identifier {
type: "Identifier",
name: "decoratorTwo"

range: [15, 27],
loc: {
start: { column: 1, line: 2 },
end: { column: 13, line: 2 },
},
}

range: [14, 27],
loc: {
start: { column: 0, line: 2 },
end: { column: 13, line: 2 },
},
},
]
id: Identifier {
type: "Identifier",
name: "Foo"

range: [34, 37],
loc: {
start: { column: 6, line: 3 },
end: { column: 9, line: 3 },
},
}
superClass: null

range: [0, 40],
loc: {
start: { column: 0, line: 1 },
end: { column: 12, line: 3 },
},
},
]
sourceType: "script"

range: [0, 41],
loc: {
start: { column: 0, line: 1 },
end: { column: 0, line: 4 },
},
}
`;

0 comments on commit 0a510a2

Please sign in to comment.