Skip to content

Commit

Permalink
Run parser tests from the official TypeScript parser (#10444)
Browse files Browse the repository at this point in the history
  • Loading branch information
nicolo-ribaudo committed Dec 3, 2019
1 parent 7195f0d commit e74efd2
Show file tree
Hide file tree
Showing 10 changed files with 583 additions and 5 deletions.
3 changes: 3 additions & 0 deletions .travis.yml
Expand Up @@ -20,6 +20,7 @@ script:
- if [ "$JOB" = "test" ]; then make -j test-ci; fi
- if [ "$JOB" = "lint" ]; then make -j code-quality-ci; fi
- if [ "$JOB" = "babel-parser-flow-tests" ]; then make -j test-flow-ci; fi
- if [ "$JOB" = "babel-parser-typescript-tests" ]; then make -j test-typescript-ci; fi
- if [ "$JOB" = "babel-parser-test262-tests" ]; then make -j test-test262-ci; fi

matrix:
Expand All @@ -42,6 +43,8 @@ matrix:
- node_js: "8"
- node_js: "node"
env: JOB=babel-parser-flow-tests
- node_js: "node"
env: JOB=babel-parser-typescript-tests
- node_js: "node"
env: JOB=babel-parser-test262-tests

Expand Down
26 changes: 21 additions & 5 deletions Makefile
@@ -1,5 +1,6 @@
FLOW_COMMIT = 09669846b7a7ca5a6c23c12d56bb3bebdafd67e9
TEST262_COMMIT = 8688c4ab79059c3097098605e69f1ee5eda6c409
TYPESCRIPT_COMMIT = 038d95144d8b93c2799d1732181c89c3d84362d5

FORCE_PUBLISH = "@babel/runtime,@babel/runtime-corejs2,@babel/runtime-corejs3,@babel/standalone,@babel/preset-env-standalone"

Expand Down Expand Up @@ -111,7 +112,7 @@ lint-js:
yarn eslint scripts $(SOURCES) '*.js' --format=codeframe

lint-ts:
scripts/tests/typescript/lint.sh
scripts/lint-ts-typings.sh

fix: fix-json fix-js

Expand Down Expand Up @@ -159,13 +160,28 @@ bootstrap-flow:
cd build/flow && git checkout $(FLOW_COMMIT)

test-flow:
node scripts/tests/flow
node scripts/parser-tests/flow

test-flow-ci: build-bundle-ci bootstrap-flow
$(MAKE) test-flow

test-flow-update-whitelist:
node scripts/tests/flow --update-whitelist
node scripts/parser-tests/flow --update-whitelist

bootstrap-typescript:
rm -rf ./build/typescript
mkdir -p ./build
git clone --branch=master --single-branch --shallow-since=2019-09-01 https://github.com/microsoft/TypeScript.git ./build/typescript
cd build/typescript && git checkout $(TYPESCRIPT_COMMIT)

test-typescript:
node scripts/parser-tests/typescript

test-typescript-ci: build-bundle-ci bootstrap-typescript
$(MAKE) test-typescript

test-typescript-update-whitelist:
node scripts/parser-tests/typescript --update-whitelist

bootstrap-test262:
rm -rf build/test262
Expand All @@ -174,13 +190,13 @@ bootstrap-test262:
cd build/test262 && git checkout $(TEST262_COMMIT)

test-test262:
node scripts/tests/test262
node scripts/parser-tests/test262

test-test262-ci: build-bundle-ci bootstrap-test262
$(MAKE) test-test262

test-test262-update-whitelist:
node scripts/tests/test262 --update-whitelist
node scripts/parser-tests/test262 --update-whitelist

# Does not work on Windows
clone-license:
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
58 changes: 58 additions & 0 deletions scripts/parser-tests/typescript/index.js
@@ -0,0 +1,58 @@
const path = require("path");
const fs = require("fs").promises;
const ts = require("typescript");
const TestRunner = require("../utils/parser-test-runner");

async function* loadTests(dir) {
const names = await fs.readdir(dir);

for (const name of names) {
const contents = await fs.readFile(path.join(dir, name), "utf8");
yield { name, contents };
}
}

const plugins = [
"typescript",
"classProperties",
"decorators-legacy",
"bigInt",
"dynamicImport",
];

const runner = new TestRunner({
testDir: path.join(
__dirname,
"../../../build/typescript/tests/cases/compiler"
),
whitelist: path.join(__dirname, "whitelist.txt"),
logInterval: 50,
shouldUpdate: process.argv.includes("--update-whitelist"),

async *getTests() {
for await (const test of loadTests(this.testDir)) {
const isTSX = test.name.slice(-4) === ".tsx";
const ast = ts.createSourceFile(
test.name,
test.contents,
ts.ScriptTarget.Latest,
false,
isTSX ? ts.ScriptKind.TSX : ts.ScriptKind.TS
);

yield {
contents: test.contents,
fileName: test.name,
id: test.name,
expectedError: ast.parseDiagnostics.length > 0,
sourceType: "module",
plugins: isTSX ? plugins.concat("jsx") : plugins,
};
}
},
});

runner.run().catch(err => {
console.error(err);
process.exitCode = 1;
});

0 comments on commit e74efd2

Please sign in to comment.