Skip to content
This repository was archived by the owner on Jan 14, 2019. It is now read-only.

BREAKING: add missing TSTypeAssertion node #79

Merged
merged 6 commits into from
Jan 4, 2019
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
1 change: 1 addition & 0 deletions src/ast-node-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,7 @@ export const AST_NODE_TYPES: { [key: string]: string } = {
TSThisType: 'TSThisType',
TSTypeAnnotation: 'TSTypeAnnotation',
TSTypeAliasDeclaration: 'TSTypeAliasDeclaration',
TSTypeAssertion: 'TSTypeAssertion',
TSTypeLiteral: 'TSTypeLiteral',
TSTypeOperator: 'TSTypeOperator',
TSTypeParameter: 'TSTypeParameter',
Expand Down
8 changes: 8 additions & 0 deletions src/convert.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2680,6 +2680,14 @@ export default function convert(config: ConvertConfig): ESTreeNode | null {
});
break;
}
case SyntaxKind.TypeAssertionExpression: {
Object.assign(result, {
type: AST_NODE_TYPES.TSTypeAssertion,
typeAnnotation: convertChildType(node.type),
expression: convertChild(node.expression)
});
break;
}
case SyntaxKind.ImportEqualsDeclaration: {
Object.assign(result, {
type: AST_NODE_TYPES.TSImportEqualsDeclaration,
Expand Down
22 changes: 18 additions & 4 deletions tests/ast-alignment/fixtures-to-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,13 @@ import jsxKnownIssues from '../jsx-known-issues';

interface Fixture {
filename: string;
jsx: boolean;
ignoreSourceType: boolean;
}

interface FixturePatternConfig {
pattern: string;
jsx: boolean;
ignoreSourceType: boolean;
}

Expand Down Expand Up @@ -69,6 +71,11 @@ function createFixturePatternConfigFor(
config.ignore = config.ignore || [];
config.fileType = config.fileType || 'js';
config.ignoreSourceType = config.ignoreSourceType || [];
const jsx =
config.fileType === 'js' ||
config.fileType === 'jsx' ||
config.fileType === 'tsx';

/**
* The TypeScript compiler gives us the "externalModuleIndicator" to allow typescript-estree do dynamically detect the "sourceType".
* Babel has similar feature sourceType='unambiguous' but its not perfect, and in some specific cases we sill have to enforce it.
Expand All @@ -85,15 +92,17 @@ function createFixturePatternConfigFor(
fixturesRequiringSourceTypeModule.push({
// It needs to be the full path from within fixtures/ for the pattern
pattern: `${fixturesSubPath}/${fixture}.src.${config.fileType}`,
ignoreSourceType: true
ignoreSourceType: true,
jsx
});
}
}
return {
pattern: `${fixturesSubPath}/!(${config.ignore.join('|')}).src.${
config.fileType
}`,
ignoreSourceType: false
ignoreSourceType: false,
jsx
};
}

Expand Down Expand Up @@ -403,7 +412,11 @@ let fixturePatternConfigsToTest = [
* Not yet supported in Babel https://github.com/babel/babel/issues/9228
* Directive field is not added to module and namespace
*/
'directive-in-namespace'
'directive-in-namespace',
/**
* there is difference in range between babel and tsep
*/
'type-assertion'
],
ignoreSourceType: [
// https://github.com/babel/babel/issues/9213
Expand Down Expand Up @@ -525,7 +538,8 @@ fixturePatternConfigsToTest.forEach(fixturePatternConfig => {
matchingFixtures.forEach(filename => {
fixturesToTest.push({
filename,
ignoreSourceType: fixturePatternConfig.ignoreSourceType
ignoreSourceType: fixturePatternConfig.ignoreSourceType,
jsx: fixturePatternConfig.jsx
});
});
});
Expand Down
87 changes: 36 additions & 51 deletions tests/ast-alignment/parse.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import codeFrame from 'babel-code-frame';
import * as parser from '../../src/parser';
import * as parseUtils from './utils';
import { ParserOptions as BabelParserOptions } from '@babel/parser';
import { ParserOptions } from '../../src/temp-types-based-on-js-source';
import { ParserPlugin } from '@babel/parser';

function createError(message: string, line: number, column: number) {
// Construct an error similar to the ones thrown by Babylon.
Expand All @@ -14,64 +13,50 @@ function createError(message: string, line: number, column: number) {
return error;
}

function parseWithBabelParser(
text: string,
parserOptions?: BabelParserOptions
) {
parserOptions = parserOptions || {};
function parseWithBabelParser(text: string, jsx: boolean = true) {
const babel = require('@babel/parser');
return babel.parse(
text,
Object.assign(
{
sourceType: 'unambiguous',
allowImportExportEverywhere: true,
allowReturnOutsideFunction: true,
ranges: true,
plugins: [
'jsx',
'typescript',
'objectRestSpread',
'decorators-legacy',
'classProperties',
'asyncGenerators',
'dynamicImport',
'estree',
'bigInt'
]
},
parserOptions
)
);
const plugins: ParserPlugin[] = [
'typescript',
'objectRestSpread',
'decorators-legacy',
'classProperties',
'asyncGenerators',
'dynamicImport',
'estree',
'bigInt'
];
if (jsx) {
plugins.push('jsx');
}

return babel.parse(text, {
sourceType: 'unambiguous',
allowImportExportEverywhere: true,
allowReturnOutsideFunction: true,
ranges: true,
plugins
});
}

function parseWithTypeScriptESTree(
text: string,
parserOptions?: ParserOptions
) {
parserOptions = parserOptions || ({} as ParserOptions);
function parseWithTypeScriptESTree(text: string, jsx: boolean = true) {
try {
return parser.parse(text, Object.assign(
{
loc: true,
range: true,
tokens: false,
comment: false,
useJSXTextNode: true,
errorOnUnknownASTType: true,
jsx: true
},
parserOptions
) as any);
return parser.parse(text, {
loc: true,
range: true,
tokens: false,
comment: false,
useJSXTextNode: true,
errorOnUnknownASTType: true,
jsx
});
} catch (e) {
throw createError(e.message, e.lineNumber, e.column);
}
}

interface ASTComparisonParseOptions {
parser: string;
typeScriptESTreeOptions?: ParserOptions;
babelParserOptions?: BabelParserOptions;
jsx?: boolean;
}

export function parse(text: string, opts: ASTComparisonParseOptions) {
Expand All @@ -88,12 +73,12 @@ export function parse(text: string, opts: ASTComparisonParseOptions) {
switch (opts.parser) {
case 'typescript-estree':
result.ast = parseUtils.normalizeNodeTypes(
parseWithTypeScriptESTree(text, opts.typeScriptESTreeOptions)
parseWithTypeScriptESTree(text, opts.jsx)
);
break;
case '@babel/parser':
result.ast = parseUtils.normalizeNodeTypes(
parseWithBabelParser(text, opts.babelParserOptions)
parseWithBabelParser(text, opts.jsx)
);
break;
default:
Expand Down
6 changes: 4 additions & 2 deletions tests/ast-alignment/spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,16 @@ fixturesToTest.forEach(fixture => {
* Parse with typescript-estree
*/
const typeScriptESTreeResult = parse(source, {
parser: 'typescript-estree'
parser: 'typescript-estree',
jsx: fixture.jsx
});

/**
* Parse the source with @babel/parser typescript-plugin
*/
const babelParserResult = parse(source, {
parser: '@babel/parser'
parser: '@babel/parser',
jsx: fixture.jsx
});

/**
Expand Down
1 change: 1 addition & 0 deletions tests/fixtures/typescript/basics/type-assertion.src.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
const foo = <any> 2;
2 changes: 2 additions & 0 deletions tests/lib/__snapshots__/semantic-diagnostics-enabled.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -1782,6 +1782,8 @@ exports[`Parse all fixtures with "errorOnTypeScriptSyntaticAndSemanticIssues" en

exports[`Parse all fixtures with "errorOnTypeScriptSyntaticAndSemanticIssues" enabled fixtures/typescript/basics/type-alias-object-without-annotation.src.ts.src 1`] = `"TEST OUTPUT: No semantic or syntactic issues found"`;

exports[`Parse all fixtures with "errorOnTypeScriptSyntaticAndSemanticIssues" enabled fixtures/typescript/basics/type-assertion.src.ts.src 1`] = `"TEST OUTPUT: No semantic or syntactic issues found"`;

exports[`Parse all fixtures with "errorOnTypeScriptSyntaticAndSemanticIssues" enabled fixtures/typescript/basics/type-guard.src.ts.src 1`] = `"TEST OUTPUT: No semantic or syntactic issues found"`;

exports[`Parse all fixtures with "errorOnTypeScriptSyntaticAndSemanticIssues" enabled fixtures/typescript/basics/type-parameters-comments.src.ts.src 1`] = `"TEST OUTPUT: No semantic or syntactic issues found"`;
Expand Down
Loading