Skip to content

Commit

Permalink
Merge branch 'williamboman-add-ts-support'
Browse files Browse the repository at this point in the history
* williamboman-add-ts-support:
  Update to babel 7.0.0-rc.1
  Unbreak test suite for babel 7
  Identify rest properties correctly in visitIdentifierNodes
  fix linting and tests
  lib/parse: use typescript plugin for ts files
  upgrade to babel 7
  use @babel/parser and use typescript over flow
  • Loading branch information
trotzig committed Aug 13, 2018
2 parents 4a84f29 + 04840ec commit f64b7bd
Show file tree
Hide file tree
Showing 21 changed files with 198 additions and 92 deletions.
10 changes: 3 additions & 7 deletions .babelrc
Original file line number Diff line number Diff line change
@@ -1,12 +1,8 @@
{
"presets": [["airbnb", {
"targets": {
"node": 6
}
}]],
"presets": ["@babel/preset-react", "@babel/preset-env"],
"plugins": [
"add-module-exports",
"transform-flow-strip-types",
"transform-class-properties"
"@babel/plugin-transform-flow-strip-types",
"@babel/plugin-proposal-class-properties"
]
}
17 changes: 17 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# EditorConfig helps developers define and maintain consistent
# coding styles between different editors and IDEs
# editorconfig.org

root = true

[*]
indent_style = space
indent_size = 2

end_of_line = lf
charset = utf-8
trim_trailing_whitespace = true
insert_final_newline = true

[*.md]
trim_trailing_whitespace = false
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,7 @@
node_modules
/coverage/
.importjs.db

