Skip to content

Commit

Permalink
feat: add ability to specify which typescript to use (#49)
Browse files Browse the repository at this point in the history
* feat: add ability to specify which typescript to use

* refactor: fix coverage
  • Loading branch information
ikatyang authored Aug 22, 2017
1 parent 683d943 commit 9213bc1
Show file tree
Hide file tree
Showing 20 changed files with 293 additions and 174 deletions.
9 changes: 7 additions & 2 deletions src/__tests__/remap-snapshot.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import * as fs from 'fs';
import * as path from 'path';
import * as ts from 'typescript';
import { remap_snapshot } from '../remap-snapshot';

const load_content = (relative_path: string) =>
Expand All @@ -11,7 +12,9 @@ const load_content = (relative_path: string) =>
it('should remap correctly with snapshot-content string', () => {
const source_content = load_content('example.ts');
const snapshot_content = load_content('__snapshots__/example.ts.snap');
expect(remap_snapshot(snapshot_content, source_content)).toMatchSnapshot();
expect(
remap_snapshot(snapshot_content, source_content, undefined, ts),
).toMatchSnapshot();
});

it('should remap correctl with snapshot-content object', () => {
Expand All @@ -22,5 +25,7 @@ it('should remap correctl with snapshot-content object', () => {
const snapshot_content = {
'Math.max(1, 2, 3) 1': '"number"',
};
expect(remap_snapshot(snapshot_content, source_content)).toMatchSnapshot();
expect(
remap_snapshot(snapshot_content, source_content, undefined, ts),
).toMatchSnapshot();
});
2 changes: 1 addition & 1 deletion src/__tests__/setup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ it('should setup correctly', () => {

it('should setup correctly with specified tsconfig', () => {
const raw_config: RawConfig = {
tsconfig: '<cwd>/fixtures/snapshots/tsconfig.json',
tsconfig: '<rootDir>/fixtures/snapshots/tsconfig.json',
};
expect(setup(filename, raw_config, targets)).toMatchSnapshot();
});
5 changes: 3 additions & 2 deletions src/__tests__/transform-actual.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,11 @@ const transform_fixture = (
const source_filename = path.resolve(__dirname, relative_path);
const source_text = fs.readFileSync(source_filename, 'utf8');
const jest_config: JestConfig = {
rootDir: process.cwd(),
globals: { _dts_jest_: { tsconfig } },
_dts_jest_debug_: debug,
_dts_jest_internal_test_: debug,
};
return transform_actual(source_text, source_filename, jest_config);
return transform_actual(source_text, source_filename, jest_config as any);
};

it('should transform correctly', () => {
Expand Down
7 changes: 6 additions & 1 deletion src/__tests__/transform.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,16 @@
import * as fs from 'fs';
import * as path from 'path';
import { JestConfig } from '../definitions';
import { transform } from '../transform';

const transform_fixture = (relative_path: string) => {
const source_filename = path.resolve(__dirname, relative_path);
const source_text = fs.readFileSync(source_filename, 'utf8');
return transform(source_text, source_filename);
const jest_config: JestConfig = {
rootDir: process.cwd(),
globals: {},
};
return transform(source_text, source_filename, jest_config as any);
};

it('should transform correctly', () => {
Expand Down
19 changes: 15 additions & 4 deletions src/definitions.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
import * as ts from 'typescript';
import * as _ts from 'typescript';

// tslint:disable-next-line:no-var-requires
export const package_name = require('../package.json').name;

export const config_namespace = '_dts_jest_';
export const runtime_namespace = '_dts_jest_runtime_';

export const env_root_dir = 'DTS_JEST_ROOT_DIR';

export const trigger_regex = /^\s*\/\/\s*@dts-jest\b(:?\S*)\s*(.+)?\s*$/;
export enum TriggerMatchIndex {
Input,
Expand Down Expand Up @@ -78,11 +80,20 @@ export interface Expected extends Trigger {
export interface Result extends Target, Snapshot {}

export interface JestConfig {
rootDir: string;
globals: { [K in typeof config_namespace]?: RawConfig };
_dts_jest_debug_?: boolean;
_dts_jest_internal_test_?: boolean;
}

export interface RawConfig {
tsconfig?: string | ts.CompilerOptions;
type_format?: ts.TypeFormatFlags;
tsconfig?: string | _ts.CompilerOptions;
type_format?: _ts.TypeFormatFlags;
typescript?: string;
}

export interface FormattedConfig {
tsconfig: _ts.CompilerOptions;
type_format: _ts.TypeFormatFlags;
typescript: typeof _ts;
typescript_path: string;
}
5 changes: 4 additions & 1 deletion src/remap-snapshot-cli.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import * as fs from 'fs';
import * as path from 'path';
import * as ts from 'typescript';
import { remap_snapshot } from './remap-snapshot';

export = (args: string[]) => {
Expand All @@ -17,5 +18,7 @@ export = (args: string[]) => {
),
'utf8',
);
process.stdout.write(remap_snapshot(snapshot_content, source_content));
process.stdout.write(
remap_snapshot(snapshot_content, source_content, undefined, ts),
);
};
7 changes: 4 additions & 3 deletions src/remap-snapshot.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import require_from_string = require('require-from-string');
import * as ts from 'typescript';
import * as _ts from 'typescript';
import { Trigger } from './definitions';
import { create_triggers } from './utils/create-triggers';
import { default_to } from './utils/default-to';
Expand All @@ -8,7 +8,8 @@ import { get_formatted_description } from './utils/get-formatted-description';
export const remap_snapshot = (
snapshot_content: string | Record<string, string>,
source_content: string,
snapshot_filename?: string,
snapshot_filename: string | undefined,
ts: typeof _ts,
) => {
const snapshot_data =
typeof snapshot_content === 'string'
Expand All @@ -24,7 +25,7 @@ export const remap_snapshot = (
ts.ScriptTarget.Latest,
false,
);
const triggers = create_triggers(source_file);
const triggers = create_triggers(source_file, ts);

const source_content_lines = source_content.split('\n');

Expand Down
10 changes: 6 additions & 4 deletions src/setup.ts
Original file line number Diff line number Diff line change
@@ -1,24 +1,26 @@
import * as ts from 'typescript';
import { RawConfig, Result, Target } from './definitions';
import { Runtime } from './runtime';
import { create_snapshots } from './utils/create-snapshots';
import { default_to } from './utils/default-to';
import { get_tsconfig } from './utils/get-tsconfig';
import { get_config } from './utils/get-config';
import { get_root_dir } from './utils/root-dir';

export const setup = (
filename: string,
raw_config: RawConfig,
targets: Target[],
) => {
const { typescript: ts, tsconfig } = get_config(raw_config, get_root_dir());

const lines = targets.map(target => target.line);

const flag = default_to(
raw_config.type_format,
ts.TypeFormatFlags.NoTruncation,
);
const program = ts.createProgram([filename], get_tsconfig(raw_config));
const program = ts.createProgram([filename], tsconfig);

const snapshots = create_snapshots(program, filename, lines, flag);
const snapshots = create_snapshots(program, filename, lines, flag, ts);
const results = targets.reduce<{ [line: number]: Result }>(
(current_results, target) => ({
...current_results,
Expand Down
36 changes: 21 additions & 15 deletions src/transform-actual.ts
Original file line number Diff line number Diff line change
@@ -1,26 +1,29 @@
import * as ts from 'typescript';
import { AssertionFlag, JestConfig, RawConfig } from './definitions';
import { config_namespace, AssertionFlag, JestConfig } from './definitions';
import { create_expecteds } from './utils/create-expecteds';
import { create_triggers } from './utils/create-triggers';
import { default_to } from './utils/default-to';
import { get_config } from './utils/get-config';
import { get_formatted_description } from './utils/get-formatted-description';
import { get_tsconfig } from './utils/get-tsconfig';
import { rewrite_expecteds_method } from './utils/rewrite-expecteds-method';

export const transform_actual = (
source_text: string,
source_filename: string,
export const transform_actual: jest.Transformer['process'] = (
source_text,
source_filename,
jest_config: JestConfig,
) => {
const { typescript: ts, tsconfig } = get_config(
jest_config.globals[config_namespace],
jest_config.rootDir,
);

const source_file = ts.createSourceFile(
source_filename,
source_text,
ts.ScriptTarget.Latest,
false,
);
const triggers = create_triggers(source_file);
const triggers = create_triggers(source_file, ts);

const expecteds = create_expecteds(triggers, source_file);
const expecteds = create_expecteds(triggers, source_file, ts);
rewrite_expecteds_method(expecteds);

let transformed = source_text;
Expand All @@ -44,13 +47,16 @@ export const transform_actual = (
)}`;
});

if (jest_config._dts_jest_debug_ === true) {
if (jest_config._dts_jest_internal_test_ === true) {
return transformed;
}

const tsconfig: ts.CompilerOptions = {
...get_tsconfig(default_to<RawConfig>(jest_config.globals._dts_jest_, {})),
inlineSourceMap: true,
};
return ts.transpile(transformed, tsconfig, source_filename);
return ts.transpile(
transformed,
{
...tsconfig,
inlineSourceMap: true,
},
source_filename,
);
};
22 changes: 15 additions & 7 deletions src/transform.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,23 @@
import * as ts from 'typescript';
import { Group } from './definitions';
import { config_namespace, Group, JestConfig } from './definitions';
import { create_group_expression } from './utils/create-group-expression';
import { create_setup_expression } from './utils/create-setup-expression';
import { create_test_expression } from './utils/create-test-expression';
import { create_triggers } from './utils/create-triggers';
import { get_config } from './utils/get-config';
import { set_root_dir } from './utils/root-dir';

export const transform = (
source_text: string,
source_filename: string,
_jest_config?: any,
export const transform: jest.Transformer['process'] = (
source_text,
source_filename,
jest_config: JestConfig,
) => {
const { typescript: ts } = get_config(
jest_config.globals[config_namespace],
jest_config.rootDir,
);

set_root_dir(jest_config.rootDir);

const source_file = ts.createSourceFile(
source_filename,
source_text,
Expand All @@ -18,7 +26,7 @@ export const transform = (
);

let last_group: Group | undefined;
const triggers = create_triggers(source_file);
const triggers = create_triggers(source_file, ts);
return triggers
.reduce<string[]>((transformed_line_texts, trigger, index) => {
const is_diff_group = trigger.group !== last_group;
Expand Down
73 changes: 39 additions & 34 deletions src/utils/create-expecteds.ts
Original file line number Diff line number Diff line change
@@ -1,55 +1,60 @@
import * as path from 'path';
import * as ts_comment from 'ts-comment';
import * as ts from 'typescript';
import * as _ts from 'typescript';
import { Expected, Trigger } from '../definitions';
import { for_each_comment } from './for-each-comment';
import { get_expression_end_line } from './get-expression-end-line';

export const create_expecteds = (
triggers: Trigger[],
source_file: ts.SourceFile,
source_file: _ts.SourceFile,
ts: typeof _ts,
) => {
const current_triggers = triggers.slice();

const expecteds: Expected[] = [];
const unmatched_comment_lines: number[] = [];

ts_comment.for_each(source_file, (comment, scanner) => {
const match = comment.match(/^\/\/=>(.+)/);
if (match === null) {
return;
}
const position = scanner.getTokenPos();
const { line: comment_line } = source_file.getLineAndCharacterOfPosition(
position,
);

if (current_triggers.length === 0) {
unmatched_comment_lines.push(comment_line);
}

while (current_triggers.length !== 0) {
const trigger = current_triggers[0];
const expression_end_line = get_expression_end_line(trigger);
for_each_comment(
source_file,
(comment, scanner) => {
const match = comment.match(/^\/\/=>(.+)/);
if (match === null) {
return;
}
const position = scanner.getTokenPos();
const { line: comment_line } = source_file.getLineAndCharacterOfPosition(
position,
);

if (comment_line < expression_end_line) {
if (current_triggers.length === 0) {
unmatched_comment_lines.push(comment_line);
break;
}

current_triggers.shift();
while (current_triggers.length !== 0) {
const trigger = current_triggers[0];
const expression_end_line = get_expression_end_line(trigger);

if (comment_line !== expression_end_line) {
continue;
}
if (comment_line < expression_end_line) {
unmatched_comment_lines.push(comment_line);
break;
}

current_triggers.shift();

if (comment_line !== expression_end_line) {
continue;
}

const [, value] = match;
expecteds.push({
...trigger,
value: value.trim(),
});
break;
}
});
const [, value] = match;
expecteds.push({
...trigger,
value: value.trim(),
});
break;
}
},
ts,
);

if (unmatched_comment_lines.length !== 0) {
const relative_filename = path.relative(
Expand Down
Loading

0 comments on commit 9213bc1

Please sign in to comment.