BANG88/typescript-react-intl

element.forEach is not a function

Closed this issue · 5 comments

When upgrade TypeScript to 2.2.0 I get the following error.

bash-3.2$ ts-node utility/extractStrings.ts
TypeError: element.forEach is not a function
    at find (/Users/mdentremont/Projects/REACT/ApolloX.Builder/node_modules/typescript-react-intl/index.ts:48:25)
    at findFirstJsxOpeningLikeElementWithName (/Users/mdentremont/Projects/REACT/ApolloX.Builder/node_modules/typescript-react-intl/index.ts:38:5)
    at main (/Users/mdentremont/Projects/REACT/ApolloX.Builder/node_modules/typescript-react-intl/index.ts:84:14)
    at /Users/mdentremont/Projects/REACT/ApolloX.Builder/utility/extractStrings.ts:29:25
    at Array.forEach (native)
    at /Users/mdentremont/Projects/REACT/ApolloX.Builder/utility/extractStrings.ts:27:15
    at f (/Users/mdentremont/Projects/REACT/ApolloX.Builder/node_modules/once/once.js:25:25)
    at Glob.<anonymous> (/Users/mdentremont/Projects/REACT/ApolloX.Builder/node_modules/glob/glob.js:151:7)
    at emitOne (events.js:96:13)
    at Glob.emit (events.js:188:7)

When I attempted to debug the problem, but cannot rebuild with the current source.
I get the following error when yarn start

bash-3.2$ yarn start
yarn start v0.19.1
$ tsc
index.ts(63,34): error TS2339: Property 'text' does not exist on type 'PrimaryExpression'.
error Command failed with exit code 2.

I'll have to look into this more when I have some time

@cramhead Could you show me the code ts-node utility/extractStrings.ts or provide a repro repo would be fine.

Sure.

import * as fs from 'fs';
/* Role: Utility function that extract ids and defaultMessages from FormattedMessages and defineMessages into i18n/en.tsx */
import * as glob from 'glob';
// tslint:disable-next-line:no-var-requires
const parser = require('typescript-react-intl').default;

interface ParsedString { id: string; defaultMessage: string; };
type ParsedStrings = [ParsedString];

// Visits all files as defined by the pattern. When complete calls the back with results.
function visit(pattern, callback: (parsedStrings) => void) {
    let results: ParsedString[] = [];

    pattern = pattern || 'src/**/*.@(tsx|ts)'; // defaults to searching through tsx and ts files
    // visit all files defined by the pattern and extract the internationalized strings

    // tslint:disable-next-line:only-arrow-functions
    glob(pattern, function (err, files) {
        if (err) {
            throw err;
        }

        files.forEach(f => {
            const contents = fs.readFileSync(f).toString();
            const res = parser(contents);
            results = results.concat(res);
        });

        // tslint:disable-next-line:no-unused-expression
        callback && callback(results);
    });
}

// tslint:disable-next-line:only-arrow-functions
visit(null, function (res: ParsedStrings) {
    const locale = {};
    const indentation = 4; // indent the output file by 4
    const comment = '/* Role: Main source localization extracted from the codebase */\n/* tslint:disable */\n'; // Put a comment at the top of the file

    res.forEach(r => {
        locale[r.id] = r.defaultMessage;
    });

    // save file to disk。you can save as a json file,just change the ext and contents as you want.
    fs.writeFileSync(`src/i18n/en.tsx`, `${comment} export default ${JSON.stringify(locale, null, indentation)};\r`);
});

Could you try it again,Thanks .