npm-error.log*
yarn-error.log*
yarn-debug.log*
2 changes: 1 addition & 1 deletion lib/Configuration.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ const DEFAULT_CONFIG = {
moduleSideEffectImports: (): Array<string> => [],
sortImports: true,
emptyLineBetweenGroups: true,
stripFileExtensions: ['.js', '.jsx'],
stripFileExtensions: ['.js', '.jsx', '.ts', '.tsx'],
danglingCommas: true,
tab: ' ',
useRelativePaths: true,
Expand Down
2 changes: 1 addition & 1 deletion lib/ImportStatement.js
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ export default class ImportStatement {
const variableIndex = this.namedImports.findIndex(({
localName,
}: {
localName: String,
localName: string,
}): boolean =>
localName === variableName);
if (variableIndex !== -1) {
Expand Down
4 changes: 2 additions & 2 deletions lib/Importer.js
Original file line number Diff line number Diff line change
Expand Up @@ -84,11 +84,11 @@ export default class Importer {
this.messages = Array.from(this.config.messages);
this.unresolvedImports = {};
try {
this.ast = parse(this.editor.currentFileContent());
this.ast = parse(this.editor.currentFileContent(), this.pathToCurrentFile);
} catch (e) {
if (e instanceof SyntaxError) {
this.message(`SyntaxError: ${e.message}`);
this.ast = parse('');
this.ast = parse('', '');
} else {
throw new Error(e);
}
Expand Down
2 changes: 1 addition & 1 deletion lib/ModuleFinder.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ function defaultExportNames(pathToFile) {
let fileName = parsed.name;
let dirName = path.basename(parsed.dir);

if (/package\.json|index\.jsx?/.test(parsed.base)) {
if (/package\.json|index\.[jt]sx?/.test(parsed.base)) {
fileName = dirName;
dirName = path.basename(path.dirname(parsed.dir));
}
Expand Down
2 changes: 2 additions & 0 deletions lib/Watcher.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ export default class Watcher {
'anyof',
['suffix', 'js'],
['suffix', 'jsx'],
['suffix', 'ts'],
['suffix', 'tsx'],
['suffix', 'json'],
],
fields: ['name', 'exists', 'mtime_ms'],
Expand Down
2 changes: 1 addition & 1 deletion lib/__tests__/Configuration-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ describe('Configuration', () => {
expect(configuration.get('sortImports')).toEqual(true);
expect(configuration.get('importDevDependencies')).toEqual(false);
expect(configuration.get('importFunction')).toEqual('require');
expect(configuration.get('stripFileExtensions')).toEqual(['.js', '.jsx']);
expect(configuration.get('stripFileExtensions')).toEqual(['.js', '.jsx', '.ts', '.tsx']);
expect(configuration.get('useRelativePaths')).toEqual(true);
expect(configuration.get('maxLineLength')).toEqual(80);
expect(configuration.get('tab')).toEqual(' ');
Expand Down
4 changes: 3 additions & 1 deletion lib/__tests__/ImportStatements-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,15 @@ import ImportStatements from '../ImportStatements';
import findCurrentImports from '../findCurrentImports';
import parse from '../parse';

const local = subPath => path.resolve(__dirname, subPath);

jest.mock('../FileUtils');

function prepare(importString) {
return findCurrentImports(
new Configuration(),
importString,
parse(importString),
parse(importString, local('foo.js')),
).imports;
}

Expand Down
33 changes: 18 additions & 15 deletions lib/__tests__/findCurrentImports-test.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
import path from 'path';
import Configuration from '../Configuration';
import findCurrentImports from '../findCurrentImports';
import parse from '../parse';

const local = subPath => path.resolve(__dirname, subPath);

it('finds require statements', () => {
const currentFileContent = `
const React = require('react');
Expand All @@ -20,7 +23,7 @@ module.exports = AlignmentRibbonPage;
const imports = findCurrentImports(
new Configuration('./foo.js'),
currentFileContent,
parse(currentFileContent),
parse(currentFileContent, local('foo.js')),
);
expect(imports.imports.size()).toEqual(1);
imports.imports.forEach((imp) => {
Expand Down Expand Up @@ -49,7 +52,7 @@ module.exports = AlignmentRibbonPage;
const imports = findCurrentImports(
new Configuration('./foo.js'),
currentFileContent,
parse(currentFileContent),
parse(currentFileContent, local('foo.js')),
);
expect(imports.imports.size()).toEqual(1);
imports.imports.forEach((imp) => {
Expand All @@ -68,7 +71,7 @@ import { type foo, bar, type baz } from 'far';
const imports = findCurrentImports(
new Configuration('./foo.js'),
currentFileContent,
parse(currentFileContent),
parse(currentFileContent, local('foo.js')),
);
expect(imports.imports.size()).toEqual(1);
imports.imports.forEach((imp) => {
Expand All @@ -90,7 +93,7 @@ import { foo, bar, } from 'far';
const imports = findCurrentImports(
new Configuration('./foo.js'),
currentFileContent,
parse(currentFileContent),
parse(currentFileContent, local('foo.js')),
);
expect(imports.imports.size()).toEqual(1);
imports.imports.forEach((imp) => {
Expand All @@ -115,7 +118,7 @@ import {
const imports = findCurrentImports(
new Configuration('./foo.js'),
currentFileContent,
parse(currentFileContent),
parse(currentFileContent, local('foo.js')),
);
expect(imports.imports.size()).toEqual(1);
imports.imports.forEach((imp) => {
Expand All @@ -137,7 +140,7 @@ import type { foo } from 'far';
const imports = findCurrentImports(
new Configuration('./foo.js'),
currentFileContent,
parse(currentFileContent),
parse(currentFileContent, local('foo.js')),
);
expect(imports.imports.size()).toEqual(1);
imports.imports.forEach((imp) => {
Expand All @@ -156,7 +159,7 @@ import type { foo, bar } from 'far';
const imports = findCurrentImports(
new Configuration('./foo.js'),
currentFileContent,
parse(currentFileContent),
parse(currentFileContent, local('foo.js')),
);
expect(imports.imports.size()).toEqual(1);
imports.imports.forEach((imp) => {
Expand All @@ -175,7 +178,7 @@ import far, { type foo, bar } from 'far';
const imports = findCurrentImports(
new Configuration('./foo.js'),
currentFileContent,
parse(currentFileContent),
parse(currentFileContent, local('foo.js')),
);
expect(imports.imports.size()).toEqual(1);
imports.imports.forEach((imp) => {
Expand All @@ -195,7 +198,7 @@ const { foo, bar } = require('far');
const imports = findCurrentImports(
new Configuration('./foo.js'),
currentFileContent,
parse(currentFileContent),
parse(currentFileContent, local('foo.js')),
);
expect(imports.imports.size()).toEqual(1);
imports.imports.forEach((imp) => {
Expand All @@ -216,7 +219,7 @@ const { foo, bar, } = require('far');
const imports = findCurrentImports(
new Configuration('./foo.js'),
currentFileContent,
parse(currentFileContent),
parse(currentFileContent, local('foo.js')),
);
imports.imports.forEach((imp) => {
expect(imp.danglingCommas).toBe(true);
Expand All @@ -231,7 +234,7 @@ import * as api from './api'
const imports = findCurrentImports(
new Configuration('./foo.js'),
currentFileContent,
parse(currentFileContent),
parse(currentFileContent, local('foo.js')),
);
expect(imports.imports.size()).toEqual(1);
imports.imports.forEach((imp) => {
Expand All @@ -253,7 +256,7 @@ const Tar = require('tar');
const imports = findCurrentImports(
new Configuration('./foo.js'),
currentFileContent,
parse(currentFileContent),
parse(currentFileContent, local('foo.js')),
);
expect(imports.imports.size()).toEqual(0);
expect(imports.range.start).toEqual(0);
Expand All @@ -269,7 +272,7 @@ const Tar = require('tar');
const imports = findCurrentImports(
new Configuration('./foo.js'),
currentFileContent,
parse(currentFileContent),
parse(currentFileContent, local('foo.js')),
);
expect(imports.imports.size()).toEqual(1);
expect(imports.range.start).toEqual(1);
Expand All @@ -282,7 +285,7 @@ it("doesn't fail for assignment-less variable declarations", () => {
const imports = findCurrentImports(
new Configuration('./foo.js'),
currentFileContent,
parse(currentFileContent),
parse(currentFileContent, local('foo.js')),
);
expect(imports.imports.size()).toEqual(0);
});
Expand All @@ -296,7 +299,7 @@ const Tar = require('tar');
const imports = findCurrentImports(
new Configuration('./foo.js'),
currentFileContent,
parse(currentFileContent),
parse(currentFileContent, local('foo.js')),
);
expect(imports.imports.size()).toEqual(1);
expect(imports.range.start).toEqual(1);
Expand Down
8 changes: 8 additions & 0 deletions lib/__tests__/findExports-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -427,6 +427,14 @@ it('does not fail for inner exports that are not objects', () => {
});
});

it('finds typescript exports', () => {
expect(findExports('export type FooType = {}; export interface FooInterface {}; export enum FooEnum {};', '/path/to/file.ts')).toEqual({
named: ['FooType', 'FooInterface', 'FooEnum'],
typed: [],
hasDefault: false,
});
});

describe('recursive exports', () => {
beforeEach(() => {
fs.__setFile(
Expand Down
Loading

0 comments on commit f64b7bd

Please sign in to comment.