Skip to content

refactor(typescript-estree): correct warnings reported by sonarlint #100

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 8 commits into from
Jan 22, 2019
Merged
6 changes: 3 additions & 3 deletions packages/typescript-estree/src/ast-converter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,11 @@ import { convertTokens } from './node-utils';
import ts from 'typescript';
import { Extra } from './temp-types-based-on-js-source';

export default (
export default function astConverter(
ast: ts.SourceFile,
extra: Extra,
shouldProvideParserServices: boolean
) => {
) {
/**
* The TypeScript compiler produced fundamental parse errors when parsing the
* source.
Expand Down Expand Up @@ -59,4 +59,4 @@ export default (
}

return { estree, astMaps };
};
}
8 changes: 4 additions & 4 deletions packages/typescript-estree/src/convert.ts
Original file line number Diff line number Diff line change
Expand Up @@ -694,8 +694,8 @@ export default function convert(config: ConvertConfig): ESTreeNode | null {
}

if (node.type) {
(result as any).id.typeAnnotation = convertTypeAnnotation(node.type);
fixTypeAnnotationParentLocation((result as any).id);
result.id!.typeAnnotation = convertTypeAnnotation(node.type);
fixTypeAnnotationParentLocation(result.id!);
}
break;
}
Expand Down Expand Up @@ -1249,7 +1249,7 @@ export default function convert(config: ConvertConfig): ESTreeNode | null {
expressions: []
});

node.templateSpans.forEach((templateSpan: any) => {
node.templateSpans.forEach(templateSpan => {
(result as any).expressions.push(convertChild(templateSpan.expression));
(result as any).quasis.push(convertChild(templateSpan.literal));
});
Expand Down Expand Up @@ -1333,7 +1333,7 @@ export default function convert(config: ConvertConfig): ESTreeNode | null {
}

if (node.questionToken) {
(parameter as any).optional = true;
parameter.optional = true;
}

if (node.modifiers) {
Expand Down
4 changes: 1 addition & 3 deletions packages/typescript-estree/src/parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -213,10 +213,8 @@ function generateAST<T extends ParserOptions = ParserOptions>(
tsNodeToESTreeNodeMap?: WeakMap<object, any>;
};
} {
const toString = String;

if (typeof code !== 'string' && !((code as any) instanceof String)) {
code = toString(code);
code = String(code);
}

resetExtra();
Expand Down
80 changes: 80 additions & 0 deletions packages/typescript-estree/tests/lib/__snapshots__/parse.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -188,3 +188,83 @@ Object {
"type": "Program",
}
`;

exports[`parse() non string code should correctly convert code to string 1`] = `
Object {
"body": Array [
Object {
"expression": Object {
"loc": Object {
"end": Object {
"column": 5,
"line": 1,
},
"start": Object {
"column": 0,
"line": 1,
},
},
"range": Array [
0,
5,
],
"raw": "12345",
"type": "Literal",
"value": 12345,
},
"loc": Object {
"end": Object {
"column": 5,
"line": 1,
},
"start": Object {
"column": 0,
"line": 1,
},
},
"range": Array [
0,
5,
],
"type": "ExpressionStatement",
},
],
"comments": Array [],
"loc": Object {
"end": Object {
"column": 5,
"line": 1,
},
"start": Object {
"column": 0,
"line": 1,
},
},
"range": Array [
0,
5,
],
"sourceType": "script",
"tokens": Array [
Object {
"loc": Object {
"end": Object {
"column": 5,
"line": 1,
},
"start": Object {
"column": 0,
"line": 1,
},
},
"range": Array [
0,
5,
],
"type": "Numeric",
"value": "12345",
},
],
"type": "Program",
}
`;
62 changes: 58 additions & 4 deletions packages/typescript-estree/tests/lib/parse.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
* MIT License
*/
import * as parser from '../../src/parser';
import * as astConverter from '../../src/ast-converter';
import { ParserOptions } from '../../src/temp-types-based-on-js-source';
import { createSnapshotTestBlock } from '../../tools/test-utils';

Expand All @@ -16,8 +17,8 @@ import { createSnapshotTestBlock } from '../../tools/test-utils';
describe('parse()', () => {
describe('basic functionality', () => {
it('should parse an empty string', () => {
expect((parser as any).parse('').body).toEqual([]);
expect(parser.parse('', {} as any).body).toEqual([]);
expect(parser.parse('').body).toEqual([]);
expect(parser.parse('', {}).body).toEqual([]);
});
});

Expand All @@ -33,7 +34,7 @@ describe('parse()', () => {

describe('general', () => {
const code = 'let foo = bar;';
const config = {
const config: ParserOptions = {
comment: true,
tokens: true,
range: true,
Expand All @@ -42,7 +43,60 @@ describe('parse()', () => {

it(
'output tokens, comments, locs, and ranges when called with those options',
createSnapshotTestBlock(code, config as ParserOptions)
createSnapshotTestBlock(code, config)
);
});

describe('non string code', () => {
const code = (12345 as any) as string;
const config: ParserOptions = {
comment: true,
tokens: true,
range: true,
loc: true
};

it(
'should correctly convert code to string',
createSnapshotTestBlock(code, config)
);
});

describe('loggerFn should be propagated to ast-converter', () => {
it('output tokens, comments, locs, and ranges when called with those options', () => {
const spy = jest.spyOn(astConverter, 'default');

const loggerFn = jest.fn(() => true);

parser.parse('let foo = bar;', {
loggerFn,
comment: true,
tokens: true,
range: true,
loc: true
});

expect(spy).toHaveBeenCalledWith(
jasmine.any(Object),
{
code: 'let foo = bar;',
comment: true,
comments: [],
errorOnTypeScriptSyntacticAndSemanticIssues: false,
errorOnUnknownASTType: false,
extraFileExtensions: [],
jsx: false,
loc: true,
log: loggerFn,
projects: [],
range: true,
strict: false,
tokens: jasmine.any(Array),
tsconfigRootDir: jasmine.any(String),
useJSXTextNode: false
},
false
);
});
});
});
1 change: 1 addition & 0 deletions packages/typescript-estree/tools/test-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ export function parseCodeAndGenerateServices(
* and which performs an assertion on the snapshot for the given code and config.
* @param {string} code The source code to parse
* @param {ParserOptions} config the parser configuration
* @param {boolean} generateServices Flag determining whether to generate ast maps and program or not
* @returns {jest.ProvidesCallback} callback for Jest it() block
*/
export function createSnapshotTestBlock(
Expand Down